chore: add logging, minor changes

This commit is contained in:
2025-09-18 07:14:40 +02:00
parent 247db562b1
commit 8b8c1c9393

View File

@@ -1,11 +1,27 @@
import sys
import xml.etree.ElementTree as ET
from dataclasses import dataclass, field
from datetime import datetime
from typing import Dict, Iterable, List, Optional, Tuple
import loguru
import requests
from src import LOG_DIR
from src.logic.dataclass import BookData
log = loguru.logger
log.remove()
log.add(sys.stdout, level="INFO")
log.add(f"{LOG_DIR}/application.log", rotation="1 MB", retention="10 days")
log.add(
f"{LOG_DIR}/{datetime.now().strftime('%Y-%m-%d')}.log",
rotation="1 day",
retention="1 month",
)
# -----------------------
# Dataclasses
# -----------------------
@@ -393,15 +409,19 @@ def book_from_marc(rec: MarcRecord) -> BookData:
)
isbn = subfield_values(rec, "020", "a")
lang = subfield_values(rec, "041", "a")
return BookData(
ppn=ppn,
title=title,
signature=signature,
edition=first_subfield_value(rec, "250", "a"),
edition=first_subfield_value(rec, "250", "a") or "",
year=year,
pages=first_subfield_value(rec, "300", "a"),
publisher=first_subfield_value(rec, "264", "b"),
pages=first_subfield_value(rec, "300", "a") or "",
publisher=first_subfield_value(rec, "264", "b") or "",
isbn=isbn,
language=lang,
link="",
)
@@ -418,7 +438,7 @@ class SWB:
url = self.url.format(query)
print("Fetching from SWB:", url)
log.debug(url)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
"Accept": "application/xml",
@@ -427,7 +447,7 @@ class SWB:
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(f"Error fetching data from SWB: {response.status_code}")
# print(response.text)
# #print(response.text)
data = response.content
# extract top-level response
@@ -437,12 +457,22 @@ class SWB:
def getBooks(self, query_args: Iterable[str]) -> List[BookData]:
records: List[Record] = self.get(query_args)
books: List[BookData] = []
title = query_args[1].split("=")[1]
# print(len(records), "records found")
# extract title from query_args if present
title = None
for arg in query_args:
if arg.startswith("pica.tit="):
title = arg.split("=")[1]
break
for rec in records:
book = book_from_marc(rec.recordData)
books.append(book)
books = [
b for b in books if b.title and b.title.lower().startswith(title.lower())
]
if title:
books = [
b
for b in books
if b.title and b.title.lower().startswith(title.lower())
]
return books
def getLinkForBook(self, book: BookData) -> str:
results = self.getBooks()