File size: 8,822 Bytes
20ea893 13b2261 20ea893 35fbdd7 13b2261 20ea893 13b2261 20ea893 13b2261 4626454 20ea893 13b2261 20ea893 13b2261 20ea893 13b2261 20ea893 35fbdd7 13b2261 20ea893 a9c180f 35fbdd7 13b2261 20ea893 4626454 20ea893 13b2261 4626454 20ea893 13b2261 20ea893 13b2261 20ea893 13b2261 20ea893 4626454 20ea893 13b2261 20ea893 13b2261 20ea893 13b2261 20ea893 13b2261 20ea893 13b2261 20ea893 13b2261 20ea893 13b2261 20ea893 |
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 |
import os
import re
import sys
import utils
import random
import urllib
import asyncio
import aiohttp
import aiofiles
import argparse
import concurrent
import html as libhtml
from constants import *
from bs4 import BeautifulSoup
async def process_link(thread_pool, session, image_url, image_ids_to_ignore, width, height, convert_to_avif, use_low_quality, min_tags):
image_id = re.search("id=(\d+)", image_url).group(1)
if image_id in image_ids_to_ignore:
# print(f"Image {image_id} already exists, skipped.")
return
image_ids_to_ignore.add(image_id)
error = None
for i in range(1, MAX_RETRY + 2): # 1 indexed.
try:
if utils.SIGINT_COUNTER >= 1:
break
# print(f"Processing image {image_id}...")
async with session.get(image_url) as response:
html = await response.text()
soup = BeautifulSoup(html, "html.parser")
image_container = soup.find("section", class_=["image-container", "note-container"])
if not image_container:
raise RuntimeError(f"No image container found for {image_id}.")
if not use_low_quality:
image_download_url = soup.find("a", string="Original image")["href"]
else:
image_download_url = image_container.find("img", id="image")["src"]
image_ext = os.path.splitext(image_download_url)[1].lower()
if not image_ext:
print(f"Image {image_id} has no file extension, skipped.")
return
if image_ext not in IMAGE_EXT:
print(f"Image {image_id} is not an image, skipped.")
return
tags = image_container["data-tags"].strip().split()
tag_count = len(tags)
if tag_count < min_tags:
# print(f"Image {image_id} doesn't have enough tags({tag_count} < {min_tags}), skipped.")
return
tags.append("nsfw" if image_container["data-rating"] in {"explicit", "questionable"} else "sfw")
random.shuffle(tags)
image_path = os.path.join(IMAGE_DIR, image_id + image_ext)
tags_path = os.path.join(IMAGE_DIR, image_id + ".txt")
tags_text = ", ".join(libhtml.unescape(tag).replace("_", " ") for tag in tags)
async with session.get(image_download_url) as img_response:
img_data = await img_response.read()
os.makedirs(IMAGE_DIR, exist_ok=True)
async with aiofiles.open(image_path, "wb") as f:
await f.write(img_data)
async with aiofiles.open(tags_path, "w", encoding="utf8") as f:
await f.write(tags_text)
if not await utils.submit_validation(thread_pool, image_path, tags_path, width, height, convert_to_avif):
image_ids_to_ignore.remove(image_id)
return
except Exception as e:
error = e
if i > MAX_RETRY:
break
# print(f"A {e.__class__.__name__} occurred with image {image_id}: {e}\nPausing for 0.1 second before retrying attempt {i}/{MAX_RETRY}...")
await asyncio.sleep(0.1)
image_ids_to_ignore.remove(image_id)
print(f"All retry attempts failed, image {image_id} skipped. Final error {error.__class__.__name__}: {error}")
def parse_args():
parser = argparse.ArgumentParser(description="Scrape images from Gelbooru.")
parser.add_argument("-s", "--site", default="https://gelbooru.com", help="Domain to scrape from, defaults to https://gelbooru.com")
parser.add_argument("-W", "--width", type=int, help="Scale the width of the image to the specified value, must either provide both width and height or not provide both")
parser.add_argument("-H", "--height", type=int, help="Scale the height of the image to the specified value, must either provide both width and height or not provide both")
parser.add_argument("-a", "--avif", action="store_true", help="If set, will convert the image into avif, need to have pillow-avif-plugin installed")
parser.add_argument("-l", "--low-quality", action="store_true", help="If set, will download the sample instead of the original image")
parser.add_argument("-t", "--min-tags", type=int, default=0, help="Filter out images with less than the specified amount of tags, default to 0")
parser.add_argument("tags_to_search", nargs=argparse.REMAINDER, help="List of tags to search for, defaults to all")
args = parser.parse_args()
if args.width is None or args.height is None:
if args.width is not None or args.height is not None:
print("You must either provide both width and height or not provide both at the same time!")
sys.exit(1)
else:
if args.width < 1:
print("Width must be greater than or equal to 1!")
sys.exit(1)
if args.height < 1:
print("Height must be greater than or equal to 1!")
sys.exit(1)
if args.avif:
try:
import pillow_avif
except ImportError:
print("You need to pip install pillow-avif-plugin to use avif conversion!")
sys.exit(1)
if args.min_tags < 0:
print("Min tags must be greater than or equal to 0!")
sys.exit(1)
if not args.tags_to_search:
args.tags_to_search = ["all"]
return args
async def main():
args = parse_args()
print("Starting...")
page_number = 0
search_tags = "+".join(urllib.parse.quote(tag, safe="") for tag in args.tags_to_search)
image_ids_to_ignore = utils.get_existing_image_tags_set(IMAGE_DIR)
utils.register_sigint_callback()
async with aiohttp.ClientSession(cookies={"fringeBenefits": "yup"}, timeout=aiohttp.ClientTimeout(total=TIMEOUT)) as session:
thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count())
tasks = []
while True:
try:
if utils.SIGINT_COUNTER >= 1:
break
request_url = f"{args.site}/index.php?page=post&s=list&tags={search_tags}&pid={page_number}"
print(f"Going to {request_url}")
async with session.get(request_url) as response:
html = await response.text()
soup = BeautifulSoup(html, "html.parser")
thumbnails_div = soup.find("div", class_="thumbnail-container")
if not thumbnails_div:
raise RuntimeError("Thumbnails division not found.")
image_urls = [a["href"] for a in thumbnails_div.find_all("a")]
image_url_count = len(image_urls)
if image_url_count == 0:
print("Website returned 0 image urls.")
break
print(f"Got {image_url_count} posts.")
page_number += image_url_count
for image_url in image_urls:
if utils.SIGINT_COUNTER >= 1:
break
while len(tasks) >= MAX_TASKS:
if utils.SIGINT_COUNTER >= 1:
break
await asyncio.sleep(0.1)
for i in range(len(tasks) - 1, -1, -1):
task = tasks[i]
if task.done():
await task
del tasks[i]
tasks.append(asyncio.create_task(process_link(thread_pool, session, image_url, image_ids_to_ignore, args.width, args.height, args.avif, args.low_quality, args.min_tags)))
except Exception as e:
print(f"An error occurred: {e}\nPausing for 0.1 second before retrying...")
await asyncio.sleep(0.1)
if utils.SIGINT_COUNTER >= 1:
print("Script interrupted by user, gracefully exiting...\nYou can interrupt again to exit semi-forcefully, but it will break image checks!")
else:
print("No more images to download, waiting already submitted tasks to finish...")
while tasks and utils.SIGINT_COUNTER <= 1:
await asyncio.sleep(0.1)
for i in range(len(tasks) - 1, -1, -1):
task = tasks[i]
if task.done():
await task
del tasks[i]
if utils.SIGINT_COUNTER >= 2:
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!")
sys.exit(1)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nScript interrupted by user, exiting...")
sys.exit(1)
|