Skip to content

Added giphy.py to fetch gifs on a given topic #5378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 18, 2021
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"))