-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
633327c
Added giphy.py to fetch gifs on a given topic
JDeepD 6f619cb
Modified code
JDeepD e43618a
Minor change
JDeepD 25c9d3b
Minor refactoring to avoid name clash
JDeepD 2046753
Made necessary changes as per review
JDeepD 4997f35
Merge branch 'TheAlgorithms:master' into dev
JDeepD 157e0f6
Update web_programming/giphy.py
JDeepD c44299c
Apply suggestions from code review
cclauss 6dd357a
Final cleanup
cclauss 8eb25e7
Placate psf/black
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,29 +1,25 @@ | ||||||||||||
#!/bin/python | ||||||||||||
import random | ||||||||||||
import json | ||||||||||||
import requests | ||||||||||||
|
||||||||||||
|
||||||||||||
api_key = "YOUR GIPHY API KEY" | ||||||||||||
api_key = "YOUR API KEY" | ||||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
# Can be fetched from https://developers.giphy.com/dashboard/ | ||||||||||||
|
||||||||||||
|
||||||||||||
def getgif(query: str) -> str: | ||||||||||||
def getgif(query: str) -> list: | ||||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
""" | ||||||||||||
Get 1 URL of GIF on a given `query` | ||||||||||||
Get a list of URLs of GIF on a given `query` | ||||||||||||
|
||||||||||||
>>> getgif("space ship") | ||||||||||||
Topic: space+ship | ||||||||||||
Total urls received: 50 | ||||||||||||
https://giphy.com/gifs/startrekfleetcommand-BPVIRni1Z70QO2nJMX | ||||||||||||
>>> getgif("space ship") | ||||||||||||
['https://giphy.com/gifs/studiosoriginals-3ov9jPghXAsbvLmcjm', 'https://giphy.com/gifs/primevideo-the-expanse-theexpanse-2daHMQ9QPwmoSsRYWZ', 'https://giphy.com/gifs/newyorker-space-rocket-bkgsATGfFyrKUf1IsO', 'https://giphy.com/gifs/CBSAllAccess-star-trek-startrek-discovery-pI9dBz15pbGJg03LnE', 'https://giphy.com/gifs/nowthis-aliens-ufo-unidentified-flying-object-TZnOtiTDAnsQUwLs3j', 'https://giphy.com/gifs/DrSquatchSoapCo-mars-bar-marsbar-bars-H1k0w5P3beQHf8OIrr', 'https://giphy.com/gifs/heyduggee-hey-duggee-thespacebadge-XaMTNZkRahZ7ysPMci', 'https://giphy.com/gifs/doctorwho-doctor-who-dr-series-12-PkS3n3TERbjIntnx9E', 'https://giphy.com/gifs/startrekfleetcommand-H03zikfKQPxzuS4ZcE', 'https://giphy.com/gifs/startrekfleetcommand-htJzM2rN0DgbS8g8nC', 'https://giphy.com/gifs/animation-space-weird-RIUZHIBmReqsnztdW2', 'https://giphy.com/gifs/doctorwho-doctor-who-dr-series-12-Vi0EFzgMLWhMOo76SE', 'https://giphy.com/gifs/startrekfleetcommand-BPVIRni1Z70QO2nJMX', 'https://giphy.com/gifs/lego-star-wars-space-SiEz6hxdcJuOf2n3TE', 'https://giphy.com/gifs/nickelodeon-space-astronauts-life-in-krr7nv4iK6Hw2vK5S6', 'https://giphy.com/gifs/rocket-space-ship-X0oEvTEdLj2YU', 'https://giphy.com/gifs/southparkgifs-l2Sqed5gI8GMP09So', 'https://giphy.com/gifs/S3bZdGN7y2MQyNgPF1', 'https://giphy.com/gifs/heyduggee-hey-duggee-thespacebadge-MB0tFoeiKyrdhWAXPM', 'https://giphy.com/gifs/star-wars-teddy-bear-tie-fighter-EpGU43iHxzKrZe3yVv', 'https://giphy.com/gifs/thesims-the-sims-thesimsxstarwars-thesims4starwarsjourneytobatuu-THCL5K4oXsF90sps16', 'https://giphy.com/gifs/time-regret-machine-73h4dfm9DaTRrhFko9', 'https://giphy.com/gifs/space-universe-rocket-l41m6oJBtKRm1NseA', 'https://giphy.com/gifs/thesims-ts-sims-the-Xy21pj6ClKwkz2KsQC', 'https://giphy.com/gifs/CBeebiesHQ-cbeebies-button-clangers-ZEr5tiKWdvpfYa2L9V', 'https://giphy.com/gifs/doctorwho-doctor-who-dalek-daleks-a5EPJWqRznZw6SVXzG', 'https://giphy.com/gifs/peanuts-snoopy-woodstock-in-space-X1gjuOGjIZioNcqKCG', 'https://giphy.com/gifs/funny-weird-aliens-26xBQ7d3MeECbUpCU', 'https://giphy.com/gifs/CBSAllAccess-star-trek-discovery-BHJ8BRu1CgTjoDfvoo', 'https://giphy.com/gifs/doctorwho-doctor-who-dr-the-doctors-wife-L1Phzvvp8Zjfvgr2uy', 'https://giphy.com/gifs/startrekfleetcommand-NRfbanUlUPvdqLzd8O', 'https://giphy.com/gifs/CBeebiesHQ-space-wow-fCUBPlsv3FTkDqtgYG', 'https://giphy.com/gifs/startrekfleetcommand-4v1pOaMg4Z1pOUFK5K', 'https://giphy.com/gifs/animation-happy-K4HqMo1UoOjew', 'https://giphy.com/gifs/ralph-stairs-space-ship-flight-of-the-navigator-jsqUVil8tURoyQ9t1C', 'https://giphy.com/gifs/shocked-surprised-cow-jrht6TXUa0SYlVZFnQ', 'https://giphy.com/gifs/PERQDOTCOM-perq-bot-perqbot-fJ5veFeMaJd3UXGfc3', 'https://giphy.com/gifs/SignatureEntertainmentUK-wesley-snipes-the-recall-final-322VVjQkldB9IvPmbX', 'https://giphy.com/gifs/doctorwho-doctor-who-dr-the-fires-of-pompeii-jsl7zjaaK6IlSdbWeb', 'https://giphy.com/gifs/usnationalarchives-launch-apollo-11-blast-off-l3vR6QzrzOvAD8I7K', 'https://giphy.com/gifs/SignatureEntertainmentUK-wesley-snipes-the-recall-final-22OMufnulfcTGUErzG', 'https://giphy.com/gifs/doctor-who-pretty-time-kxAX99ncvbPk4', 'https://giphy.com/gifs/rocket-launch-spaceship-TRdADd4gJOQteAeY0z', 'https://giphy.com/gifs/doctorwho-doctor-who-dr-the-doctors-wife-YOGLP2Z7eaMRDsPwmD', 'https://giphy.com/gifs/rocket-austin-powers-UtymVt10zlXPCo6FL3', 'https://giphy.com/gifs/AdVentureCapitalistHH-capitalism-adcap-adventure-capitalist-4EJXnSY3oPsQ4xvpfa', 'https://giphy.com/gifs/ralph-scifi-space-ship-xT1Ra1Nu4MaJqHIBgY', 'https://giphy.com/gifs/world-spacex-grasshopper-huvlSnGxC0rT2', 'https://giphy.com/gifs/SignatureEntertainmentUK-wesley-snipes-the-recall-final-7TiefcAB5P4nlV1Ok5', 'https://giphy.com/gifs/SignatureEntertainmentUK-wesley-snipes-the-recall-final-8vLnUghW4aamUxtTy2'] | ||||||||||||
""" | ||||||||||||
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"] | ||||||||||||
formatted_query = "+".join(query.split(" ")) | ||||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
url = f"http://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}" | ||||||||||||
data = requests.get(url).json() | ||||||||||||
url_data = [i["url"] for i in data["data"]] | ||||||||||||
|
||||||||||||
return url_data | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
print(getgif("space ship")) | ||||||||||||
|
||||||||||||
if __name__ == "__main__": | ||||||||||||
print(getgif("space ship")) | ||||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.