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
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`

>>> getgif("space ship")
Topic: space+ship
Total urls received: 50
https://giphy.com/gifs/startrekfleetcommand-BPVIRni1Z70QO2nJMX
"""
formatted_query: str = "+".join(query.split(" "))
print("Topic: ", formatted_query)
url: str = f"http://api.giphy.com/v1/gifs/search?q={formatted_query}&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"))