refactor(webrequest): linting, docstrings
This commit is contained in:
@@ -375,7 +375,6 @@ class RDSData:
|
||||
def transform(self, data: str):
|
||||
# rds_availability = RDS_AVAIL_DATA()
|
||||
# rds_data = RDS_GENERIC_DATA()
|
||||
print(data)
|
||||
|
||||
def __get_raw_data(data: str) -> list:
|
||||
# create base data to be turned into pydantic classes
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
"""A dataclass representing book data from the library system and catalogue."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
@@ -7,6 +11,15 @@ import regex
|
||||
|
||||
@dataclass
|
||||
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
|
||||
title: str | None = None
|
||||
signature: str | None = None
|
||||
@@ -14,7 +27,7 @@ class BookData:
|
||||
link: str | None = None
|
||||
isbn: str | list[str] | None = field(default_factory=list[str])
|
||||
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
|
||||
place: str | None = None
|
||||
year: int | None = None
|
||||
@@ -25,10 +38,11 @@ class BookData:
|
||||
old_book: Any | None = None
|
||||
media_type: str | None = None
|
||||
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
|
||||
|
||||
def __post_init__(self):
|
||||
def __post_init__(self) -> None:
|
||||
"""Run Post-initialization processing."""
|
||||
self.library_location = (
|
||||
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.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():
|
||||
setattr(self, key, value)
|
||||
return self
|
||||
|
||||
def merge(self, other: "BookData") -> "BookData":
|
||||
def merge(self, other: BookData) -> BookData:
|
||||
for key, value in other.__dict__.items():
|
||||
# merge lists, if the attribute is a list, extend it
|
||||
if isinstance(value, list):
|
||||
@@ -89,12 +103,12 @@ class BookData:
|
||||
return "Druckausgabe"
|
||||
return None
|
||||
|
||||
def from_string(self, data: str) -> "BookData":
|
||||
def from_string(self, data: str) -> BookData:
|
||||
ndata = json.loads(data)
|
||||
|
||||
return BookData(**ndata)
|
||||
|
||||
def from_LehmannsSearchResult(self, result: Any) -> "BookData":
|
||||
def from_LehmannsSearchResult(self, result: Any) -> BookData:
|
||||
self.title = result.title
|
||||
self.author = "; ".join(result.authors) if result.authors else None
|
||||
self.edition = str(result.edition) if result.edition else None
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
"""A module to request data from the internal catalogue data."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
@@ -16,6 +20,8 @@ from src.bibapi._transformers import (
|
||||
RDSData,
|
||||
RISData,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.bibapi.schemas.bookdata import BookData
|
||||
|
||||
API_URL = "https://rds.ibs-bw.de/phfreiburg/opac/RDSIndexrecord/{}/"
|
||||
@@ -28,10 +34,11 @@ ISBN = "RDS_ISBN"
|
||||
AUTHOR = "RDS_PERSON"
|
||||
ALLOWED_IPS = [
|
||||
"193.197.140.245", # PHFR Internal
|
||||
"193.197.140.249", # PHFR Eduroam
|
||||
]
|
||||
HEADERS = {
|
||||
"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",
|
||||
}
|
||||
RATE_LIMIT = 20
|
||||
@@ -60,7 +67,8 @@ class WebRequest:
|
||||
self.public_ip = None
|
||||
self._can_run()
|
||||
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:
|
||||
"""Check if requests can be made."""
|
||||
@@ -81,13 +89,13 @@ class WebRequest:
|
||||
self.use_any = True
|
||||
return self
|
||||
|
||||
def set_apparat(self, apparat: int) -> "WebRequest":
|
||||
def set_apparat(self, apparat: int) -> WebRequest:
|
||||
self.apparat = apparat
|
||||
if int(self.apparat) < 10:
|
||||
self.apparat = f"0{self.apparat}"
|
||||
return self
|
||||
|
||||
def get_ppn(self, signature: str) -> "WebRequest":
|
||||
def get_ppn(self, signature: str) -> WebRequest:
|
||||
self.signature = signature
|
||||
if "+" in signature:
|
||||
signature = signature.replace("+", "%2B")
|
||||
@@ -215,12 +223,12 @@ class BibTextTransformer:
|
||||
self.data = None
|
||||
# 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"""
|
||||
self.signature = signature
|
||||
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 -"
|
||||
ARRAY_IDENT = "[kid]"
|
||||
COinS_IDENT = "ctx_ver"
|
||||
|
||||
Reference in New Issue
Block a user