Skip to content

Commit

Permalink
random
Browse files Browse the repository at this point in the history
Resolves #11
  • Loading branch information
nogarcia committed Apr 26, 2019
1 parent 1bd187b commit 182a318
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions xkcdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
import requests
from PIL import Image
from io import BytesIO
import random

@click.command() # See click decorators
@click.option('--output', default=None, help='Output file') # output file
@click.option('--num', default=None, help='Comic number to retrieve')
@click.option('--popup', flag_value=True, default=False, help="Show in Pillow window")
def cli(output, num, popup):
@click.option('--random', 'randYN', flag_value=True, default=False, help="Get random comic")
def cli(output, num, popup, randYN):
if (output != None and popup == True):
raise Exception("Cannot download and not download comic.")
if (num == None):
if (num == None and randYN != True):
r = requests.get("http://xkcd.com/info.0.json") # get latest comic
elif (randYN == True):
ra = requests.get("http://xkcd.com/info.0.json")
raData = json.loads(ra.text)
raRes = int(raData["num"])
num = random.randint(1, raRes+1)
r = requests.get("https://xkcd.com/{}/info.0.json".format(num))
else:
try:
r = requests.get("https://xkcd.com/{}/info.0.json".format(num))
Expand Down

0 comments on commit 182a318

Please sign in to comment.