1919from perdoo .models .metadata import Format , Meta , Source , Tool
2020from perdoo .models .metron_info import InformationSource
2121from perdoo .services import Comicvine , League , Marvel , Metron
22- from perdoo .settings import OutputFormat , Settings
22+ from perdoo .settings import OutputFormat , Service , Settings
2323from perdoo .utils import Details , Identifications , get_metadata_id , list_files , sanitize
2424
2525LOGGER = logging .getLogger ("perdoo" )
@@ -47,14 +47,15 @@ def convert_collection(path: Path, output: OutputFormat) -> None:
4747 archive_type .convert (old_archive = archive )
4848
4949
50- def read_meta (archive : BaseArchive ) -> tuple [Meta , Details ]:
50+ def read_meta (archive : BaseArchive ) -> tuple [Meta | None , Details | None ]:
5151 filenames = archive .list_filenames ()
5252
5353 def read_meta_file (cls : type [InfoModel ], filename : str ) -> InfoModel | None :
5454 if filename in filenames :
5555 return cls .from_bytes (content = archive .read_file (filename = filename ))
5656 return None
5757
58+ # region Read Metadata
5859 try :
5960 metadata = read_meta_file (cls = Metadata , filename = "/Metadata.xml" ) or read_meta_file (
6061 cls = Metadata , filename = "Metadata.xml"
@@ -97,6 +98,9 @@ def read_meta_file(cls: type[InfoModel], filename: str) -> InfoModel | None:
9798 return meta , details
9899 except ValidationError :
99100 LOGGER .error ("%s contains an invalid Metadata file" , archive .path .name ) # noqa: TRY400
101+ # endregion
102+
103+ # region Read MetronInfo
100104 try :
101105 metron_info = read_meta_file (cls = MetronInfo , filename = "/MetronInfo.xml" ) or read_meta_file (
102106 cls = MetronInfo , filename = "MetronInfo.xml"
@@ -139,6 +143,9 @@ def read_meta_file(cls: type[InfoModel], filename: str) -> InfoModel | None:
139143 return Meta (date_ = date .today (), tool = Tool (value = "MetronInfo" )), details
140144 except ValidationError :
141145 LOGGER .error ("%s contains an invalid MetronInfo file" , archive .path .name ) # noqa: TRY400
146+ # endregion
147+
148+ # region Read ComicInfo
142149 try :
143150 comic_info = read_meta_file (cls = ComicInfo , filename = "/ComicInfo.xml" ) or read_meta_file (
144151 cls = ComicInfo , filename = "ComicInfo.xml"
@@ -151,11 +158,28 @@ def read_meta_file(cls: type[InfoModel], filename: str) -> InfoModel | None:
151158 return Meta (date_ = date .today (), tool = Tool (value = "ComicInfo" )), details
152159 except ValidationError :
153160 LOGGER .error ("%s contains an invalid ComicInfo file" , archive .path .name ) # noqa: TRY400
154-
155- return Meta (date_ = date .today (), tool = Tool (value = "Manual" )), Details (
156- series = Identifications (search = Prompt .ask ("Series title" , console = CONSOLE )),
157- issue = Identifications (),
158- )
161+ # endregion
162+
163+ return None , None
164+
165+
166+ def load_archives (
167+ path : Path , output : OutputFormat , force : bool = False
168+ ) -> list [tuple [Path , BaseArchive , Details | None ]]:
169+ archives = []
170+ with CONSOLE .status (f"Searching for { output } files" ):
171+ for file in list_files (path , f".{ output } " ):
172+ archive = get_archive (path = file )
173+ LOGGER .debug ("Reading %s" , file .stem )
174+ meta , details = read_meta (archive = archive )
175+ if not meta or not details :
176+ archives .append ((file , archive , details ))
177+ continue
178+ difference = abs (date .today () - meta .date_ )
179+ if force or meta .tool != Tool () or difference .days >= 28 :
180+ archives .append ((file , archive , details ))
181+ continue
182+ return archives
159183
160184
161185def fetch_from_services (
@@ -181,7 +205,15 @@ def fetch_from_services(
181205 LOGGER .warning ("No external services configured" )
182206 return None , None , None
183207
184- for service in (marvel , metron , comicvine , league ):
208+ services = {
209+ Service .COMICVINE : comicvine ,
210+ Service .LEAGUE_OF_COMIC_GEEKS : league ,
211+ Service .MARVEL : marvel ,
212+ Service .METRON : metron ,
213+ }
214+
215+ for service_name in settings .service_order :
216+ service = services [service_name ]
185217 if not service :
186218 continue
187219 LOGGER .info ("Fetching details from %s" , type (service ).__name__ )
@@ -243,6 +275,7 @@ def process_pages(
243275 from perdoo .models .metadata import Page as MetadataPage
244276 from perdoo .models .metron_info import Page as MetronPage
245277
278+ LOGGER .info ("Processing pages" )
246279 rename_images (folder = folder , filename = filename )
247280 image_list = list_files (folder , * IMAGE_EXTENSIONS )
248281 metadata_pages = set ()
@@ -272,16 +305,17 @@ def process_pages(
272305def start (settings : Settings , force : bool = False ) -> None :
273306 LOGGER .info ("Starting Perdoo" )
274307 convert_collection (path = settings .collection_folder , output = settings .output .format )
275- for file in list_files (settings .collection_folder , f".{ settings .output .format } " ):
276- archive = get_archive (path = file )
308+ archives = load_archives (
309+ path = settings .collection_folder , output = settings .output .format , force = force
310+ )
311+
312+ for file , archive , details in archives :
277313 CONSOLE .rule (file .stem )
278314 LOGGER .info ("Processing %s" , file .stem )
279- meta , details = read_meta (archive = archive )
280-
281- if not force :
282- difference = abs (date .today () - meta .date_ )
283- if meta .tool == Tool () and difference .days < 28 :
284- continue
315+ details = details or Details ( # noqa: PLW2901
316+ series = Identifications (search = Prompt .ask ("Series title" , console = CONSOLE )),
317+ issue = Identifications (),
318+ )
285319
286320 metadata , metron_info , comic_info = fetch_from_services (settings = settings , details = details )
287321 new_file = generate_filename (
@@ -293,7 +327,6 @@ def start(settings: Settings, force: bool = False) -> None:
293327 temp_folder = Path (temp_str )
294328 if not archive .extract_files (destination = temp_folder ):
295329 return
296- LOGGER .info ("Processing %s pages" , file .stem )
297330 process_pages (
298331 folder = temp_folder ,
299332 metadata = metadata ,
0 commit comments