refactor(webrequest): linting, docstrings

This commit is contained in:
2025-12-10 10:11:07 +01:00
parent f87e56a92f
commit e09247a03e
3 changed files with 37 additions and 16 deletions

View File

@@ -375,7 +375,6 @@ class RDSData:
def transform(self, data: str): def transform(self, data: str):
# rds_availability = RDS_AVAIL_DATA() # rds_availability = RDS_AVAIL_DATA()
# rds_data = RDS_GENERIC_DATA() # rds_data = RDS_GENERIC_DATA()
print(data)
def __get_raw_data(data: str) -> list: def __get_raw_data(data: str) -> list:
# create base data to be turned into pydantic classes # create base data to be turned into pydantic classes

View File

@@ -1,3 +1,7 @@
"""A dataclass representing book data from the library system and catalogue."""
from __future__ import annotations
import json import json
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Any from typing import Any
@@ -7,6 +11,15 @@ import regex
@dataclass @dataclass
class BookData: class BookData:
"""A dataclass representing the book object.
Returns
-------
self : BookData
The book data object with attributes like title, author, year, etc.
"""
ppn: str | None = None ppn: str | None = None
title: str | None = None title: str | None = None
signature: str | None = None signature: str | None = None
@@ -14,7 +27,7 @@ class BookData:
link: str | None = None link: str | None = None
isbn: str | list[str] | None = field(default_factory=list[str]) isbn: str | list[str] | None = field(default_factory=list[str])
author: str | None = None author: str | None = None
language: str | list[str] | None = field(default_factory=list) language: str | list[str] | None = field(default_factory=list[str])
publisher: str | None = None publisher: str | None = None
place: str | None = None place: str | None = None
year: int | None = None year: int | None = None
@@ -25,10 +38,11 @@ class BookData:
old_book: Any | None = None old_book: Any | None = None
media_type: str | None = None media_type: str | None = None
in_library: bool | None = None # whether the book is in the library or not in_library: bool | None = None # whether the book is in the library or not
libraries: list[str] | None = field(default_factory=list) libraries: list[str] | None = field(default_factory=list[str])
medianr: int | None = None # media number medianr: int | None = None # media number
def __post_init__(self): def __post_init__(self) -> None:
"""Run Post-initialization processing."""
self.library_location = ( self.library_location = (
str(self.library_location) if self.library_location else None str(self.library_location) if self.library_location else None
) )
@@ -38,12 +52,12 @@ class BookData:
self.year = regex.sub(r"[^\d]", "", str(self.year)) if self.year else None self.year = regex.sub(r"[^\d]", "", str(self.year)) if self.year else None
self.in_library = True if self.signature else False self.in_library = True if self.signature else False
def from_dict(self, data: dict) -> "BookData": def from_dict(self, data: dict[str, Any]) -> BookData:
for key, value in data.items(): for key, value in data.items():
setattr(self, key, value) setattr(self, key, value)
return self return self
def merge(self, other: "BookData") -> "BookData": def merge(self, other: BookData) -> BookData:
for key, value in other.__dict__.items(): for key, value in other.__dict__.items():
# merge lists, if the attribute is a list, extend it # merge lists, if the attribute is a list, extend it
if isinstance(value, list): if isinstance(value, list):
@@ -89,12 +103,12 @@ class BookData:
return "Druckausgabe" return "Druckausgabe"
return None return None
def from_string(self, data: str) -> "BookData": def from_string(self, data: str) -> BookData:
ndata = json.loads(data) ndata = json.loads(data)
return BookData(**ndata) return BookData(**ndata)
def from_LehmannsSearchResult(self, result: Any) -> "BookData": def from_LehmannsSearchResult(self, result: Any) -> BookData:
self.title = result.title self.title = result.title
self.author = "; ".join(result.authors) if result.authors else None self.author = "; ".join(result.authors) if result.authors else None
self.edition = str(result.edition) if result.edition else None self.edition = str(result.edition) if result.edition else None

View File

