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): | ||||
|             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.") | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user