From 0fe042d0c056a2ee7aacd93938e7630bc089287f Mon Sep 17 00:00:00 2001 From: Michal Tuszowski Date: Sun, 7 Apr 2024 15:54:31 +0200 Subject: [PATCH] Fixes! --- conjurer_librarian/conjurer_librarian.py | 48 +++++++-------- conjurer_librarian/search_bot.py | 75 ++++++++++++++++++++++++ utils/cut_large_file.py | 19 ++++-- 3 files changed, 109 insertions(+), 33 deletions(-) create mode 100644 conjurer_librarian/search_bot.py diff --git a/conjurer_librarian/conjurer_librarian.py b/conjurer_librarian/conjurer_librarian.py index 39da52b..b1b627a 100644 --- a/conjurer_librarian/conjurer_librarian.py +++ b/conjurer_librarian/conjurer_librarian.py @@ -9,9 +9,6 @@ from json.decoder import JSONDecodeError from platform import uname from sys import platform from urllib.request import urlopen -import asyncio -from queue import Queue -from threading import Thread from habanero import Crossref from waitress import serve @@ -76,29 +73,15 @@ class Librarian(object): #TODO: Później robimy tak że query jest najpierw sprawdzane w sql - i dawane jako "zapisana lokalna kopia" #TODO: Paginacja - najpierw hity z pliku, potem na końcu to co nie zwróciło żadnych hitów - i to dopiero można crawlerem sprawdzić - def check_dois_in_file(self, file_name, dois, out_queue): - with open(f'{DATABASE_PATH}{file_name}', "r+", encoding=ENCODING) as file: - while True: - # Get next line from file - line = file.readline() - if line == dois: - return True - # if line is empty - # end of file is reached - if not line: - return False def check_dois_in_local_db(self, dois): - _sentinel = object() - chunk_list = [] - q = Queue() + #odwolanie do search_bota - for file in chunk_list: - t = Thread(target = self.check_dois_in_file, args = (file ,dois, q)) - t.start() - q.put(_sentinel) + result = False + return result - def check_if_exists(self, page_url): + + def check_if_exists_brute_force(self, page_url): if not page_url.startswith(("http:", "https:")): raise ValueError("URL must start with 'http:' or 'https:'") # trunk-ignore(bandit/B310) @@ -107,23 +90,31 @@ class Librarian(object): text = data.decode("utf-8") for line in text.splitlines(): if re.match(r".*Unfortunately, Sci-Hub doesn't have the requested document.*", line): - return None + return True if m := re.match(r".*= MAXTHREADS: + print("All workers finished") + break + + +def search_for_doi(doi): + threads = [] + work_q = Queue() + control_q = Queue() + t_cons = Thread(target = consumer, args = (work_q, control_q, doi)) + threads.append(t_cons) + for i in range(0,MAXTHREADS): + #filename = "test" + str(i) + CHUNk + filename = str(i) + CHUNk + print(filename) + threads.append(Thread(target = producer, args = (work_q, control_q, filename))) + for worker in threads: + worker.start() + for worker in threads: + worker.join() + + try: + check = control_q.get(block=False) + control_q.put(check) + return True + except Empty: + return False + +if __name__ == "__main__": + if search_for_doi("10.1145/2559206.2567832"): + print("found") + exit() + else: + print("Not found") \ No newline at end of file diff --git a/utils/cut_large_file.py b/utils/cut_large_file.py index 6a02153..176da2f 100644 --- a/utils/cut_large_file.py +++ b/utils/cut_large_file.py @@ -39,9 +39,12 @@ def smart_write(lines, file): :return: The function `smart_write` returns the size of the encoded bytes of the input `lines` in bytes. """ - encoded_bytes = lines.encode("utf-8") - size_in_bytes = len(encoded_bytes) - file.write(lines) + try: + encoded_bytes = lines.encode("utf-8") + size_in_bytes = len(encoded_bytes) + file.write(lines) + except: + return 0 return size_in_bytes @@ -65,8 +68,9 @@ def cut_my_life_into_pieces(source, output_folder, output_file): with open(source, encoding="utf-8") as infile: output_size = 0 output_no = 0 - + lineno = 0 for line in infile: + lineno += 1 if output_size > FILE_SIZE_LIMIT: output_no += 1 output_size = 0 @@ -76,7 +80,12 @@ def cut_my_life_into_pieces(source, output_folder, output_file): "a", encoding="utf-8", ) as op_file: - output_size += smart_write(line, op_file) + tmp = smart_write(line, op_file) + if tmp > 0: + output_size += smart_write(line, op_file) + else: + print (f'Error at {lineno}') + print(line) if __name__ == "__main__": cut_my_life_into_pieces(SOURCE,OUTPUT_FOLDER,OUTPUT_FILE_SCHEMA) \ No newline at end of file