From d31194332500dd4e5cc8afacc644765a419cd91b Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 30 Jun 2023 18:29:54 +0100 Subject: [PATCH 1/7] Revert "Quick fix for small images" This reverts commit 4b5b1936579cc0c7c87d5dfe523de30277c56101. --- combineImg/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/combineImg/__init__.py b/combineImg/__init__.py index 82dfb2e..e3ee318 100644 --- a/combineImg/__init__.py +++ b/combineImg/__init__.py @@ -69,8 +69,7 @@ def combineImages(imageArray, totalWidth, totalHeight,pad=True): x += image.size[0] y += imageArray[0].size[1] x = 0 - # paste the final image so that it's centered - newImage.paste(imageArray[2], (int((totalWidth - imageArray[2].size[0]) / 2), y)) + newImage.paste(imageArray[2], (x, y)) elif (len(imageArray) == 4): # if there are four images, combine the first two horizontally, then combine the last two vertically for image in imageArray[0:2]: newImage.paste(image, (x, y)) @@ -93,7 +92,7 @@ def saveImage(image, name): def genImage(imageArray): totalSize=getTotalImgSize(imageArray) combined = combineImages(imageArray, *totalSize) - + finalImg = combined.convert('RGB') bbox = finalImg.getbbox() From b4f903d0c8b42390af5c30e8ca6a334368991546 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 30 Jun 2023 18:30:00 +0100 Subject: [PATCH 2/7] Revert "Better image combination" This reverts commit 223d40eb9bc222c9e938b00a528927d965f7ea10. --- combineImg/__init__.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/combineImg/__init__.py b/combineImg/__init__.py index e3ee318..2784b9b 100644 --- a/combineImg/__init__.py +++ b/combineImg/__init__.py @@ -30,10 +30,9 @@ def scaleImageIterable(args): targetWidth = args[1] targetHeight = args[2] pad=args[3] - image = image.convert('RGBA') - image = ImageOps.expand(image,20) if pad: - newImg = ImageOps.contain(image, (targetWidth, targetHeight)) + image = image.convert('RGBA') + newImg = ImageOps.pad(image, (targetWidth, targetHeight),color=(0, 0, 0, 0)) else: newImg = ImageOps.fit(image, (targetWidth, targetHeight)) # scale + crop return newImg @@ -92,12 +91,11 @@ def saveImage(image, name): def genImage(imageArray): totalSize=getTotalImgSize(imageArray) combined = combineImages(imageArray, *totalSize) - - finalImg = combined.convert('RGB') - - bbox = finalImg.getbbox() - finalImg = finalImg.crop(bbox) - + combinedBG = combineImages(imageArray, *totalSize,False) + combinedBG = blurImage(combinedBG,50) + finalImg = Image.alpha_composite(combinedBG,combined) + #finalImg = ImageOps.pad(finalImg, findImageWithMostPixels(imageArray).size,color=(0, 0, 0, 0)) + finalImg = finalImg.convert('RGB') return finalImg def downloadImage(url): From a738923316b6a3e2e4689566d2fed4d8b8fa6ec4 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 30 Jun 2023 18:37:38 +0100 Subject: [PATCH 3/7] Cache combined images --- combineImg/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/combineImg/__init__.py b/combineImg/__init__.py index 2784b9b..cbfb7a9 100644 --- a/combineImg/__init__.py +++ b/combineImg/__init__.py @@ -133,7 +133,8 @@ def lambda_handler(event, context): 'statusCode': 200, "headers": { - "Content-Type": "image/jpeg" + "Content-Type": "image/jpeg", + "Cache-Control": "max-age=86400" }, 'body': combined_str, 'isBase64Encoded': True From 1e84875a8fde6bbfdb193c0f4b6df722cc2d29f1 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 30 Jun 2023 20:41:20 +0100 Subject: [PATCH 4/7] Move user agent check to function --- combineImg/__init__.py | 2 +- twitfix.py | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/combineImg/__init__.py b/combineImg/__init__.py index cbfb7a9..c70fffe 100644 --- a/combineImg/__init__.py +++ b/combineImg/__init__.py @@ -134,7 +134,7 @@ def lambda_handler(event, context): "headers": { "Content-Type": "image/jpeg", - "Cache-Control": "max-age=86400" + "Cache-Control": "public, max-age=86400" }, 'body': combined_str, 'isBase64Encoded': True diff --git a/twitfix.py b/twitfix.py index 38d3ce1..544b613 100644 --- a/twitfix.py +++ b/twitfix.py @@ -36,10 +36,13 @@ generate_embed_user_agents = [ "Synapse (bot; +https://github.com/matrix-org/synapse)", "test"] +def isValidUserAgent(user_agent): + return user_agent in generate_embed_user_agents + @app.route('/') # If the useragent is discord, return the embed, if not, redirect to configured repo directly def default(): user_agent = request.headers.get('user-agent') - if user_agent in generate_embed_user_agents: + if isValidUserAgent(user_agent): return message("TwitFix is an attempt to fix twitter video embeds in discord! created by Robin Universe :)\n\nšŸ’–\n\nClick me to be redirected to the repo!") else: return redirect(config['config']['repo'], 301) @@ -72,7 +75,7 @@ def twitfix(sub_path): return message(msgs.failedToScan) return make_cached_vnf_response(vnf,getTemplate("rawvideo.html",vnf,"","",clean,"","","","")) elif request.url.startswith("https://d.vx"): # Matches d.fx? Try to give the user a direct link - if user_agent in generate_embed_user_agents: + if isValidUserAgent(user_agent): twitter_url = config['config']['url'] + "/"+sub_path log.debug( "d.vx link shown to discord user-agent!") if request.url.endswith(".mp4") and "?" not in request.url: @@ -103,7 +106,7 @@ def twitfix(sub_path): if match.start() == 0: twitter_url = "https://twitter.com/" + sub_path - if user_agent in generate_embed_user_agents: + if isValidUserAgent(user_agent): res = embedCombined(twitter_url) return res @@ -125,7 +128,7 @@ def dir(sub_path): if match.start() == 0: twitter_url = "https://twitter.com/" + url - if user_agent in generate_embed_user_agents: + if isValidUserAgent(user_agent): res = embed_video(twitter_url) return res From 50b1f66c59f5631fef108124bebe4cc3e287526b Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 30 Jun 2023 21:00:04 +0100 Subject: [PATCH 5/7] Retry image downloads --- combineImg/__init__.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/combineImg/__init__.py b/combineImg/__init__.py index c70fffe..1f5846a 100644 --- a/combineImg/__init__.py +++ b/combineImg/__init__.py @@ -99,7 +99,12 @@ def genImage(imageArray): return finalImg def downloadImage(url): - return Image.open(BytesIO(get(url).content)) + for i in range(3): + try: + return Image.open(BytesIO(get(url).content)) + except: + pass + return None def genImageFromURL(urlArray): # this method avoids storing the images in disk, instead they're stored in memory @@ -110,6 +115,8 @@ def genImageFromURL(urlArray): imageArray = [executor.submit(downloadImage, url) for url in urlArray] imageArray = [future.result() for future in imageArray] print(f"Images downloaded in: {timer() - start}s") + if (None in imageArray): # return none if any of the images failed to download + return None start = timer() finalImg = genImage(imageArray) print(f"Image generated in: {timer() - start}s") @@ -126,6 +133,8 @@ def lambda_handler(event, context): if not img.startswith("https://pbs.twimg.com"): return {'statusCode':400,'body':'Invalid image URL'} combined = genImageFromURL(images) + if (combined == None): + return {'statusCode':400,'body':'Failed to download image(s)'} buffered = BytesIO() combined.save(buffered,format="JPEG",quality=60) combined_str=base64.b64encode(buffered.getvalue()).decode('ascii') From e66f5e890c1287b3320d1e8872a1365f4620a417 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 30 Jun 2023 21:25:41 +0100 Subject: [PATCH 6/7] Change failed download response code --- combineImg/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/combineImg/__init__.py b/combineImg/__init__.py index 1f5846a..f836831 100644 --- a/combineImg/__init__.py +++ b/combineImg/__init__.py @@ -134,7 +134,7 @@ def lambda_handler(event, context): return {'statusCode':400,'body':'Invalid image URL'} combined = genImageFromURL(images) if (combined == None): - return {'statusCode':400,'body':'Failed to download image(s)'} + return {'statusCode':500,'body':'Failed to download image(s)'} buffered = BytesIO() combined.save(buffered,format="JPEG",quality=60) combined_str=base64.b64encode(buffered.getvalue()).decode('ascii') From a8226279fc54686ea9fbcd426a1894bcf3d69d5c Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 1 Jul 2023 19:00:30 +0100 Subject: [PATCH 7/7] Thanks Elon! --- msgs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/msgs.py b/msgs.py index f860d08..a01c04a 100644 --- a/msgs.py +++ b/msgs.py @@ -1,4 +1,4 @@ -failedToScan="Failed to scan your link! This may be due to an incorrect link, private/suspended account, deleted tweet, or Twitter itself might be having issues (Check here: https://api.twitterstat.us/)" +failedToScan="Failed to scan your link! This may be due to an incorrect link, private/suspended account, deleted tweet, or recent changes to Twitter's API (Thanks, Elon!)." failedToScanExtra = "\n\nTwitter gave me this error: " tweetNotFound="Tweet not found." tweetSuspended="This Tweet is from a suspended account."