does not save file if it already exists
This commit is contained in:
parent
0bccc33730
commit
fd96708790
24
microblog.py
24
microblog.py
@ -443,23 +443,25 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
def fn1a(profiles, save_path, img_src):
|
def fn1a(profiles, save_path, img_src):
|
||||||
import hashlib
|
import hashlib
|
||||||
avatar_urls = []
|
imgs, info = fetch([p["avatar"] for p in profiles])
|
||||||
for profile in profiles:
|
|
||||||
avatar_urls.append(profile["avatar"])
|
|
||||||
imgs, info = fetch(avatar_urls)
|
|
||||||
l = len(imgs)
|
l = len(imgs)
|
||||||
if l != len(profiles) or l == 0:
|
if l != len(profiles) or l == 0:
|
||||||
print("error in retrieving images", file=sys.stderr)
|
print("error in retrieving images", file=sys.stderr)
|
||||||
return
|
return
|
||||||
for i in range(0, l):
|
for i in range(0,l):
|
||||||
image = imgs[i]
|
content_type = info[i].split('/')
|
||||||
ext = info[i].split('/').pop()
|
ext = content_type.pop()
|
||||||
data = image.getvalue()
|
if content_type.pop() != "image":
|
||||||
|
print("\tskip: not an image", file=sys.stderr)
|
||||||
|
continue
|
||||||
|
data = imgs[i].getvalue()
|
||||||
h = hashlib.sha1(data).hexdigest()
|
h = hashlib.sha1(data).hexdigest()
|
||||||
filename = "%s.%s" % (h, ext)
|
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)
|
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"])
|
j, m = fetch(f_cfg["list"])
|
||||||
list_of_json_objs = to_json(j)
|
list_of_json_objs = to_json(j)
|
||||||
@ -536,4 +538,4 @@ if __name__ == "__main__":
|
|||||||
print("\n\tYour configuration file is malformed.")
|
print("\n\tYour configuration file is malformed.")
|
||||||
except FileNotFoundError as e:
|
except FileNotFoundError as e:
|
||||||
traceback.print_exc()
|
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.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user