@@ -1,5 +1,9 @@
"""A module to request data from the internal catalogue data."""
from __future__ import annotations
from enum import Enum from enum import Enum
from typing import Any from typing import TYPE_CHECKING, Any
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@@ -16,7 +20,9 @@ from src.bibapi._transformers import (
RDSData, RDSData,
RISData, RISData,
) )
from src.bibapi.schemas.bookdata import BookData
if TYPE_CHECKING:
from src.bibapi.schemas.bookdata import BookData
API_URL = "https://rds.ibs-bw.de/phfreiburg/opac/RDSIndexrecord/{}/" API_URL = "https://rds.ibs-bw.de/phfreiburg/opac/RDSIndexrecord/{}/"
PPN_URL = "https://rds.ibs-bw.de/phfreiburg/opac/RDSIndex/Search?type0%5B%5D=allfields&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=au&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=ti&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=ct&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=isn&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=ta&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=co&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=py&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=pp&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=pu&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=si&lookfor0%5B%5D={}&join=AND&bool0%5B%5D=AND&type0%5B%5D=zr&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=cc&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND" PPN_URL = "https://rds.ibs-bw.de/phfreiburg/opac/RDSIndex/Search?type0%5B%5D=allfields&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=au&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=ti&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=ct&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=isn&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=ta&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=co&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=py&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=pp&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=pu&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=si&lookfor0%5B%5D={}&join=AND&bool0%5B%5D=AND&type0%5B%5D=zr&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND&type0%5B%5D=cc&lookfor0%5B%5D=&join=AND&bool0%5B%5D=AND"
@@ -28,10 +34,11 @@ ISBN = "RDS_ISBN"
AUTHOR = "RDS_PERSON" AUTHOR = "RDS_PERSON"
ALLOWED_IPS = [ ALLOWED_IPS = [
"193.197.140.245", # PHFR Internal "193.197.140.245", # PHFR Internal
"193.197.140.249", # PHFR Eduroam
] ]
HEADERS = { HEADERS = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \
(HTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36", (HTML, like Gecko) Chromes/44.0.2403.157 Safari/537.36",
"Accept-Language": "en-US, en;q=0.5", "Accept-Language": "en-US, en;q=0.5",
} }
RATE_LIMIT = 20 RATE_LIMIT = 20
@@ -60,7 +67,8 @@ class WebRequest:
self.public_ip = None self.public_ip = None
self._can_run() self._can_run()
if self.public_ip not in ALLOWED_IPS: if self.public_ip not in ALLOWED_IPS:
raise PermissionError("IP not allowed to access the requested data.") e = f"IP {self.public_ip} not allowed to access the requested data"
raise PermissionError(e)
def _can_run(self) -> None: def _can_run(self) -> None:
"""Check if requests can be made.""" """Check if requests can be made."""
@@ -81,13 +89,13 @@ class WebRequest:
self.use_any = True self.use_any = True
return self return self
def set_apparat(self, apparat: int) -> "WebRequest": def set_apparat(self, apparat: int) -> WebRequest:
self.apparat = apparat self.apparat = apparat
if int(self.apparat) < 10: if int(self.apparat) < 10:
self.apparat = f"0{self.apparat}" self.apparat = f"0{self.apparat}"
return self return self
def get_ppn(self, signature: str) -> "WebRequest": def get_ppn(self, signature: str) -> WebRequest:
self.signature = signature self.signature = signature
if "+" in signature: if "+" in signature:
signature = signature.replace("+", "%2B") signature = signature.replace("+", "%2B")
@@ -215,12 +223,12 @@ class BibTextTransformer:
self.data = None self.data = None
# self.bookdata = BookData(**self.data) # self.bookdata = BookData(**self.data)
def use_signature(self, signature: str) -> "BibTextTransformer": def use_signature(self, signature: str) -> BibTextTransformer:
"""Use the exact signature to search for the book""" """Use the exact signature to search for the book"""
self.signature = signature self.signature = signature
return self return self
def get_data(self, data: list[str] | None = None) -> "BibTextTransformer": def get_data(self, data: list[str] | None = None) -> BibTextTransformer:
RIS_IDENT = "TY -" RIS_IDENT = "TY -"
ARRAY_IDENT = "[kid]" ARRAY_IDENT = "[kid]"
COinS_IDENT = "ctx_ver" COinS_IDENT = "ctx_ver"