@@ -70,18 +70,23 @@ async def _get_audnexus_book(
7070 https://audnex.us/#tag/Books/operation/getBookById
7171 """
7272 logger .debug ("Fetching book from Audnexus" , asin = asin , region = region )
73- async with session .get (
74- f"https://api.audnex.us/books/{ asin } ?region={ region } "
75- ) as response :
76- if not response .ok :
77- logger .warning (
78- "Failed to fetch book from Audnexus" ,
79- asin = asin ,
80- status = response .status ,
81- reason = response .reason ,
82- )
83- return None
84- book = await response .json ()
73+ try :
74+ async with session .get (
75+ f"https://api.audnex.us/books/{ asin } ?region={ region } " ,
76+ headers = {"Client-Agent" : "audiobookrequest" },
77+ ) as response :
78+ if not response .ok :
79+ logger .warning (
80+ "Failed to fetch book from Audnexus" ,
81+ asin = asin ,
82+ status = response .status ,
83+ reason = response .reason ,
84+ )
85+ return None
86+ book = await response .json ()
87+ except Exception as e :
88+ logger .error ("Exception while fetching book from Audnexus" , asin = asin , error = e )
89+ return None
8590 return BookRequest (
8691 asin = book ["asin" ],
8792 title = book ["title" ],
@@ -103,18 +108,23 @@ async def _get_audimeta_book(
103108 https://audimeta.de/api-docs/#/book/get_book__asin_
104109 """
105110 logger .debug ("Fetching book from Audimeta" , asin = asin , region = region )
106- async with session .get (
107- f"https://audimeta.de/book/{ asin } ?region={ region } "
108- ) as response :
109- if not response .ok :
110- logger .warning (
111- "Failed to fetch book from Audimeta" ,
112- asin = asin ,
113- status = response .status ,
114- reason = response .reason ,
115- )
116- return None
117- book = await response .json ()
111+ try :
112+ async with session .get (
113+ f"https://audimeta.de/book/{ asin } ?region={ region } " ,
114+ headers = {"Client-Agent" : "audiobookrequest" },
115+ ) as response :
116+ if not response .ok :
117+ logger .warning (
118+ "Failed to fetch book from Audimeta" ,
119+ asin = asin ,
120+ status = response .status ,
121+ reason = response .reason ,
122+ )
123+ return None
124+ book = await response .json ()
125+ except Exception as e :
126+ logger .error ("Exception while fetching book from Audimeta" , asin = asin , error = e )
127+ return None
118128 return BookRequest (
119129 asin = book ["asin" ],
120130 title = book ["title" ],
@@ -135,10 +145,19 @@ async def get_book_by_asin(
135145 book = await _get_audimeta_book (session , asin , audible_region )
136146 if book :
137147 return book
148+ logger .debug (
149+ "Audimeta did not have the book, trying Audnexus" ,
150+ asin = asin ,
151+ region = audible_region ,
152+ )
138153 book = await _get_audnexus_book (session , asin , audible_region )
139154 if book :
140155 return book
141- logger .warning ("Failed to fetch book" , asin = asin , region = audible_region )
156+ logger .warning (
157+ "Did not find the book on both Audnexus and Audimeta" ,
158+ asin = asin ,
159+ region = audible_region ,
160+ )
142161
143162
144163class CacheQuery (pydantic .BaseModel , frozen = True ):
0 commit comments