mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-14 21:38:38 +00:00
Search webservice (#2)
* Deployable script for windows file service * Deploy! * Server side work
This commit is contained in:
@@ -12,9 +12,8 @@ from habanero import Crossref
|
||||
from waitress import serve
|
||||
from queue import Queue
|
||||
import search_bot
|
||||
import scrape_bot
|
||||
import requests
|
||||
from typing import Callable
|
||||
from uuid import UUID
|
||||
NETRC_FILE = r"C:\Users\Activcom.pl\.netrc"
|
||||
|
||||
HOST_ADDRESS = "192.168.1.192"
|
||||
@@ -22,7 +21,7 @@ PORT_ADDRESS = 5001
|
||||
MAIN_BOT_ADDRESS = "http://192.168.1.191:5000"
|
||||
SEND_RESULTS = "/conjurer"
|
||||
BDSM_UUID_TEST = '96b7f85a-1142-4908-8986-62a2ea25a147'
|
||||
|
||||
MAX_CR_RESULTS = 150000
|
||||
ENCODING = "utf-8"
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -37,13 +36,15 @@ class Librarian(object):
|
||||
self.cr = Crossref(mailto=authTokens[0])
|
||||
self.query = query
|
||||
self.uuid = str(uuid)
|
||||
self.limit = 3000
|
||||
self.limit = MAX_CR_RESULTS
|
||||
self.fetched = 0
|
||||
self.hit = 0
|
||||
self.total = 0
|
||||
self.app = app
|
||||
self.logger = app.logger
|
||||
self.live_results = []
|
||||
self.final_result = []
|
||||
self.not_in_db = []
|
||||
self.search_result_from_cr = {}
|
||||
self.done = False
|
||||
|
||||
@@ -62,8 +63,7 @@ class Librarian(object):
|
||||
self.fetched += len(result['message']['items'])
|
||||
self.logger.info (self.total)
|
||||
self.logger.info(self.fetched)
|
||||
|
||||
time.sleep(1)
|
||||
time.sleep(0.1)
|
||||
self.app.logger.info("CROSSREF DONE")
|
||||
#TODO: add to sql with UUID
|
||||
#sql row - title, doi, author, uuid, last retrieved, is_available
|
||||
@@ -142,20 +142,20 @@ class Librarian(object):
|
||||
temp.append([item["DOI"], item])
|
||||
|
||||
refined_result = await self.check_if_exists(temp)
|
||||
temp = []
|
||||
|
||||
for item in refined_result:
|
||||
if item["exists"]:
|
||||
temp.append(item["data"])
|
||||
refined_result = temp
|
||||
self.hit = len(refined_result)
|
||||
return refined_result
|
||||
self.final_result.append(item["data"])
|
||||
else:
|
||||
self.not_in_db.append(item["data"])
|
||||
self.hit = len(self.final_result)
|
||||
return self.final_result
|
||||
#TODO: DOłożyć sprawdzenie czy w rafinowanym pliku już nie mamy częściowego wyniku
|
||||
async def answer_query(self):
|
||||
#TODO: DOłożyć sprawdzenie czy w rafinowanym pliku już nie mamy częściowego wyniku tutaj bo zanim w ogóle otworzymy nierafinowany
|
||||
#Search sql for UUID
|
||||
#SQL SELECT * FROM RESULTS WHERE UUID = uuid
|
||||
self.logger.info(f"Search started {self.uuid}")
|
||||
uuid_found = False
|
||||
database = None
|
||||
query = self.query
|
||||
with open("cr_results.json", "r+", encoding="utf-8") as cr_file:
|
||||
@@ -165,7 +165,7 @@ class Librarian(object):
|
||||
database = json.load(cr_file)
|
||||
except JSONDecodeError:
|
||||
pass
|
||||
|
||||
#TODO: Zwracać i final_result i not_in_db. Not in db_do pliku - i do ręcznego przeglądania potem.
|
||||
if database:
|
||||
for item in database.keys():
|
||||
if item in self.uuid:
|
||||
@@ -174,6 +174,10 @@ class Librarian(object):
|
||||
for item in self.result:
|
||||
temp[item['DOI']] = {"Title" : item['title'], "type": item['type']}
|
||||
self.result = temp
|
||||
temp2 = {}
|
||||
for item in self.not_in_db:
|
||||
temp2[item['DOI']] = {"Title" : item['title'], "type": item['type']}
|
||||
self.not_in_db = temp2
|
||||
return self.result
|
||||
answer = await self.search_crossref(query=query)
|
||||
database.update(answer)
|
||||
@@ -194,6 +198,10 @@ class Librarian(object):
|
||||
for item in self.result:
|
||||
temp[item['DOI']] = {"Title" : item['title'], "type": item['type']}
|
||||
self.result = temp
|
||||
temp2 = {}
|
||||
for item in self.not_in_db:
|
||||
temp2[item['DOI']] = {"Title" : item['title'], "type": item['type']}
|
||||
self.not_in_db = temp2
|
||||
return self.result
|
||||
|
||||
#============================= FLASK INTERNALS===============================
|
||||
@@ -221,11 +229,25 @@ class BackgroundTaskSearch(threading.Thread):
|
||||
async def _run(self):
|
||||
while True:
|
||||
database = None
|
||||
ndb_database = None
|
||||
librarian = librarian_queue.get()
|
||||
self.app.logger.info("STARTED")
|
||||
result = await librarian.answer_query()
|
||||
result = {librarian.uuid: result}
|
||||
self.app.logger.info("Saving to file")
|
||||
with open("not_in_db.json", "r+", encoding="utf-8") as ndb_file:
|
||||
try:
|
||||
ndb_database = json.load(ndb_file)
|
||||
except JSONDecodeError:
|
||||
pass
|
||||
if ndb_database:
|
||||
ndb_database.update(librarian.not_in_db)
|
||||
else:
|
||||
ndb_database = librarian.not_in_db
|
||||
ndb_file.truncate(0)
|
||||
ndb_file.seek(0)
|
||||
json.dump(ndb_database, ndb_file)
|
||||
|
||||
with open("s_results.json", "r+", encoding="utf-8") as s_file:
|
||||
try:
|
||||
database = json.load(s_file)
|
||||
@@ -291,14 +313,15 @@ async def get_partial():
|
||||
|
||||
#=======================================MAIN===================================================
|
||||
if __name__ == "__main__":
|
||||
logger = logging.getLogger('conjurer_musician')
|
||||
logger = app.logger
|
||||
logger.setLevel(logging.DEBUG)
|
||||
threads = []
|
||||
#threads.append(threading.Thread(target=waitress_run))
|
||||
threads.append(threading.Thread(target=flask_debug))
|
||||
threads.append(threading.Thread(target=waitress_run))
|
||||
#threads.append(threading.Thread(target=flask_debug))
|
||||
bgtask = BackgroundTaskSearch()
|
||||
bgtask.app = app
|
||||
threads.append(bgtask)
|
||||
threads.append(threading.Thread(target = scrape_bot.scraper, args=(logger,)))
|
||||
i = 0
|
||||
for worker in threads:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user