Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
WARNING: NOT REALLY A GOOD THING.
import requests
GIPHY_API_KEY = "dc6zaTOxFJmzC" # Giphy public beta API key
def decode(s):
"""
Convert base62 (i.e. a-z,A-Z,0-9) to base 10
PILLAGED FROM https://gist.github.com/adyliu/4494223
"""
basedigits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
BASE = len(basedigits)
ret, mult = 0, 1
for c in reversed(s):
ret += mult*basedigits.index(c)
mult *= BASE
return ret
def get_random_from_giphy():
"""
Get a random(ish, maybe?) number from Giphy.
Requests a random gif, looks at the ID.
Converts last 5 chars to integer. Ignore other ID chars since they might have deliberately skipped some IDs...?
This assumes that the ID is in base62, which it probably is...
"""
r = requests.get("http://api.giphy.com/v1/gifs/random?api_key={0}".format(GIPHY_API_KEY))
random_string = r.json()["data"]["id"][:5] #ignore all but the last 5 chars for... reasons?
return decode(random_string)
get_random_from_giphy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment