Refactor process_audible_serie to improve handling of non-series books and enhance logging for missing books
This commit is contained in:
parent
6e151a8d28
commit
043f57842a
1 changed files with 28 additions and 31 deletions
59
main.py
59
main.py
|
|
@ -56,39 +56,17 @@ def expand_range(part):
|
|||
return [] # Handle non-numeric input or invalid format
|
||||
|
||||
|
||||
def process_sequence(books):
|
||||
"""Groups books by ASIN, handling sequence ranges (including floats)."""
|
||||
books_sequence = {}
|
||||
for book in books:
|
||||
asin = book["asin"]
|
||||
sequence = book.get("sequence", "")
|
||||
|
||||
if sequence:
|
||||
keys = expand_range(sequence.split(", ")[0])
|
||||
else:
|
||||
keys = [float(book.get("sort", "1")) * -1]
|
||||
|
||||
for key in keys:
|
||||
if key not in books_sequence:
|
||||
books_sequence[key] = []
|
||||
books_sequence[key].append(asin)
|
||||
|
||||
keys = sorted(books_sequence.keys(), key=lambda x: float(x))
|
||||
ordered_sequence = {}
|
||||
for key in keys:
|
||||
ordered_sequence[key] = books_sequence[key]
|
||||
return ordered_sequence
|
||||
|
||||
|
||||
def process_audible_serie(books, serie_name):
|
||||
processed_books = BookCollection(serie_name)
|
||||
|
||||
for json in books:
|
||||
if book["relationship_type"] == "series":
|
||||
if json["relationship_type"] == "series":
|
||||
book = Book(json["asin"])
|
||||
book.series.setdefault(serie_name, json["sequence"])
|
||||
book.series.setdefault(serie_name, f"-{json['sort']}")
|
||||
processed_books.add(book)
|
||||
else:
|
||||
logger.debug("Skipping non-series book: %s", json["asin"])
|
||||
|
||||
return processed_books
|
||||
|
||||
|
|
@ -169,7 +147,9 @@ def main():
|
|||
continue
|
||||
|
||||
audible_serie = audible.get_produce_from_asin(series_asin)
|
||||
audible_book_sequence = process_sequence(audible_serie["relationships"])
|
||||
audible_book_sequence = process_audible_serie(
|
||||
audible_serie["relationships"], series_name
|
||||
)
|
||||
|
||||
if len(abs_book_sequence) >= len(audible_book_sequence):
|
||||
continue
|
||||
|
|
@ -187,13 +167,30 @@ def main():
|
|||
soon_to_release_books = []
|
||||
|
||||
for key in missing_keys:
|
||||
try:
|
||||
audnexus.get_book_from_asin(audible_book_sequence[key][0])
|
||||
missing_books.append(key)
|
||||
found = False
|
||||
for asin in audible_book_sequence[key]:
|
||||
try:
|
||||
audnexus.get_book_from_asin(asin)
|
||||
missing_books.append(key)
|
||||
logger.debug(
|
||||
"%s Book %.1f is missing - %s",
|
||||
series_name,
|
||||
key,
|
||||
audible_book_sequence[key][0],
|
||||
)
|
||||
found = True
|
||||
break
|
||||
except requests.exceptions.HTTPError:
|
||||
pass
|
||||
|
||||
except requests.exceptions.HTTPError:
|
||||
logger.debug("%s Book %d is yet to be released", series_name, key)
|
||||
if not found:
|
||||
soon_to_release_books.append(key)
|
||||
logger.debug(
|
||||
"%s Book %d is yet to be released - %s",
|
||||
series_name,
|
||||
key,
|
||||
audible_book_sequence[key][0],
|
||||
)
|
||||
|
||||
msgs = []
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue