1- from requests . auth import HTTPBasicAuth
2- import requests
1+ import os
2+ import json
33import random
4+ import requests
5+ from requests .auth import HTTPBasicAuth
46from termcolor import colored
57from alive_progress import alive_bar
68from time import sleep
79from datetime import datetime
8- import os
9- import json
1010
1111from main import unsafe_chars
12- now = datetime .now ()
13- dt_now = now .strftime ("%d-%m-%Y_%H-%M-%S" )
1412
15- class E6System ():
16- def Fetcher (user_tags , user_blacklist , proxy_list , max_sites , user_proxies , apiUser ,apiKey , header , db , site , ai_training ):
13+ class E6System :
14+ @staticmethod
15+ def fetcher (user_tags , user_blacklist , proxy_list , max_sites , user_proxies , api_user , api_key , header , db , site , ai_training ):
1716 try :
1817 approved_list = []
18+ now = datetime .now ()
19+ dt_now = now .strftime ("%d-%m-%Y_%H-%M-%S" )
1920 page = 1
21+
2022 while True :
2123 URL = f"https://{ site } .net/posts.json?tags={ user_tags } &limit=320&page={ page } "
22- if user_proxies == True :
23- proxy = random .choice (proxy_list )
24- raw_req = requests .get (URL , headers = header , proxies = proxy , auth = HTTPBasicAuth (apiUser , apiKey ))
25- else :
26- raw_req = requests .get (URL , headers = header , auth = HTTPBasicAuth (apiUser , apiKey ))
27-
24+ proxy = random .choice (proxy_list ) if user_proxies else None
25+ raw_req = requests .get (URL , headers = header , proxies = proxy , auth = HTTPBasicAuth (api_user , api_key ))
2826 req = raw_req .json ()
29-
30- try :
31- if req ["message" ] == "You cannot go beyond page 750. Please narrow your search terms." :
32- print (colored (req ["message" ] + " (API limit)" , "red" ))
33- sleep (5 )
34- break
35- except :
36- pass
3727
38- if req ["posts" ] == []:
28+ if "message" in req and req ["message" ] == "You cannot go beyond page 750. Please narrow your search terms." :
29+ print (colored (req ["message" ] + " (API limit)" , "red" ))
30+ sleep (5 )
31+ break
32+
33+ if not req ["posts" ]:
3934 print (colored ("No images found or all downloaded! Try different tags." , "yellow" ))
4035 sleep (5 )
4136 break
42-
37+
4338 elif page == max_sites :
4439 print (colored (f"Finished Downloading { max_sites } of { max_sites } pages." , "yellow" ))
4540 sleep (5 )
4641 break
4742
48- else :
43+ else :
4944 for item in req ["posts" ]:
5045 image_id = item ["id" ]
51- image_address = item ["file" ]["url" ]
52- post_tags1 = item ["tags" ]["general" ]
53- post_tags2 = item ["tags" ]["species" ]
54- post_tags3 = item ["tags" ]["character" ]
55- if site == "e6ai" :
56- post_tags4 = item ["tags" ]["director" ]
57- post_tags5 = item ["tags" ]["meta" ]
58- else :
59- post_tags4 = item ["tags" ]["copyright" ]
60- post_tags5 = item ["tags" ]["artist" ]
61-
62- if ai_training == True :
63- meta_tags = item ["tags" ]
64- else :
65- meta_tags = []
46+ image_address = item ["file" ].get ("url" )
47+ meta_tags = item ["tags" ] if ai_training else []
48+ post_tags = [item ["tags" ][tag_type ] for tag_type in ["general" , "species" , "character" ]]
49+ post_tags += [item ["tags" ]["director" ], item ["tags" ]["meta" ]] if site == "e6ai" else [item ["tags" ]["copyright" ], item ["tags" ]["artist" ]]
50+ post_tags = sum (post_tags , [])
51+ user_blacklist_length = len (user_blacklist )
52+
53+ passed = sum (blacklisted_tag in post_tags for blacklisted_tag in user_blacklist )
6654
67- post_tags = post_tags1 + post_tags2 + post_tags3 + post_tags4 + post_tags5
68- image_format = item ["file" ]["ext" ]
69- user_blacklist_lenght = len (user_blacklist )
70- passed = 0
55+ if passed == 0 and not db and image_address and not any (tag in user_blacklist for tag in post_tags ):
56+ image_data = {"image_address" : image_address , "image_format" : item ["file" ]["ext" ], "image_id" : image_id , "meta_tags" : meta_tags }
57+ approved_list .append (image_data )
7158
72- for blacklisted_tag in user_blacklist :
73- if blacklisted_tag in post_tags :
74- break
75- else :
76- passed += 1
77- if passed == user_blacklist_lenght and str (image_id ) not in db and image_address != None :
78- image_data = {"image_address" : image_address , "image_format" : image_format , "image_id" : image_id , "meta_tags" : meta_tags }
59+ elif db and str (image_id ) not in db and image_address and not any (tag in user_blacklist for tag in post_tags ):
60+ image_data = {"image_address" : image_address , "image_format" : item ["file" ]["ext" ], "image_id" : image_id , "meta_tags" : meta_tags }
7961 approved_list .append (image_data )
80- else :
81- pass
8262
83- # Download Each file
8463 with alive_bar (len (approved_list ), calibrate = 1 , dual_line = True , title = 'Downloading' ) as bar :
8564 for data in approved_list :
86- image_address = data [ "image_address" ]
87- image_format = data [ "image_format" ]
88- image_id = data [ "image_id" ]
89- meta_tags = data [ "meta_tags" ]
65+ image_address = data . get ( "image_address" )
66+ image_format = data . get ( "image_format" )
67+ image_id = data . get ( "image_id" )
68+ meta_tags = data . get ( "meta_tags" )
9069 bar .text = f'-> Downloading: { image_id } , please wait...'
91- if user_proxies == True :
92- proxy = random .choice (proxy_list )
93- img_data = requests .get (image_address , proxies = proxy ).content
94- else :
95- sleep (1 )
96- img_data = requests .get (image_address ).content
97-
98- safe_user_tags = user_tags .replace (" " , "_" )
99- for char in unsafe_chars :
100- safe_user_tags = safe_user_tags .replace (char , "" )
10170
102- if not os . path . exists ( f"media/ { dt_now } _ { safe_user_tags } " ):
103- os . mkdir ( f"media/ { dt_now } _ { safe_user_tags } " )
71+ proxy = random . choice ( proxy_list ) if user_proxies else None
72+ img_data = requests . get ( image_address , proxies = proxy ). content if user_proxies else requests . get ( image_address ). content
10473
105- if not os .path .exists (f"media/{ dt_now } _{ safe_user_tags } /meta" ) and ai_training == True :
106- os .mkdir (f"media/{ dt_now } _{ safe_user_tags } /meta" )
74+ safe_user_tags = "" .join (char for char in user_tags if char not in unsafe_chars ).replace (" " , "_" )
75+ directory = f"media/{ dt_now } _{ safe_user_tags } "
76+ meta_directory = f"{ directory } /meta"
10777
108- with open (f"media/{ dt_now } _{ safe_user_tags } /{ str (image_id )} .{ image_format } " , 'wb' ) as handler :
109- handler .write (img_data )
78+ os .makedirs (directory , exist_ok = True )
11079
11180 if ai_training == True :
112- with open (f"media/{ dt_now } _{ safe_user_tags } /meta/{ str (image_id )} .json" , 'w' ) as handler :
81+ os .makedirs (meta_directory , exist_ok = True )
82+ with open (f"{ meta_directory } /{ str (image_id )} .json" , 'w' ) as handler :
11383 json .dump (meta_tags , handler , indent = 6 )
11484
115- with open (f"db/{ site } .db" , "a" ) as db_writer :
116- db_writer .write (f"{ str (image_id )} \n " )
85+ with open (f"{ directory } /{ str (image_id )} .{ image_format } " , 'wb' ) as handler :
86+ handler .write (img_data )
87+
88+ if db != False :
89+ with open (f"db/{ site } .db" , "a" ) as db_writer :
90+ db_writer .write (f"{ str (image_id )} \n " )
91+
11792 bar ()
11893
11994 print (colored (f"Page { page } Completed" , "green" ))
@@ -124,4 +99,4 @@ def Fetcher(user_tags, user_blacklist, proxy_list, max_sites, user_proxies, apiU
12499 return {"status" : "ok" }
125100
126101 except Exception as e :
127- return {"status" : "error" , "uinput" : user_tags , "exception" : str (e ), "extra" : raw_req .content }
102+ return {"status" : "error" , "uinput" : user_tags , "exception" : str (e ), "extra" : raw_req .content }
0 commit comments