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