Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import filetype
- import os
- import requests
- import shutil
- from threading import Thread
- from urllib.parse import urlparse, parse_qs
- MAX_TRIES = 50
- POOL_SIZE = 10
- START_ID = "aaaaaa"
- headers = {
- "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",
- "Accept-Encoding": "gzip, deflate",
- "Connection": "keep-alive"
- }
- def get_image(url, num_try=0):
- if num_try > MAX_TRIES:
- return None
- t = requests.get(url, headers=headers)
- if t.url != url:
- parsed = urlparse(t.url)
- parsed_q = parse_qs(parsed.query)
- print(url)
- print(t.url)
- return parsed_q["u"][0]
- else:
- if t.text.startswith('{"state":"fail"'):
- return "FAIL"
- else:
- return get_image(url, num_try=num_try + 1)
- ALLOWED_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- def inc_char(c):
- i = ALLOWED_CHARS.index(c)
- if i < len(ALLOWED_CHARS) - 1:
- return ALLOWED_CHARS[i + 1]
- return ALLOWED_CHARS[0]
- def get_id(prev_id):
- last = get_last_increment(prev_id)
- if last == -1:
- return ""
- unchanged = prev_id[:last]
- changed = prev_id[last:]
- first_inc = inc_char(changed[0])
- rest_count = len(changed) - 1
- return unchanged + first_inc + (ALLOWED_CHARS[0] * rest_count)
- def get_last_increment(str):
- c = len(str) - 1
- while c >= 0:
- if str[c] != 'z':
- return c
- c -= 1
- return -1
- def build_url(id):
- return f"http://gs.3g.cn/D/{id}/w"
- def download_data(id):
- url = build_url(id)
- print(f"ID: {id} URL: {url}")
- try:
- img = get_image(url)
- if img is not None and img != "FAIL":
- img_data = requests.get(img, stream=True)
- img_data.decode_content = True
- with open(f"output/{id}.bin", "wb") as output_file:
- shutil.copyfileobj(img_data.raw, output_file)
- del img_data
- kind = filetype.guess(f"output/{id}.bin")
- if kind is None:
- print(f"Unknown file type for {id}")
- else:
- os.rename(f"output/{id}.bin", f"output/{id}.{kind.extension}")
- print(f"Saved file \"output/{id}.{kind.extension}\"")
- except:
- print(f"Skipped {id} due to many fails.")
- pass
- print()
- thread_pool = []
- id = get_id(START_ID)
- while id != "":
- while len(thread_pool) < POOL_SIZE:
- t = Thread(target=download_data, args=(id,))
- t.start()
- thread_pool.append(t)
- id = get_id(id)
- to_remove = []
- for thread in thread_pool:
- if not thread.is_alive():
- to_remove.append(thread)
- for thread in to_remove:
- thread_pool.remove(thread)
- try:
- thread.join()
- except:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement