1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-25 02:57:22 +00:00

Update catch errors for load metadata from amazon (#2333)

This commit is contained in:
Ozzie Isaacs 2022-03-12 08:27:18 +01:00
parent 34478079d8
commit 49692b4a45

View File

@ -25,8 +25,11 @@ try:
except ImportError:
pass
from cps.services.Metadata import MetaRecord, MetaSourceInfo, Metadata
import cps.logger as logger
#from time import time
from operator import itemgetter
log = logger.create()
class Amazon(Metadata):
__name__ = "Amazon"
@ -49,14 +52,14 @@ class Amazon(Metadata):
):
#timer=time()
def inner(link, index) -> [dict, int]:
try:
with self.session as session:
r = session.get(f"https://www.amazon.com/{link}")
r = session.get(f"https://www.amazon.com{link}")
r.raise_for_status()
long_soup = BS(r.text, "lxml") #~4sec :/
soup2 = long_soup.find("div", attrs={"cel_widget_id": "dpx-books-ppd_csm_instrumentation_wrapper"})
if soup2 is None:
return
try:
match = MetaRecord(
title = "",
authors = "",
@ -65,7 +68,7 @@ class Amazon(Metadata):
description="Amazon Books",
link="https://amazon.com/"
),
url = f"https://www.amazon.com/{link}",
url = f"https://www.amazon.com{link}",
#the more searches the slower, these are too hard to find in reasonable time or might not even exist
publisher= "", # very unreliable
publishedDate= "", # very unreliable
@ -102,13 +105,15 @@ class Amazon(Metadata):
match.cover = ""
return match, index
except Exception as e:
print(e)
log.debug_or_exception(e)
return
val = list()
try:
if self.active:
results = self.session.get(
f"https://www.amazon.com/s?k={query.replace(' ', '+')}&i=digital-text&sprefix={query.replace(' ', '+')}"
f"https://www.amazon.com/s?k={query.replace(' ', '+')}"
f"&i=digital-text&sprefix={query.replace(' ', '+')}"
f"%2Cdigital-text&ref=nb_sb_noss",
headers=self.headers)
results.raise_for_status()
@ -120,3 +125,6 @@ class Amazon(Metadata):
val = list(map(lambda x: x.result(), concurrent.futures.as_completed(fut)))
result = list(filter(lambda x: x, val))
return [x[0] for x in sorted(result, key=itemgetter(1))] #sort by amazons listing order for best relevance
except requests.exceptions.HTTPError as e:
log.debug_or_exception(e)
return []