dev #21

Merged
WorldTeacher merged 46 commits from dev into main 2025-11-24 12:59:41 +00:00
Showing only changes of commit 8b8c1c9393 - Show all commits

View File

@@ -1,11 +1,27 @@
import sys
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime
from typing import Dict, Iterable, List, Optional, Tuple from typing import Dict, Iterable, List, Optional, Tuple
import loguru
import requests import requests
from src import LOG_DIR
from src.logic.dataclass import BookData 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 # Dataclasses
# ----------------------- # -----------------------
@@ -393,15 +409,19 @@ def book_from_marc(rec: MarcRecord) -> BookData:
) )
isbn = subfield_values(rec, "020", "a") isbn = subfield_values(rec, "020", "a")
lang = subfield_values(rec, "041", "a")
return BookData( return BookData(
ppn=ppn, ppn=ppn,
title=title, title=title,
signature=signature, signature=signature,
edition=first_subfield_value(rec, "250", "a"), edition=first_subfield_value(rec, "250", "a") or "",
year=year, year=year,
pages=first_subfield_value(rec, "300", "a"), pages=first_subfield_value(rec, "300", "a") or "",
publisher=first_subfield_value(rec, "264", "b"), publisher=first_subfield_value(rec, "264", "b") or "",
isbn=isbn, isbn=isbn,
language=lang,
link="",
) )
@@ -418,7 +438,7 @@ class SWB:
url = self.url.format(query) url = self.url.format(query)
print("Fetching from SWB:", url) log.debug(url)
headers = { 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", "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", "Accept": "application/xml",
@@ -427,7 +447,7 @@ class SWB:
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
if response.status_code != 200: if response.status_code != 200:
raise Exception(f"Error fetching data from SWB: {response.status_code}") raise Exception(f"Error fetching data from SWB: {response.status_code}")
# print(response.text) # #print(response.text)
data = response.content data = response.content
# extract top-level response # extract top-level response
@@ -437,12 +457,22 @@ class SWB:
def getBooks(self, query_args: Iterable[str]) -> List[BookData]: def getBooks(self, query_args: Iterable[str]) -> List[BookData]:
records: List[Record] = self.get(query_args) records: List[Record] = self.get(query_args)
books: List[BookData] = [] books: List[BookData] = []
title = query_args[1].split("=")[1] # extract title from query_args if present
# print(len(records), "records found") title = None
for arg in query_args:
if arg.startswith("pica.tit="):
title = arg.split("=")[1]
break
for rec in records: for rec in records:
book = book_from_marc(rec.recordData) book = book_from_marc(rec.recordData)
books.append(book) books.append(book)
books = [ if title:
b for b in books if b.title and b.title.lower().startswith(title.lower()) books = [
] b
for b in books
if b.title and b.title.lower().startswith(title.lower())
]
return books return books
def getLinkForBook(self, book: BookData) -> str:
results = self.getBooks()