-
Notifications
You must be signed in to change notification settings - Fork 67
/
xkcd_get.py
46 lines (34 loc) · 1.21 KB
/
xkcd_get.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
import logging
import os
from PIL import Image
from utility import is_stale, configure_logging
import sys
configure_logging()
def xkcd_get_img():
xkcd_file_name = "xkcd-comic-strip.png"
if not is_stale(xkcd_file_name, 3600):
logging.info("xkcd-comic-strip.png is still fresh. Skipping download.")
sys.exit(1)
logging.info("Downloading xkcd-json")
response = requests.get("https://xkcd.com/info.0.json")
result = response.json()
logging.info("Downloading xkcd_img")
logging.info(result["img"])
path = os.path.dirname(os.path.realpath(__file__))
filename = path + '/' + os.path.basename(xkcd_file_name)
if os.path.exists(filename):
os.remove(filename)
image_response = requests.get(result["img"])
open(filename, 'wb').write(image_response.content)
logging.info("Resizing the image to fit the screen. Disortions can happen.")
im = Image.open(filename)
logging.debug("PNG size: ",im.size)
width = int(os.environ.get('WAVESHARE_WIDTH'))
height = int(os.environ.get('WAVESHARE_HEIGHT'))
im = im.resize((width,height))
im.save(filename, "PNG")
def main():
xkcd_get_img()
if __name__ == "__main__":
main()