v2ray commited on
Commit
1546b82
·
1 Parent(s): ef1ffa0

Fixed utils.

Browse files
scrape_gel.py CHANGED
@@ -22,7 +22,7 @@ async def process_link(thread_pool, session, image_url, image_ids_to_ignore, wid
22
  error = None
23
  for i in range(1, MAX_RETRY + 2): # 1 indexed.
24
  try:
25
- if utils.SIGINT_COUNTER >= 1:
26
  break
27
  # print(f"Processing image {image_id}...")
28
  async with session.get(image_url) as response:
@@ -114,7 +114,8 @@ async def main():
114
  page_number = 0
115
  search_tags = "+".join(urllib.parse.quote(tag, safe="") for tag in args.tags_to_search)
116
 
117
- image_ids_to_ignore = utils.get_existing_image_tags_set(IMAGE_DIR)
 
118
  utils.register_sigint_callback()
119
 
120
  async with aiohttp.ClientSession(cookies={"fringeBenefits": "yup"}, timeout=aiohttp.ClientTimeout(total=TIMEOUT)) as session:
@@ -122,7 +123,7 @@ async def main():
122
  tasks = []
123
  while True:
124
  try:
125
- if utils.SIGINT_COUNTER >= 1:
126
  break
127
  request_url = f"{args.site}/index.php?page=post&s=list&tags={search_tags}&pid={page_number}"
128
  print(f"Going to {request_url}")
@@ -140,10 +141,10 @@ async def main():
140
  print(f"Got {image_url_count} posts.")
141
  page_number += image_url_count
142
  for image_url in image_urls:
143
- if utils.SIGINT_COUNTER >= 1:
144
  break
145
  while len(tasks) >= MAX_TASKS:
146
- if utils.SIGINT_COUNTER >= 1:
147
  break
148
  await asyncio.sleep(0.1)
149
  for i in range(len(tasks) - 1, -1, -1):
@@ -155,18 +156,18 @@ async def main():
155
  except Exception as e:
156
  print(f"An error occurred: {e}\nPausing for 0.1 second before retrying...")
157
  await asyncio.sleep(0.1)
158
- if utils.SIGINT_COUNTER >= 1:
159
  print("Script interrupted by user, gracefully exiting...\nYou can interrupt again to exit semi-forcefully, but it will break image checks!")
160
  else:
161
  print("No more images to download, waiting already submitted tasks to finish...")
162
- while tasks and utils.SIGINT_COUNTER <= 1:
163
  await asyncio.sleep(0.1)
164
  for i in range(len(tasks) - 1, -1, -1):
165
  task = tasks[i]
166
  if task.done():
167
  await task
168
  del tasks[i]
169
- if utils.SIGINT_COUNTER >= 2:
170
  print("Another interrupt received, exiting semi-forcefully...\nYou can interrupt again for truly forceful exit, but it most likely will break a lot of things!")
171
  sys.exit(1)
172
 
 
22
  error = None
23
  for i in range(1, MAX_RETRY + 2): # 1 indexed.
24
  try:
25
+ if utils.get_sigint_count() >= 1:
26
  break
27
  # print(f"Processing image {image_id}...")
28
  async with session.get(image_url) as response:
 
114
  page_number = 0
115
  search_tags = "+".join(urllib.parse.quote(tag, safe="") for tag in args.tags_to_search)
116
 
117
+ os.makedirs(IMAGE_DIR, exist_ok=True)
118
+ image_ids_to_ignore = utils.get_existing_image_id_set(IMAGE_DIR)
119
  utils.register_sigint_callback()
120
 
121
  async with aiohttp.ClientSession(cookies={"fringeBenefits": "yup"}, timeout=aiohttp.ClientTimeout(total=TIMEOUT)) as session:
 
123
  tasks = []
124
  while True:
125
  try:
126
+ if utils.get_sigint_count() >= 1:
127
  break
128
  request_url = f"{args.site}/index.php?page=post&s=list&tags={search_tags}&pid={page_number}"
129
  print(f"Going to {request_url}")
 
141
  print(f"Got {image_url_count} posts.")
142
  page_number += image_url_count
143
  for image_url in image_urls:
144
+ if utils.get_sigint_count() >= 1:
145
  break
146
  while len(tasks) >= MAX_TASKS:
147
+ if utils.get_sigint_count() >= 1:
148
  break
149
  await asyncio.sleep(0.1)
150
  for i in range(len(tasks) - 1, -1, -1):
 
156
  except Exception as e:
157
  print(f"An error occurred: {e}\nPausing for 0.1 second before retrying...")
158
  await asyncio.sleep(0.1)
159
+ if utils.get_sigint_count() >= 1:
160
  print("Script interrupted by user, gracefully exiting...\nYou can interrupt again to exit semi-forcefully, but it will break image checks!")
161
  else:
162
  print("No more images to download, waiting already submitted tasks to finish...")
163
+ while tasks and utils.get_sigint_count() <= 1:
164
  await asyncio.sleep(0.1)
165
  for i in range(len(tasks) - 1, -1, -1):
166
  task = tasks[i]
167
  if task.done():
168
  await task
169
  del tasks[i]
170
+ if utils.get_sigint_count() >= 2:
171
  print("Another interrupt received, exiting semi-forcefully...\nYou can interrupt again for truly forceful exit, but it most likely will break a lot of things!")
172
  sys.exit(1)
173
 
scrape_yan.py CHANGED
@@ -21,7 +21,7 @@ async def process_link(thread_pool, session, image_object, image_ids_to_ignore,
21
  error = None
22
  for i in range(1, MAX_RETRY + 2): # 1 indexed.
23
  try:
24
- if utils.SIGINT_COUNTER >= 1:
25
  break
26
  # print(f"Processing image {image_id}...")
27
  if not use_low_quality:
@@ -107,7 +107,8 @@ async def main():
107
  page_number = 1
108
  search_tags = "+".join(urllib.parse.quote(tag, safe="") for tag in args.tags_to_search)
109
 
110
- image_ids_to_ignore = utils.get_existing_image_tags_set(IMAGE_DIR)
 
111
  utils.register_sigint_callback()
112
 
113
  async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=TIMEOUT)) as session:
