From d002b51930897729fbc77e1f5d02c74aa90038c4 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Fri, 13 May 2022 12:02:25 -0400 Subject: [PATCH] [bin] Properly overwrite dup images --- bin/.bin/get_bing_potd.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/bin/.bin/get_bing_potd.py b/bin/.bin/get_bing_potd.py index 5648bb5..d03220e 100755 --- a/bin/.bin/get_bing_potd.py +++ b/bin/.bin/get_bing_potd.py @@ -1,18 +1,33 @@ #!/usr/bin/env python3 import os import subprocess -from datetime import datetime +from datetime import datetime, timedelta +from PIL import Image +import imagehash import requests + today = datetime.today().strftime("%Y-%m-%d") +yesterday = (datetime.today() - timedelta(days=1)).strftime("%Y-%m-%d") home = os.path.expanduser("~") +check_path = f"{home}/var/media/backgrounds/bing/{yesterday}.jpg" target_path = f"{home}/var/media/backgrounds/bing/{today}.jpg" # If the file for today already exists, just exit if os.path.isfile(target_path): - print(f"Bing image for {today} already exists, skipping download") - exit() + + yesterday_img = imagehash.average_hash(Image.open(check_path)) + today_img = imagehash.average_hash(Image.open(target_path)) + cutoff = 5 # maximum bits that could be different between the hashes. + + found_match = today_img - yesterday_img < cutoff + + if not found_match: + print(f"Bing image for {today} already exists, skipping download") + exit() + + print(f"Bing image for {today} same as yesterday, overwriting") iotd_uri = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=1&n=1" r = requests.get(iotd_uri)