Skip to content

Instantly share code, notes, and snippets.

@JosephRedfern
Last active August 2, 2016 16:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save JosephRedfern/ac89bc5d09cdf0961e26bb764abe32f0 to your computer and use it in GitHub Desktop.
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