ソースを参照

[bin] Properly overwrite dup images

Colin Powell 3 年 前
コミット
d002b51930
1 ファイル変更18 行追加3 行削除
  1. 18 3
      bin/.bin/get_bing_potd.py

+ 18 - 3
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)