[bin] Properly overwrite dup images
This commit is contained in:
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user