This commit is contained in:
2024-04-01 18:30:52 +02:00
parent 71131d4d90
commit 840a87d85f
2 changed files with 30 additions and 161310 deletions
+30 -23
View File
@@ -24,6 +24,7 @@ HOST_ADDRESS = "0.0.0.0"
PORT_ADDRESS = 5001
class Librarian(object):
def __init__(self) -> None:
netrc_mod = netrc.netrc(NETRC_FILE)
authTokens = netrc_mod.authenticators("crossref")
@@ -31,24 +32,26 @@ class Librarian(object):
self.last_search = ""
self.limit = 1000
def search_crossref(self, query):
def search_crossref(self, query, uuid):
self.last_search = self.cr.works(query = query, limit = self.limit)
for item in self.last_search['message']['items']:
print("=====")
#print (item)
print(item['DOI'])
print(item['title'])
print(item['type'])
print("=====")
if "container-title" in item:
print("This a part of larger release")
print(item['container-title'])
print (self.last_search['message']['total-results'])
#add to sql with UUID
#sql row - title, doi, author, uuid, last retrieved, is_available
with open("results.json", "x", encoding="UTF-8") as new_file:
new_file.seek(0)
json.dump(self.last_search['message']['items'], new_file, indent=4)
with open("results.json", "r+", encoding="UTF-8") as data_file:
# First we load existing data into a dict.
file_data = json.load(data_file)
# Join new_data with file_data inside emp_details
summarized_results = []
for item in self.last_search['message']['items']:
container = "NO"
if "container-title" in item:
container = "YES"
summarized_results.append({"DOI" : item['DOI'], "title": item['title'], "type": item['type'], "container": container})
result = {"UUID": uuid, "total_results": self.last_search['message']['total-results'], "on_page" : 1, "summary":summarized_results, "results":self.last_search['message']['items'], }
file_data.append(result)
data_file.seek(0)
# convert back to json.
json.dump(file_data, data_file, indent=4)
def check_if_exists(self,page_url):
in_scihub_db = True
@@ -78,14 +81,23 @@ class Librarian(object):
if self.check_if_exists(page_url):
return page_url
return None
def answer_query(self, uuid, query):
#Search sql for UUID
#SQL SELECT * FROM RESULTS WHERE UUID = uuid
uuid_found = True
uuid_found = False
with open("results.json", "r", encoding="UTF-8") as new_file:
#tu logika komunikacji z SQL i cała logika związana z wyszukaniem
#ale tymczasowo plik
database = json.load(new_file)
for item in database:
if item["UUID"]:
print item["UUID"]
uuid_found = True
if uuid_found:
return self.last_search
self.search_crossref(query=query, uuid=uuid)
self.search_crossref(query)
#example not exists
#sample_non_existing_page_url = "https://sci-hub.se/10.4324/9781003006237"
#sample usage to download
@@ -93,7 +105,6 @@ class Librarian(object):
#print(cl.download(sample_existing_page_url))
#cl.search_crossref("BDSM")
app = Flask(__name__)
def flask_debug():
"""
@@ -111,7 +122,7 @@ def waitress_run():
serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS)
@app.route("/query", methods=["POST"])
def update_music_list():
def query_database():
"""
The function `update_music_list` receives a POST request with a JSON payload, logs the received
item, adds it to a music file list, and returns a success message along with the updated record.
@@ -127,9 +138,6 @@ def update_music_list():
app.logger.info(record["UUID"])
cl = Librarian()
with open("results.json", "r", encoding="UTF-8") as new_file:
#tu logika komunikacji z SQL i cała logika związana z wyszukaniem
cl.last_search = json.load(new_file)
answer_data = cl.last_search
return_data = (
@@ -147,7 +155,6 @@ if __name__ == "__main__":
for worker in threads:
worker.start()
for worker in threads:
worker.join()
-161287
View File
File diff suppressed because one or more lines are too long