Skip to content
29 changes: 29 additions & 0 deletions web_programming/giphy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/python
import random
import json
import requests


api_key = "YOUR GIPHY API KEY"
# Can be fetched from https://developers.giphy.com/dashboard/


def getgif(query: str) -> str:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file web_programming/giphy.py, please provide doctest for the function getgif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding doctest will not be helpful in this scenario because the output(url of gif) is chosen at random.

"""
Get 1 URL of GIF on a given `query`

❯ python giphy.py
Topic: space+ship
Total urls received: 50
https://giphy.com/gifs/startrekfleetcommand-BPVIRni1Z70QO2nJMX
"""
qu: str = '+'.join(query.split(" "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
qu: str = '+'.join(query.split(" "))
query = '+'.join(query.split(" "))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍🏻

print("Topic: ", qu)
url: str = f"http://api.giphy.com/v1/gifs/search?q={qu}&api_key={api_key}"
jsdata = requests.get(url)
data: dict = json.loads(jsdata.content)
print("Total urls received: ", len(data['data']))
return data['data'][random.randint(0, 49)]['url']


print(getgif("space ship"))