Created
April 8, 2015 18:23
-
-
Save gootik/2c2c6babbeff94da7082 to your computer and use it in GitHub Desktop.
random XKCD
This file contains 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
import random | |
import requests | |
import shutil | |
# Get the number of the latest xkcd | |
current_xkcd_num = int(requests.get('http://xkcd.com/info.0.json').json()['num']) | |
# Generate a random number using rng.py (expecting a natural number) | |
# Mod it by current_xkcd_num to give us the number of a valid xkcd comic | |
## Get the URL of the image of the random comic | |
#random_xkcd_num = rng.get_random_number() % current_xkcd_num | |
random_xkcd_num = random.randint(1,current_xkcd_num) | |
img_url = requests.get('http://xkcd.com/' + str(random_xkcd_num) + '/info.0.json').json()['img'] | |
# Get the random comic and save it as 'dank.png' | |
response_img = requests.get(img_url, stream=True) | |
with open('dank' + str(random_xkcd_num) + '.png', 'wb') as out_file: | |
shutil.copyfileobj(response_img.raw, out_file) | |
print 'dank' + str(random_xkcd_num) + '.png' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment