Skip to content

Commit ecd7954

Browse files
improvements and bug fixes
fix: #19 fix: #20 refactor: add comments in furbooru.py
2 parents 3797ed0 + 7e69fda commit ecd7954

File tree

6 files changed

+180
-189
lines changed

6 files changed

+180
-189
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ Welcome to the successor of the [multporn image downloader v1][2] & [v2][1] and
99

1010
<br />
1111

12+
#### Features:
13+
- Portable
14+
- Proxy Support
15+
- AI Training Compatible
16+
- Avoid Duplicates
17+
18+
<br />
19+
1220
#### Currently Supported:
1321
- [Rule34][3] (API)
1422
- [E621][4] (API)
@@ -46,7 +54,6 @@ Welcome to the successor of the [multporn image downloader v1][2] & [v2][1] and
4654

4755
Further sites can be added. Just open a [support ticket][11] with the URL to the site.
4856

49-
<br />
5057
<br />
5158
<br />
5259

main.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77
import inquirer
88

9-
version = "1.6.1"
9+
version = "1.6.2"
1010

1111
if os.name == 'nt':
1212
from ctypes import windll
@@ -15,7 +15,7 @@
1515
proxy_list = []
1616
header = {"User-Agent":f"nn-downloader/{version} (by Official Husko on GitHub)"}
1717
needed_folders = ["db", "media"]
18-
database_list = ["e621", "furbooru", "rule34", "e6ai", "e926"]
18+
database_list = ["e621", "e6ai", "e926", "furbooru", "rule34"]
1919
unsafe_chars = ["/", "\\", ":", "*", "?", "\"", "<", ">", "|", "\0", "$", "#", "@", "&", "%", "!", "`", "^", "(", ")", "{", "}", "[", "]", "=", "+", "~", ",", ";"]
2020

2121
if sys.gettrace() is not None:
@@ -107,7 +107,7 @@ def clear_screen():
107107

108108
site = answers.get("selection").lower()
109109

110-
if site in ["e621", "e6ai", "e926"]:
110+
if site in ["e621", "e6ai", "e926", "furbooru", "rule34"]:
111111

112112
print(colored("Please enter the tags you want to use.", "green"))
113113
user_tags = input(">> ").lower()
@@ -121,33 +121,41 @@ def clear_screen():
121121
max_sites = input(">> ").lower()
122122
print("")
123123

124-
apiUser = config["user_credentials"][site]["apiUser"]
125-
apiKey = config["user_credentials"][site]["apiKey"]
124+
if site in ["e621", "e6ai", "e926"]:
125+
api_user = config.get("user_credentials",{}).get(site, {}).get("apiUser", "")
126+
api_key = config.get("user_credentials", {}).get(site, {}).get("apiKey", "")
126127
if oneTimeDownload == True:
127128
with open(f"db/{site}.db", "r") as db_reader:
128129
database = db_reader.read().splitlines()
129-
if apiKey == "" or apiUser == "":
130-
print(colored("Please add your Api Key into the config.json", "red"))
131-
sleep(5)
132130
else:
133-
output = E6System.Fetcher(user_tags=user_tags, user_blacklist=config["blacklisted_tags"], proxy_list=proxy_list, max_sites=max_sites, user_proxies=config["proxies"], apiUser=apiUser, apiKey=apiKey, header=header, db=database, site=site, ai_training=ai_training)
134-
131+
database = False
132+
if api_key == "" or api_user == "":
133+
print(colored("Please add your API Key into the config.json", "red"))
134+
sleep(10)
135+
sys.exit(0)
136+
else:
137+
output = E6System.fetcher(user_tags=user_tags, user_blacklist=config["blacklisted_tags"], proxy_list=proxy_list, max_sites=max_sites, user_proxies=config["proxies"], api_user=api_user, api_key=api_key, header=header, db=database, site=site, ai_training=ai_training)
138+
135139
elif site == "rule34":
136140
if oneTimeDownload == True:
137141
with open("db/rule34.db", "r") as db_reader:
138142
database = db_reader.read().splitlines()
139-
output = RULE34.Fetcher(user_tags=user_tags, user_blacklist=config["blacklisted_tags"], proxy_list=proxy_list, max_sites=max_sites, user_proxies=config["proxies"], header=header, db=database)
143+
else:
144+
database = False
145+
output = RULE34.fetcher(user_tags=user_tags, user_blacklist=config["blacklisted_tags"], proxy_list=proxy_list, max_sites=max_sites, user_proxies=config["proxies"], header=header, db=database)
140146

141147
elif site == "furbooru":
142-
apiKey = config["user_credentials"]["furbooru"]["apiKey"]
148+
api_key = config.get("user_credentials", {}).get(site, {}).get("apiKey", "")
143149
if oneTimeDownload == True:
144150
with open("db/furbooru.db", "r") as db_reader:
145151
database = db_reader.read().splitlines()
146-
if apiKey == "":
147-
print(colored("Please add your Api Key into the config.json", "red"))
152+
else:
153+
database = False
154+
if api_key == "":
155+
print(colored("Please add your API Key into the config.json", "red"))
148156
sleep(5)
149157
else:
150-
output = FURBOORU.Fetcher(user_tags=user_tags, user_blacklist=config["blacklisted_tags"], proxy_list=proxy_list, max_sites=max_sites, user_proxies=config["proxies"], apiKey=apiKey, header=header, db=database)
158+
output = FURBOORU.fetcher(user_tags=user_tags, user_blacklist=config["blacklisted_tags"], proxy_list=proxy_list, max_sites=max_sites, user_proxies=config["proxies"], api_key=api_key, header=header, db=database)
151159

152160
elif site == "multporn":
153161
print(colored("Please enter the link. (e.g. https://multporn.net/comics/double_trouble_18)", "green"))

modules/e6systems.py

Lines changed: 53 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,94 @@
1-
from requests.auth import HTTPBasicAuth
2-
import requests
1+
import os
2+
import json
33
import random
4+
import requests
5+
from requests.auth import HTTPBasicAuth
46
from termcolor import colored
57
from alive_progress import alive_bar
68
from time import sleep
79
from datetime import datetime
8-
import os
9-
import json
1010

1111
from 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

Comments
 (0)