import json from json.decoder import JSONDecodeError from urllib.request import urlopen from requests import Timeout,ConnectTimeout, ConnectionError from queue import Queue import re import time import random import logging from threading import Thread SCR_DATABASE_PATH = r'C:\\Database\\chunks\\' SCR_FILENAME = "40_chunk.txt" SCR_ENCODING = "utf-8" WORK_Q = Queue() random.seed() def load_ndb_to_q(logger): logger.info("Loader started") while True: with open("not_in_db.json", "r+", encoding="utf-8") as ndb_file: try: ndb_database = json.load(ndb_file) for item in ndb_database.keys(): logger.info(item) url = f"https://sci-hub.se/{item}" WORK_Q.put([item, url, False]) ndb_file.truncate(0) except JSONDecodeError: time.sleep(60*60*2) pass time.sleep(180) def check_if_exists_brute_force(logger): while True: logger.info("Scraper tick") item = WORK_Q.get() page_url =item[1] logger.error("REFINE: Brute force search!") if not page_url.startswith(("http:", "https:")): raise ValueError("URL must start with 'http:' or 'https:'") blocked = True try: # trunk-ignore(bandit/B310) with urlopen(page_url) as response: data = response.read() text = data.decode("utf-8") if item[2]: logger.info("Already found") time.sleep(180) blocked = False else: for line in text.splitlines(): if re.match(r".*Unfortunately, Sci-Hub doesn't have the requested document.*", line): blocked = False logger.info("Not found") item[2] = False if m := re.match(r".*