@@ -115,7 +116,7 @@ async def main():
115
  tasks = []
116
  while True:
117
  try:
118
- if utils.SIGINT_COUNTER >= 1:
119
  break
120
  request_url = f"{args.site}/post.json?limit=1000&tags={search_tags}&page={page_number}"
121
  print(f"Going to {request_url}")
@@ -128,10 +129,10 @@ async def main():
128
  print(f"Got {image_count} posts.")
129
  page_number += 1
130
  for image_object in image_objects:
131
- if utils.SIGINT_COUNTER >= 1:
132
  break
133
  while len(tasks) >= MAX_TASKS:
134
- if utils.SIGINT_COUNTER >= 1:
135
  break
136
  await asyncio.sleep(0.1)
137
  for i in range(len(tasks) - 1, -1, -1):
@@ -143,18 +144,18 @@ async def main():
143
  except Exception as e:
144
  print(f"An error occurred: {e}\nPausing for 0.1 second before retrying...")
145
  await asyncio.sleep(0.1)
146
- if utils.SIGINT_COUNTER >= 1:
147
  print("Script interrupted by user, gracefully exiting...\nYou can interrupt again to exit semi-forcefully, but it will break image checks!")
148
  else:
149
  print("No more images to download, waiting already submitted tasks to finish...")
150
- while tasks and utils.SIGINT_COUNTER <= 1:
151
  await asyncio.sleep(0.1)
152
  for i in range(len(tasks) - 1, -1, -1):
153
  task = tasks[i]
154
  if task.done():
155
  await task
156
  del tasks[i]
157
- if utils.SIGINT_COUNTER >= 2:
158
  print("Another interrupt received, exiting semi-forcefully...\nYou can interrupt again for truly forceful exit, but it most likely will break a lot of things!")
159
  sys.exit(1)
160
 
 
21
  error = None
22
  for i in range(1, MAX_RETRY + 2): # 1 indexed.
23
  try:
24
+ if utils.get_sigint_count() >= 1:
25
  break
26
  # print(f"Processing image {image_id}...")
27
  if not use_low_quality:
 
107
  page_number = 1
108
  search_tags = "+".join(urllib.parse.quote(tag, safe="") for tag in args.tags_to_search)
109
 
110
+ os.makedirs(IMAGE_DIR, exist_ok=True)
111
+ image_ids_to_ignore = utils.get_existing_image_id_set(IMAGE_DIR)
112
  utils.register_sigint_callback()
113
 
114
  async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=TIMEOUT)) as session:
 
116
  tasks = []
117
  while True:
118
  try:
119
+ if utils.get_sigint_count() >= 1:
120
  break
121
  request_url = f"{args.site}/post.json?limit=1000&tags={search_tags}&page={page_number}"
122
  print(f"Going to {request_url}")
 
129
  print(f"Got {image_count} posts.")
130
  page_number += 1
131
  for image_object in image_objects:
132
+ if utils.get_sigint_count() >= 1:
133
  break
134
  while len(tasks) >= MAX_TASKS:
135
+ if utils.get_sigint_count() >= 1:
136
  break
137
  await asyncio.sleep(0.1)
138
  for i in range(len(tasks) - 1, -1, -1):
 
144
  except Exception as e:
145
  print(f"An error occurred: {e}\nPausing for 0.1 second before retrying...")
146
  await asyncio.sleep(0.1)
147
+ if utils.get_sigint_count() >= 1:
148
  print("Script interrupted by user, gracefully exiting...\nYou can interrupt again to exit semi-forcefully, but it will break image checks!")
149
  else:
150
  print("No more images to download, waiting already submitted tasks to finish...")
151
+ while tasks and utils.get_sigint_count() <= 1:
152
  await asyncio.sleep(0.1)
153
  for i in range(len(tasks) - 1, -1, -1):
154
  task = tasks[i]
155
  if task.done():
156
  await task
157
  del tasks[i]
158
+ if utils.get_sigint_count() >= 2:
159
  print("Another interrupt received, exiting semi-forcefully...\nYou can interrupt again for truly forceful exit, but it most likely will break a lot of things!")
160
  sys.exit(1)
161
 
utils/__init__.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from .utils import *
2
+ from .scrape_state import *
3
+ from .sigint_handler import *
utils/scrape_state.py ADDED
File without changes
utils/sigint_handler.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import signal
3
+
4
+ _SIGINT_COUNTER = 0
5
+
6
+ def get_sigint_count():
7
+ return _SIGINT_COUNTER
8
+
9
+ def sigint_handler(signum, frame):
10
+ global _SIGINT_COUNTER
11
+ _SIGINT_COUNTER += 1
12
+ print()
13
+ if _SIGINT_COUNTER >= 3:
14
+ print("Script force quit by user, exiting...")
15
+ sys.exit(1)
16
+
17
+ def register_sigint_callback():
18
+ signal.signal(signal.SIGINT, sigint_handler)
utils.py → utils/utils.py RENAMED
@@ -1,22 +1,7 @@
1
  import os
2
- import sys
3
- import signal
4
  import asyncio
5
  from PIL import Image
6
 
7
- SIGINT_COUNTER = 0
8
-
9
- def sigint_handler(signum, frame):
10
- global SIGINT_COUNTER
11
- SIGINT_COUNTER += 1
12
- print()
13
- if SIGINT_COUNTER >= 3:
14
- print("Script force quit by user, exiting...")
15
- sys.exit(1)
16
-
17
- def register_sigint_callback():
18
- signal.signal(signal.SIGINT, sigint_handler)
19
-
20
  def validate_image(image_path, tags_path, width=None, height=None, convert_to_avif=False):
21
  new_path = None
22
  try:
@@ -76,5 +61,5 @@ def get_image_id_image_tags_path_tuple_dict(image_dir):
76
  image_id_image_tags_path_tuple_dict[image_id] = (path, tags_path)
77
  return image_id_image_tags_path_tuple_dict
78
 
79
- def get_existing_image_tags_set(image_dir):
80
- return {tags_path for _, tags_path in get_image_id_image_tags_path_tuple_dict(image_dir)}
 
1
  import os
 
 
2
  import asyncio
3
  from PIL import Image
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def validate_image(image_path, tags_path, width=None, height=None, convert_to_avif=False):
6
  new_path = None
7
  try:
 
61
  image_id_image_tags_path_tuple_dict[image_id] = (path, tags_path)
62
  return image_id_image_tags_path_tuple_dict
63
 
64
+ def get_existing_image_id_set(image_dir):
65
+ return set(get_image_id_image_tags_path_tuple_dict(image_dir))