0

我想根据用户输入生成 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)`
4

1 回答 1

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)
于 2018-12-09T15:50:17.893 回答