Replacement
This commit is contained in:
parent
f37ce39266
commit
c215e7aaae
@ -177,24 +177,35 @@ class MessageHandlerThread(threading.Thread):
|
|||||||
|
|
||||||
def get_metadata(self, name: str, baseName: str, evnetId: str) -> Optional[DataResult]:
|
def get_metadata(self, name: str, baseName: str, evnetId: str) -> Optional[DataResult]:
|
||||||
result = None
|
result = None
|
||||||
logger.info("Checking cache for offloading")
|
logger.info("Checking cache")
|
||||||
cache_result = ResultCache.get(name)
|
titleCache = ResultCache.get(name)
|
||||||
if cache_result is None:
|
if (titleCache is None):
|
||||||
cache_result = ResultCache.get(baseName)
|
titleCache = UseSource(title=name, eventId=evnetId).select_result()
|
||||||
if cache_result:
|
if titleCache is not None:
|
||||||
logger.info("Cache hit for %s", name)
|
logger.info("Storing response for %s in in-memory cache", name)
|
||||||
result = cache_result
|
ResultCache.add(title=name, result=titleCache)
|
||||||
else:
|
else:
|
||||||
logger.info("Cache hit for %s", name)
|
logger.info("Cache hit for %s", name)
|
||||||
result = cache_result
|
|
||||||
|
|
||||||
if result is None:
|
baseNameCache = ResultCache.get(baseName)
|
||||||
logger.info("Not in cache: %s or %s", name, baseName)
|
if (baseNameCache is None):
|
||||||
logger.info("Searching in sources for information about %s", name)
|
baseNameCache = UseSource(title=baseName, eventId=evnetId).select_result()
|
||||||
result: Optional[NamedDataResult] = UseSource(title=name, baseName=baseName, eventId=evnetId).select_result()
|
if baseNameCache is not None:
|
||||||
if (result.data.status == "COMPLETED"):
|
logger.info("Storing response for %s in in-memory cache", baseName)
|
||||||
logger.info("Storing response for %s in in-memory cache", name)
|
ResultCache.add(title=baseName, result=baseNameCache)
|
||||||
ResultCache.add(title=result.name, result=result.data)
|
else:
|
||||||
|
logger.info("Cache hit for %s", baseName)
|
||||||
|
|
||||||
|
if titleCache is not None and baseNameCache is not None:
|
||||||
|
if (titleCache.data.type.lower() == "movie" or baseNameCache.data.type.lower() == "movie"):
|
||||||
|
result = baseNameCache
|
||||||
|
else:
|
||||||
|
result = titleCache
|
||||||
|
elif titleCache is not None:
|
||||||
|
result = titleCache
|
||||||
|
elif baseNameCache is not None:
|
||||||
|
result = baseNameCache
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -31,11 +31,3 @@ class DataResult:
|
|||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return asdict(self)
|
return asdict(self)
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class NamedDataResult:
|
|
||||||
name: str
|
|
||||||
data: DataResult
|
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return asdict(self)
|
|
||||||
@ -30,11 +30,9 @@ class DataAndScore:
|
|||||||
|
|
||||||
class UseSource():
|
class UseSource():
|
||||||
title: str
|
title: str
|
||||||
baseName: str
|
|
||||||
eventId: str
|
eventId: str
|
||||||
def __init__(self, title, baseName, eventId) -> None:
|
def __init__(self, title, eventId) -> None:
|
||||||
self.title = title
|
self.title = title
|
||||||
self.baseName = baseName
|
|
||||||
self.eventId = eventId
|
self.eventId = eventId
|
||||||
|
|
||||||
def stripped(self, input_string) -> str:
|
def stripped(self, input_string) -> str:
|
||||||
@ -71,47 +69,26 @@ class UseSource():
|
|||||||
if (altScore > highScore):
|
if (altScore > highScore):
|
||||||
highScore = altScore
|
highScore = altScore
|
||||||
logger.info(f"[A:{highScore}]\t{self.stripped(wd.result.data.title.lower())} => {self.stripped(title.lower())}")
|
logger.info(f"[A:{highScore}]\t{self.stripped(wd.result.data.title.lower())} => {self.stripped(title.lower())}")
|
||||||
givenScore = highScore * (wd.weight / 10)
|
givenScore = highScore * wd.weight
|
||||||
logger.info(f"[G:{givenScore}]\t{self.stripped(wd.result.data.title.lower())} => {self.stripped(title.lower())} Weight:{wd.weight}")
|
logger.info(f"[G:{givenScore}]\t{self.stripped(wd.result.data.title.lower())} => {self.stripped(title.lower())} Weight:{wd.weight}")
|
||||||
result.append(DataAndScore(wd.result, givenScore))
|
result.append(DataAndScore(wd.result, givenScore))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def select_result(self) -> Optional[NamedDataResult]:
|
def select_result(self) -> Optional[DataResult]:
|
||||||
""""""
|
""""""
|
||||||
scored: List[DataAndScore] = []
|
result = self.__perform_search(title=self.title)
|
||||||
titleResult = self.__perform_search(title=self.title)
|
|
||||||
baseNameResult = self.__perform_search(title=self.baseName)
|
|
||||||
|
|
||||||
titleScoreResult = self.__calculate_score(title=self.title, weightData=titleResult)
|
scoredResult = self.__calculate_score(title=self.title, weightData=result)
|
||||||
baseNameScoreResult = self.__calculate_score(title=self.baseName, weightData=baseNameResult)
|
|
||||||
|
|
||||||
titleScoreResult.sort(key=lambda x: x.score, reverse=True)
|
scoredResult.sort(key=lambda x: x.score, reverse=True)
|
||||||
baseNameScoreResult.sort(key=lambda x: x.score, reverse=True)
|
|
||||||
|
|
||||||
scored.extend(titleScoreResult)
|
selected: DataResult|None = scoredResult[0].result if len(scoredResult) > 0 else None
|
||||||
scored.extend(baseNameScoreResult)
|
|
||||||
|
|
||||||
selected: NamedDataResult|None = None
|
|
||||||
ht = titleScoreResult[0]
|
|
||||||
bt = baseNameScoreResult[0]
|
|
||||||
if (bt is not None and ht is not None):
|
|
||||||
if (bt.score >= ht.score):
|
|
||||||
selected = NamedDataResult(self.baseName, bt.result)
|
|
||||||
else:
|
|
||||||
selected = NamedDataResult(self.title, ht.result)
|
|
||||||
else:
|
|
||||||
if len(titleScoreResult) > 0:
|
|
||||||
selected = NamedDataResult(self.title, titleScoreResult[0].result)
|
|
||||||
elif len(baseNameScoreResult) > 0:
|
|
||||||
selected = NamedDataResult(self.baseName, baseNameScoreResult[0].result)
|
|
||||||
else:
|
|
||||||
selected = None
|
|
||||||
|
|
||||||
jsr = ""
|
jsr = ""
|
||||||
try:
|
try:
|
||||||
jsr = json.dumps([obj.to_dict() for obj in scored], indent=4)
|
jsr = json.dumps([obj.to_dict() for obj in scoredResult], indent=4)
|
||||||
with open(f"./logs/{self.eventId}.json", "w", encoding="utf-8") as f:
|
with open(f"./logs/{self.eventId}-{self.title}.json", "w", encoding="utf-8") as f:
|
||||||
f.write(jsr)
|
f.write(jsr)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.info("Couldn't dump log..")
|
logger.info("Couldn't dump log..")
|
||||||
@ -119,15 +96,13 @@ class UseSource():
|
|||||||
logger.info(jsr)
|
logger.info(jsr)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
titledResult = titleResult
|
|
||||||
titledResult.extend(baseNameResult)
|
|
||||||
|
|
||||||
titles: List[str] = []
|
titles: List[str] = []
|
||||||
for wd in titledResult:
|
for wd in scoredResult:
|
||||||
titles.append(wd.result.data.title)
|
titles.append(wd.result.data.title)
|
||||||
titles.extend(wd.result.data.altTitle)
|
titles.extend(wd.result.data.altTitle)
|
||||||
joinedTitles = "\n\t" + "\n\t".join(titles)
|
joinedTitles = "\n\t" + "\n\t".join(titles)
|
||||||
logger.info(f"\nTitle: {self.title} \nBaseName: {self.baseName} \nFound: {joinedTitles} \nTitle selected: \n\t{selected.data.data.title}\n")
|
logger.info(f"\Name: {self.title} \n \nFound: {joinedTitles} \nTitle selected: \n\t{selected.data.title}\n")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
pass
|
pass
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user