From ccf53199615e1759c083313e3411088b9afcb80d Mon Sep 17 00:00:00 2001 From: June Date: Thu, 28 Aug 2025 10:25:46 +1200 Subject: [PATCH] Add AWS Lambda file --- lambda.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 lambda.py diff --git a/lambda.py b/lambda.py new file mode 100644 index 0000000..03cdc4a --- /dev/null +++ b/lambda.py @@ -0,0 +1,63 @@ +from urllib import request as req +import base64 + +# running on AWS Lambda (currently) +def lambda_handler(event, context): + # check is date has been passed in the event, else use today + print("EVT", event) + if ('body' in event): + b64date = event['body'] + try: + date = base64.b64decode(b64date).decode("utf8") + except: + date = event['body'] + print("sanity check:", date, type(date)) + return comicWithDate(date) + + r = req.Request('https://www.creators.com/read/heathcliff', headers={'User-Agent': 'Mozilla/5.0'}) + html = req.urlopen(r).read().decode("utf8") + + urlStart = html.index('https://cdn.alphacomedy.com') + imageUrl = html[urlStart:].split("\"")[0] + + response = { + 'statusCode': 200, + 'body': imageUrl + } + + return response + + +# date is a string, formatted as %Y/%m/%d +def comicWithDate(date): + notFoundURL = 'https://cdn.discordapp.com/attachments/760773607550353428/1120629965721960498/heathcliff_notfound.png' + + html = None + comicURL = f"https://www.gocomics.com/heathcliff/{date}" + + r = req.Request(comicURL, headers={'User-Agent': 'Mozilla/5.0'}) + + try: + html = req.urlopen(r).read().decode("utf8") + except: + return { + 'statusCode': 404, + 'body': notFoundURL + } + + # check to make sure image exists today (it REALLY should given the above block but idc im being cautious) + if "data-image" in html: + urlStart = html.index("data-image") + 12 + imageUrl = html[urlStart:].split("\"")[0] + else: + return { + 'statusCode': 404, + 'body': notFoundURL + } + + response = { + 'statusCode': 200, + 'body': imageUrl + } + + return response \ No newline at end of file