-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
188 lines (157 loc) · 6.16 KB
/
main.py
File metadata and controls
188 lines (157 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import sys
import platform
import aiohttp
import asyncio
import random
import argparse
from aiohttp import ClientSession
from urllib.parse import quote
from tqdm.asyncio import tqdm_asyncio
MAX_CONNECTIONS = 5
RETRY_LIMIT = 5
DOWNLOAD_CHUNK_SIZE = 100
PAGE_SIZE = 100 # Number of posts per API request
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0"
}
def parse_tags_input(raw_tags: str) -> str:
tags = raw_tags.strip().split()
encoded_tags = []
for tag in tags:
if tag.startswith('-') and len(tag) > 1:
encoded_tags.append('-' + quote(tag[1:]))
else:
encoded_tags.append(quote(tag))
return "+".join(encoded_tags)
async def fetch_with_retries(session, url, retries=RETRY_LIMIT):
for attempt in range(1, retries + 1):
try:
async with session.get(url, headers=HEADERS) as resp:
if resp.status in (429, 503):
print(f"Error {resp.status}, retry {attempt}")
await asyncio.sleep(5 * attempt)
continue
if resp.status != 200:
print(f"Error {resp.status}, retry {attempt}")
await asyncio.sleep(2)
continue
return await resp.json()
except Exception as e:
print(f"Error {e}, retry {attempt}")
await asyncio.sleep(2)
return None
async def download_file(session: ClientSession, url: str, filepath: str):
for attempt in range(1, RETRY_LIMIT + 1):
try:
await asyncio.sleep(random.uniform(1.5, 3.0))
async with session.get(url, headers=HEADERS) as resp:
if resp.status in (429, 503):
print(f"Error {resp.status}, retry {attempt}")
await asyncio.sleep(5 * attempt)
continue
if resp.status != 200:
print(f"Error {resp.status}, retry {attempt}")
await asyncio.sleep(2)
continue
with open(filepath, "wb") as f:
while True:
chunk = await resp.content.read(1024)
if not chunk:
break
f.write(chunk)
return
except Exception as e:
print(f"Failed to download {url}: {e}, retry {attempt}")
await asyncio.sleep(2)
async def download_all(posts, folder):
# Попытаться создать папку, при отказе — в домашней директории
try:
os.makedirs(folder, exist_ok=True)
except PermissionError:
print(f"⚠️ Не удалось создать папку «{folder}» — отказано в доступе.")
home = os.path.expanduser("~")
fallback = os.path.join(home, os.path.basename(folder))
print(f"👉 Будем сохранять в «{fallback}».")
folder = fallback
os.makedirs(folder, exist_ok=True)
sem = asyncio.Semaphore(MAX_CONNECTIONS)
async with aiohttp.ClientSession() as session:
async def bound_download(post):
file_url = post.get("file_url")
if not file_url:
return
filename = file_url.split("/")[-1]
filepath = os.path.join(folder, filename)
if os.path.exists(filepath):
return
async with sem:
await download_file(session, file_url, filepath)
await tqdm_asyncio.gather(
*(bound_download(post) for post in posts),
desc="Downloading",
total=len(posts),
leave=True
)
async def fetch_posts(tags: str):
print("Getting post list")
all_posts = []
page = 0
async with aiohttp.ClientSession() as session:
while True:
await asyncio.sleep(random.uniform(1.5, 3.0))
url = (
f"https://rule34.xxx/index.php?page=dapi&s=post&q=index&json=1"
f"&tags={tags}&pid={page}&limit={PAGE_SIZE}"
)
data = await fetch_with_retries(session, url)
if not data:
break
all_posts.extend(data)
print(f" Retrieved {len(data)} posts (page {page})")
if len(data) < PAGE_SIZE:
break
page += 1
print(f"Found {len(all_posts)} posts total")
return all_posts
def default_downloads_dir() -> str:
home = os.path.expanduser("~")
system = platform.system().lower()
if "windows" in system:
return os.path.join(os.environ.get("USERPROFILE", home), "Downloads")
else:
return os.path.join(home, "Downloads")
async def main():
parser = argparse.ArgumentParser(description="Rule34 Downloader")
parser.add_argument(
"-o", "--output-dir",
help="Куда сохранять скачанные файлы (по умолчанию — папка «Downloads»).",
default=default_downloads_dir()
)
args = parser.parse_args()
raw_tags = input("Any tags? (example: Miyabi -ai_generated): ")
tag_folder_name = raw_tags.replace(" ", "_")
tag_string = parse_tags_input(raw_tags)
base_dir = os.path.abspath(args.output_dir)
os.makedirs(base_dir, exist_ok=True)
download_folder = os.path.join(base_dir, tag_folder_name)
posts = await fetch_posts(tag_string)
if not posts:
print("Nothing found.")
return
confirm = input(f"Start download {len(posts)} posts? (y/n): ").strip().lower()
if confirm != 'y':
print("❌ Cancelled by user.")
return
count_input = input("How many posts to download? (0 or Enter = all): ").strip()
count = int(count_input) if count_input else 0
if count > 0:
posts = posts[:count]
print("Starting download...\n")
for i in range(0, len(posts), DOWNLOAD_CHUNK_SIZE):
chunk = posts[i:i + DOWNLOAD_CHUNK_SIZE]
await download_all(chunk, download_folder)
await asyncio.sleep(random.uniform(10, 20))
print("\n✅ Download complete!")
if __name__ == "__main__":
asyncio.run(main())