forked from illacceptanything/illacceptanything
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-xkcd.py
More file actions
18 lines (14 loc) · 700 Bytes
/
random-xkcd.py
File metadata and controls
18 lines (14 loc) · 700 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import rng
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
random_xkcd_num = rng.get_random_number() % current_xkcd_num
# Get the URL of the image of the random comic
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.png', 'wb') as out_file:
shutil.copyfileobj(response_img.raw, out_file)