Compare commits
2 commits
223bfbf6bc
...
503ffd0930
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
503ffd0930 | ||
|
|
a81d277f17 |
5 changed files with 89 additions and 33 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
class ABSConnector:
|
||||
|
|
@ -15,11 +16,8 @@ class ABSConnector:
|
|||
data = response.json()
|
||||
return data["libraries"]
|
||||
|
||||
def get_series_by_library_id(self, library_id, page_size=100):
|
||||
def _get_library_page(self, library_id, page=0, page_size=100):
|
||||
endpoint = f"{self.abs_url}/api/libraries/{library_id}/series"
|
||||
page = 0
|
||||
|
||||
while True:
|
||||
response = self.requests.get(
|
||||
endpoint,
|
||||
params={
|
||||
|
|
@ -30,7 +28,13 @@ class ABSConnector:
|
|||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
return response.json()
|
||||
|
||||
def get_series_by_library_id(self, library_id, page_size=100):
|
||||
page = 0
|
||||
|
||||
while True:
|
||||
data = self.__get_library_page(library_id, page, page_size)
|
||||
|
||||
yield from data["results"]
|
||||
|
||||
|
|
@ -41,17 +45,39 @@ class ABSConnector:
|
|||
|
||||
|
||||
class ABSConnectorMock(ABSConnector):
|
||||
def __init__(self, abs_url, token=None):
|
||||
super().__init__(abs_url, token)
|
||||
|
||||
self.directory = "dumps/abs"
|
||||
if not os.path.exists(self.directory):
|
||||
os.makedirs(self.directory)
|
||||
|
||||
def get_library_ids(self):
|
||||
with open("dumps/libraries.json", "r") as f:
|
||||
path = f"{self.directory}/libraries.json"
|
||||
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
data = json.load(f)
|
||||
return data["libraries"]
|
||||
except FileNotFoundError:
|
||||
data = ABSConnector.get_library_ids(self)
|
||||
with open(path, "w+") as f:
|
||||
json.dump({"libraries": data}, f, indent=4)
|
||||
return data
|
||||
|
||||
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:
|
||||
path = f"{self.directory}/library_{library_id}.page_{page}.json"
|
||||
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
data = json.load(f)
|
||||
except FileNotFoundError:
|
||||
data = ABSConnector._get_library_page(self, library_id, page, page_size)
|
||||
with open(path, "w+") as f:
|
||||
json.dump(data, f, indent=4)
|
||||
|
||||
yield from data["results"]
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import os
|
||||
from getpass import getpass
|
||||
import audible
|
||||
import json
|
||||
from getpass import getpass
|
||||
import os
|
||||
|
||||
|
||||
class AudibleConnector:
|
||||
|
|
@ -44,13 +44,22 @@ class AudibleConnector:
|
|||
|
||||
|
||||
class AudibleConnectorMock(AudibleConnector):
|
||||
def __init__(self, authFile):
|
||||
super().__init__(authFile)
|
||||
|
||||
self.directory = "dumps/audible"
|
||||
if not os.path.exists(self.directory):
|
||||
os.makedirs(self.directory)
|
||||
|
||||
def get_produce_from_asin(self, asin):
|
||||
path = f"{self.directory}/products_{asin}.json"
|
||||
|
||||
try:
|
||||
with open(f"dumps/products_{asin}.json", "r") as f:
|
||||
with open(path, "r") as f:
|
||||
data = json.load(f)
|
||||
return data["product"]
|
||||
except FileNotFoundError:
|
||||
data = AudibleConnector.get_produce_from_asin(self, asin)
|
||||
with open(f"dumps/products_{asin}.json", "w+") as f:
|
||||
with open(path, "w+") as f:
|
||||
json.dump({"product": data}, f, indent=4)
|
||||
return data
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from ratelimit import limits
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
class AudNexusConnector:
|
||||
|
|
@ -17,13 +18,22 @@ class AudNexusConnector:
|
|||
|
||||
|
||||
class AudNexusConnectorMock(AudNexusConnector):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self.directory = "dumps/audnexus"
|
||||
if not os.path.exists(self.directory):
|
||||
os.makedirs(self.directory)
|
||||
|
||||
def get_book_from_asin(self, book_asin):
|
||||
path = f"{self.directory}/book_{book_asin}.json"
|
||||
|
||||
try:
|
||||
with open(f"dumps/book_{book_asin}.json", "r") as f:
|
||||
with open(path, "r") as f:
|
||||
data = json.load(f)
|
||||
return data
|
||||
except FileNotFoundError:
|
||||
data = AudNexusConnector.get_book_from_asin(self, book_asin)
|
||||
with open(f"dumps/book_{book_asin}.json", "w+") as f:
|
||||
with open(path, "w+") as f:
|
||||
json.dump(data, f, indent=4)
|
||||
return data
|
||||
|
|
|
|||
15
main.py
15
main.py
|
|
@ -1,11 +1,11 @@
|
|||
import connectors
|
||||
import argparse
|
||||
import logging
|
||||
import config
|
||||
|
||||
logging.basicConfig(
|
||||
filename="log",
|
||||
filemode="w",
|
||||
level=logging.INFO,
|
||||
format="%(levelname)s - %(message)s",
|
||||
)
|
||||
|
||||
|
|
@ -198,10 +198,21 @@ if __name__ == "__main__":
|
|||
logging.getLogger("urllib3").setLevel(logging.WARNING)
|
||||
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-d", "--dev", action="store_true")
|
||||
parser.add_argument("-v", "--verbose", action="store_true")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.dev:
|
||||
abs = connectors.ABSConnectorMock(config.ABS_API_URL, config.ABS_API_TOKEN)
|
||||
audible = connectors.AudibleConnectorMock(config.AUDIBLE_AUTH_FILE)
|
||||
audnexus = connectors.AudNexusConnectorMock()
|
||||
else:
|
||||
abs = connectors.ABSConnector(config.ABS_API_URL, config.ABS_API_TOKEN)
|
||||
audible = connectors.AudibleConnector(config.AUDIBLE_AUTH_FILE)
|
||||
audnexus = connectors.AudNexusConnector()
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG if args.verbose else logging.INFO)
|
||||
main()
|
||||
|
|
|
|||
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue