Add initial implementation of Audible Series Checker with API connectors and configuration
This commit is contained in:
commit
223bfbf6bc
10 changed files with 630 additions and 0 deletions
61
connectors/abs_connector.py
Normal file
61
connectors/abs_connector.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import requests
|
||||
import json
|
||||
|
||||
|
||||
class ABSConnector:
|
||||
def __init__(self, abs_url, token=None):
|
||||
self.abs_url = abs_url
|
||||
self.requests = requests.Session()
|
||||
self.requests.headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
def get_library_ids(self):
|
||||
endpoint = f"{self.abs_url}/api/libraries"
|
||||
response = self.requests.get(endpoint)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
return data["libraries"]
|
||||
|
||||
def get_series_by_library_id(self, library_id, page_size=100):
|
||||
endpoint = f"{self.abs_url}/api/libraries/{library_id}/series"
|
||||
page = 0
|
||||
|
||||
while True:
|
||||
response = self.requests.get(
|
||||
endpoint,
|
||||
params={
|
||||
"limit": page_size,
|
||||
"page": page,
|
||||
"minified": 1,
|
||||
"sort": "name",
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
yield from data["results"]
|
||||
|
||||
page += 1
|
||||
|
||||
if data["total"] < page_size * page: # Stop if no more data
|
||||
break
|
||||
|
||||
|
||||
class ABSConnectorMock(ABSConnector):
|
||||
def get_library_ids(self):
|
||||
with open("dumps/libraries.json", "r") as f:
|
||||
data = json.load(f)
|
||||
return data["libraries"]
|
||||
|
||||
def get_series_by_library_id(self, library_id, page_size=100):
|
||||
page = 0
|
||||
|
||||
while True:
|
||||
with open(f"dumps/library_{library_id}.page{page}.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
yield from data["results"]
|
||||
|
||||
page += 1
|
||||
|
||||
if data["total"] < page_size * page: # Stop if no more data
|
||||
break
|
||||
Loading…
Add table
Add a link
Reference in a new issue