【发布时间】:2019-05-10 15:50:41
【问题描述】:
我想根据用户输入生成 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)`
【问题讨论】:
-
(与问题无关)-在函数调用中导入导入是否有特定原因?
-
另外,我不知道你为什么将
request传递给函数... -
我正在使用函数调用来填充 img 源。对其他设计输入开放
标签: python django python-requests giphy giphy-api