【问题标题】:Generating gifs based on a user input根据用户输入生成 gif
【发布时间】: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


【解决方案1】:

也许我不完全理解您正在 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2020-04-17
    相关资源
    最近更新 更多