|
5 | 5 | from concurrent.futures import ThreadPoolExecutor |
6 | 6 | from datetime import datetime |
7 | 7 | from itertools import chain |
| 8 | +from typing import Iterable |
8 | 9 |
|
9 | 10 | from ..clients.calibre import Calibre |
10 | 11 | from ..clients.hardcover import Hardcover |
@@ -64,43 +65,43 @@ def run(self) -> None: |
64 | 65 | logger.info("Finished murid cycle") |
65 | 66 |
|
66 | 67 | @staticmethod |
67 | | - def fetch_calibre_books(calibre: Calibre) -> list[Book]: |
| 68 | + def fetch_calibre_books(calibre: Calibre) -> set[Book]: |
68 | 69 | """Fetch books from the Calibre library.""" |
69 | 70 | books = calibre.get_books() |
70 | 71 | logger.info("Feched %d books from Calibre database", len(books)) |
71 | 72 | return books |
72 | 73 |
|
73 | 74 | @staticmethod |
74 | | - def fetch_hardcover_books(hardcover: list[Hardcover]) -> list[Book]: |
| 75 | + def fetch_hardcover_books(hardcover: list[Hardcover]) -> set[Book]: |
75 | 76 | """Fetch books from the Hardcover API.""" |
76 | 77 | with ThreadPoolExecutor(max_workers=min(10, len(hardcover))) as executor: |
77 | 78 | results = executor.map(lambda client: client.get_books(), hardcover) |
78 | | - books = list(chain.from_iterable(results)) |
| 79 | + books = set(chain.from_iterable(results)) |
79 | 80 | logger.info("Fetched %d books from Hardcover API", len(books)) |
80 | 81 | return books |
81 | 82 |
|
82 | 83 | @staticmethod |
83 | 84 | def match_books( |
84 | | - calibre_books: list[Book], |
85 | | - hardcover_books: list[Book], |
| 85 | + calibre_books: Iterable[Book], |
| 86 | + hardcover_books: Iterable[Book], |
86 | 87 | matcher: BookMatcher, |
87 | | - ) -> list[tuple[Book, Book, float]]: |
| 88 | + ) -> set[tuple[Book, Book, float]]: |
88 | 89 | """Match books from the Hardcover list against the Calibre library.""" |
89 | 90 | matches = matcher.match_books(calibre_books, hardcover_books) |
90 | 91 | logger.debug("%d books already present in Calibre library", len(matches)) |
91 | 92 | return matches |
92 | 93 |
|
93 | 94 | @staticmethod |
94 | 95 | def process_books( |
95 | | - present_books: list[tuple[Book, Book, float]], |
96 | | - wanted_books: list[Book], |
| 96 | + present_books: Iterable[tuple[Book, Book, float]], |
| 97 | + wanted_books: Iterable[Book], |
97 | 98 | torrent_discovery: TorrentDiscoveryService, |
98 | 99 | matcher: BookMatcher, |
99 | | - ) -> list[tuple[bytes, Book]]: |
| 100 | + ) -> set[tuple[bytes, Book]]: |
100 | 101 | """Process the matched and unmatched books to find torrents for the missing ones.""" |
101 | 102 | matched_ids = {h.id for _, h, _ in present_books} |
102 | | - missing_books = [h for h in wanted_books if h.id not in matched_ids] |
| 103 | + missing_books = {h for h in wanted_books if h.id not in matched_ids} |
103 | 104 |
|
104 | 105 | if not missing_books: |
105 | | - return [] |
| 106 | + return set() |
106 | 107 | return torrent_discovery.download_torrents(missing_books, matcher) |
0 commit comments