Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to print comic information to STDOUT #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions xkcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

@click.command()
@click.option('--random', flag_value='random', default=False, help='Get Random Comic!')
def cli(random):
@click.option('--metadata', flag_value='metadata', default=False, help='Print Comic Metadata to Terminal')
@click.option('--noimage', flag_value='noimage', default=False, help='Do not display comic image.')
def cli(random,metadata,noimage):
"""XKCD Terminal Tool"""
try:
from sh import lolcat, figlet # Hacky fix for Build to pass system packages
Expand All @@ -26,13 +28,18 @@ def cli(random):
endpoint = "https://xkcd.com/{}/info.0.json".format(rand_digits)
content = s.get(endpoint).content.decode()
data = json.loads(content)

if noimage != "noimage":
res = s.get(data["img"])
img = Image.open(BytesIO(res.content))
img.show()
else:
res = s.get(data["img"])
img = Image.open(BytesIO(res.content))
img.show()

print("Title: ", data["title"])
print("Number:", data["num"])

if metadata == 'metadata':
print("Date: ", data["year"]+"/"+data["month"]+"/"+data["day"])
print("alt: ", data ["alt"])

except requests.ConnectionError:
error_image = Image.open("assets/xkcd_404.jpg")
Expand Down