python - 根据用户输入生成 gif
问题描述
我想根据用户输入生成 gif。我可以使用以下代码生成单个 gif:`
def getGif(request):
import time
import giphy_client
from giphy_client.rest import ApiException
from pprint import pprint
# create an instance of the API class
api_instance = giphy_client.DefaultApi()
api_key = 'dc6zaTOxFJmzC' # str | Giphy API Key.
q = 'rainbow' # str | Search query term or prhase.
limit = 25 # int | The maximum number of records to return. (optional) (default to 25)
offset = 0 # int | An optional results offset. Defaults to 0. (optional) (default to 0)
rating = 'g' # str | Filters results by specified rating. (optional)
lang = 'en' # str | Specify default country for regional content; use a 2-letter ISO 639-1 country code. See list of supported languages <a href = \"../language-support\">here</a>. (optional)
fmt = 'json' # str | Used to indicate the expected response format. Default is Json. (optional) (default to json)
try:
# Search Endpoint
api_response = api_instance.gifs_search_get(api_key, q, limit=limit, offset=offset, rating=rating, lang=lang,
fmt=fmt)
# pprint
img = requests.get(api_response.data[0].images.fixed_height_downsampled.url)
return HttpResponse(img,content_type='image/gif')
#return HttpResponse(api_response.data[0].images.fixed_height_downsampled.url, content_type="application/json")
except ApiException as e:
print("Exception when calling DefaultApi->gifs_search_get: %s\n" % e)`
解决方案
也许我不完全理解您正在 ping 的 API,但是这样要求输入是否有问题:
def getGif(request):
import time
import giphy_client
from giphy_client.rest import ApiException
from pprint import pprint
# create an instance of the API class
api_instance = giphy_client.DefaultApi()
api_key = 'dc6zaTOxFJmzC' # str | Giphy API Key.
# ASK FOR USER INPUT TO ALTER THE QUERY
q = input("What gifs would you like to find? ")
limit = 25 # int | The maximum number of records to return. (optional) (default to 25)
offset = 0 # int | An optional results offset. Defaults to 0. (optional) (default to 0)
rating = 'g' # str | Filters results by specified rating. (optional)
lang = 'en' # str | Specify default country for regional content; use a 2-letter ISO 639-1 country code. See list of supported languages <a href = \"../language-support\">here</a>. (optional)
fmt = 'json' # str | Used to indicate the expected response format. Default is Json. (optional) (default to json)
try:
# Search Endpoint
api_response = api_instance.gifs_search_get(api_key, q, limit=limit, offset=offset, rating=rating, lang=lang,
fmt=fmt)
# pprint
img = requests.get(api_response.data[0].images.fixed_height_downsampled.url)
return HttpResponse(img,content_type='image/gif')
#return HttpResponse(api_response.data[0].images.fixed_height_downsampled.url, content_type="application/json")
except ApiException as e:
print("Exception when calling DefaultApi->gifs_search_get: %s\n" % e)
推荐阅读
- python - How to specify timeout for firebase.put() in python?
- c# - 我应该如何像游戏“Lingo”一样为字母着色
- spring-boot - Not able to establish connection to the kerberos and SASL enabled kafka producer
- python - Comparing between entries in a list of lists using correct indexes
- nestjs - NESTJS 中的 TypeORM 实体 - 无法在模块外使用导入语句
- javascript - 我们可以在javascript中单击事件完成后执行操作吗?
- android - 从服务器转储数据库是不可能的吗?
- python - 如何制作打印和清除按钮 GUI,在 Python 中打印和清除标签?
- oracle - 下拉过滤器级别明智
- python - 如何在每辆车上独立更改 x 和 y 轴的值?