Created
January 15, 2022 18:12
-
-
Save Syndesi/a282c0c5716d61faf257a5af1825ef44 to your computer and use it in GitHub Desktop.
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
# wip, early code, non-python-standards etc. | |
from lxml import etree | |
import datetime | |
import requests | |
import shutil | |
import sys | |
from loguru import logger | |
logger.add("taskGoComic.log") | |
def downloadGoComicImage(comic, date, filename): | |
# get comic page HTML | |
url = f"https://www.gocomics.com/{comic}/{date.year}/{date.month}/{date.day}" | |
logger.info("Comic url: {}", url) | |
r = requests.get(url) | |
body = r.text | |
# get comic image URL from HTML | |
parser = etree.XMLParser(recover=True) | |
tree = etree.XML(body, parser) | |
imageUrl = tree.xpath('//div[@class="comic__container"]//picture[@class="item-comic-image"]/img/@src') | |
if len(imageUrl) == 0: | |
logger.error("no image found") | |
return False | |
imageUrl = imageUrl[0] | |
logger.info("image url: {}", imageUrl) | |
# download comic image | |
r = requests.get(imageUrl, stream = True) | |
if r.status_code == 200: | |
r.raw.decode_content = True | |
with open(filename,'wb') as f: | |
shutil.copyfileobj(r.raw, f) | |
logger.info("image successfully downloaded: {}", filename) | |
else: | |
logger.error("iamge could not be downloaded") | |
return False | |
return True | |
def downloadLastGoComicImage(comic, filename, tries = 5): | |
now = datetime.datetime.now() | |
downloaded = False | |
while downloaded == False and tries > 0: | |
downloaded = downloadGoComicImage(comic, now, filename) | |
now = now - datetime.timedelta(days=1) | |
tries -= 1 | |
return downloaded | |
logger.info("Task started") | |
now = datetime.datetime.now() | |
date = f"{now.year}{now.month}{now.day}" | |
downloadGoComicImage('bleeker', now, f"assets/{date}-bleeker.gif") | |
downloadGoComicImage('wallace-the-brave', now, f"assets/{date}-wallace-the-brave.gif") | |
downloadGoComicImage('mannequin-on-the-moon', now, f"assets/{date}-mannequin-on-the-moon.gif") | |
downloadGoComicImage('calvinandhobbes', now, f"assets/{date}-calvinandhobbes.gif") | |
downloadGoComicImage('peanuts', now, f"assets/{date}-peanuts.gif") | |
downloadLastGoComicImage('doodle-town', f"assets/{date}-doodle-town.gif") | |
logger.info("Task finished") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment