Skip to content

Commit 0935ab0

Browse files
JDeepDcclauss
andauthored
Added giphy.py to fetch gifs on a given topic (#5378)
* Added giphy.py to fetch gifs on a given topic * Modified code [*]Added doctest [*]Formatted with black * Minor change * Minor refactoring to avoid name clash * Made necessary changes as per review * Update web_programming/giphy.py Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestions from code review * Final cleanup * Placate psf/black Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 1e64bf4 commit 0935ab0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

web_programming/giphy.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
import requests
3+
4+
giphy_api_key = "YOUR API KEY"
5+
# Can be fetched from https://developers.giphy.com/dashboard/
6+
7+
8+
def get_gifs(query: str, api_key: str = giphy_api_key) -> list:
9+
"""
10+
Get a list of URLs of GIFs based on a given query..
11+
"""
12+
formatted_query = "+".join(query.split())
13+
url = f"http://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}"
14+
gifs = requests.get(url).json()["data"]
15+
return [gif["url"] for gif in gifs]
16+
17+
18+
if __name__ == "__main__":
19+
print("\n".join(get_gifs("space ship")))

0 commit comments

Comments
 (0)