0

Here is the code I have, it works fine, but how could I make it that there is a new variable that is equal to the Image URL of the GIF so that the user can get the Source URL of the GIF?

import requests

url = "https://giphy.p.rapidapi.com/v1/gifs/search"

searchtag = input()

querystring = {"api_key":"secret_key","q":searchtag,"limit":"1","offset":"0","rating":"pg-13"}

headers = {
    'x-rapidapi-key': "4005b98f9bmsh977c629b89034a7p19b52fjsn22b7fb5ce3bb",
    'x-rapidapi-host': "giphy.p.rapidapi.com"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

1 Answers1

0

First off you should probably remove your api key for your own personal safety . I'd recommend looking into environmental files for storing private information that doesn't belong in a database. Second off to answer your question you could just declare the url as a variable if you're feeling ever so inclined, here:

    import requests

url = "https://giphy.p.rapidapi.com/v1/gifs/search"

searchtag = input()

querystring = {"api_key":process.env.APIKEY,"q":searchtag,"limit":"1","offset":"0","rating":"pg-13"}

headers = {
    'x-rapidapi-key': "4005b98f9bmsh977c629b89034a7p19b52fjsn22b7fb5ce3bb",
    'x-rapidapi-host': "giphy.p.rapidapi.com"
    }

response = requests.request("GET", url, headers=headers, params=querystring)
gifurl = response.text
print(gifurl)
tear
  • 11
  • 3
  • That isn't currectly that helpful to me, I was wondering how I could get the GIF Source URL, not all the stuff. Just the URL of the GIF, e.g.https://media2.giphy.com/media/LmNwrBhejkK9EFP504/giphy.gif?cid=5a95d078edh4mlvitozznw09hf8txq8myq9pimlahpjpe7fh&rid=giphy.gif&ct=g – Vihaan Patil Aug 05 '21 at 05:40
  • try parsing it using periods between the path to the desired field ie, if the api returns { info: metadata: "metadata stuff here", sizeinfo: "size info here", sourceurl: "source url here" } then the path would be response.text.info.sourceurl – tear Aug 05 '21 at 06:12
  • How can I reference different parts of the response in this case? Your answer was otherwise helpful. I'll check the docs to see how I can reference my way around the response. – Vihaan Patil Aug 05 '21 at 06:29
  • Trying to parse with periods did not work for me, it just said object(str) has no attribute original – Vihaan Patil Aug 05 '21 at 06:51
  • hm ok ill take an actual look this time give me a sec just finishing up another project – tear Aug 05 '21 at 07:23
  • Are you still here? – Vihaan Patil Aug 07 '21 at 00:12