jkjnkjkllkjjk
This commit is contained in:
@@ -75,6 +75,11 @@ def credential_openlibrary(config: Dict[str, Any]) -> Tuple[Optional[str], Optio
|
||||
return email, password
|
||||
|
||||
|
||||
class BookNotAvailableError(Exception):
|
||||
"""Raised when a book is not available for borrowing (waitlisted/in use)."""
|
||||
pass
|
||||
|
||||
|
||||
def display_error(response: requests.Response, message: str) -> None:
|
||||
"""Display error and exit."""
|
||||
log(message, file=sys.stderr)
|
||||
@@ -133,9 +138,11 @@ def loan(session: requests.Session, book_id: str, verbose: bool = True) -> reque
|
||||
if response.status_code == 400:
|
||||
try:
|
||||
if response.json()["error"] == "This book is not available to borrow at this time. Please try again later.":
|
||||
debug("This book doesn't need to be borrowed")
|
||||
return session
|
||||
debug("Book is not available for borrowing (waitlisted or in use)")
|
||||
raise BookNotAvailableError("Book is waitlisted or in use")
|
||||
display_error(response, "Something went wrong when trying to borrow the book.")
|
||||
except BookNotAvailableError:
|
||||
raise
|
||||
except:
|
||||
display_error(response, "The book cannot be borrowed")
|
||||
|
||||
@@ -182,11 +189,21 @@ def get_book_infos(session: requests.Session, url: str) -> Tuple[str, List[str],
|
||||
|
||||
# Try to extract the infos URL from the response
|
||||
try:
|
||||
# Look for the "url" field in the response
|
||||
if '"url":"' not in r:
|
||||
raise ValueError("No 'url' field found in response")
|
||||
infos_url = "https:" + r.split('"url":"')[1].split('"')[0].replace("\\u0026", "&")
|
||||
except (IndexError, ValueError) as e:
|
||||
# Look for the "url" field in the response using regex
|
||||
# Matches "url":"//archive.org/..."
|
||||
import re
|
||||
match = re.search(r'"url"\s*:\s*"([^"]+)"', r)
|
||||
if not match:
|
||||
raise ValueError("No 'url' field found in response")
|
||||
|
||||
url_path = match.group(1)
|
||||
if url_path.startswith("//"):
|
||||
infos_url = "https:" + url_path
|
||||
else:
|
||||
infos_url = url_path
|
||||
|
||||
infos_url = infos_url.replace("\\u0026", "&")
|
||||
except (IndexError, ValueError, AttributeError) as e:
|
||||
# If URL extraction fails, raise with better error message
|
||||
raise RuntimeError(f"Failed to extract book info URL from response: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user