From fd96708790220eaa8e4833697e12db045317f9e4 Mon Sep 17 00:00:00 2001 From: likhy Date: Sat, 2 Sep 2023 11:17:20 -0700 Subject: [PATCH] does not save file if it already exists --- microblog.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/microblog.py b/microblog.py index 12ac17e..ecb5bc2 100644 --- a/microblog.py +++ b/microblog.py @@ -443,23 +443,25 @@ if __name__ == "__main__": def fn1a(profiles, save_path, img_src): import hashlib - avatar_urls = [] - for profile in profiles: - avatar_urls.append(profile["avatar"]) - imgs, info = fetch(avatar_urls) + imgs, info = fetch([p["avatar"] for p in profiles]) l = len(imgs) if l != len(profiles) or l == 0: print("error in retrieving images", file=sys.stderr) return - for i in range(0, l): - image = imgs[i] - ext = info[i].split('/').pop() - data = image.getvalue() + for i in range(0,l): + content_type = info[i].split('/') + ext = content_type.pop() + if content_type.pop() != "image": + print("\tskip: not an image", file=sys.stderr) + continue + data = imgs[i].getvalue() h = hashlib.sha1(data).hexdigest() filename = "%s.%s" % (h, ext) - with open("%s/%s" % (save_path, filename), "wb") as f: - f.write(data) profiles[i]["avatar"] = "%s/%s" % (img_src, filename) + asdf = "%s/%s" % (save_path, filename) + if not os.path.isfile(asdf): + with open(asdf, "wb") as f: + f.write(data) j, m = fetch(f_cfg["list"]) list_of_json_objs = to_json(j) @@ -536,4 +538,4 @@ if __name__ == "__main__": print("\n\tYour configuration file is malformed.") except FileNotFoundError as e: traceback.print_exc() - print("\n\tA potential cause is attempting to save a file to a folder that does not exist..") + print("\n\tA potential cause is attempting to save a file to a folder that does not exist.")