Zum Inhalt

mdpi_with_images

Overview

The mdpi_with_images module retrieves full-text PDF, XML, and image files for MDPI articles from an MDPI-provided FTP/rclone remote, and sorts them into pdf/, xml/, and images/ subfolders in the article's temp module directory. It is a more strictly-validated counterpart to the plain mdpi module — the key addition is retrieving the supplementary image files alongside the PDF and XML, and performing much stricter per-zip content validation before accepting a download as complete.

Before processing any articles, the flow performs three preparatory steps:

  1. Sync the MDPI table-of-contents (TOC) XML from the MDPI FTP server, which is used to map ISSN → MDPI journal abbreviation.
  2. Build (or load from cache) the ISSN → abbreviation mapping, used to compute the expected zip filename for each article.
  3. Sync the actual content zip files for the journals being processed from a second rclone remote, using a persistent skip-list file so already-downloaded zips are not re-fetched.

Only after this sync step does the flow iterate articles and unpack their per-article content from the now-locally-cached zip files.

Module ordering: mdpi_with_images should run only after metadata-article.json is created.

The module contains two primary Python files:

  1. flow.py
  2. tasks.py

flow.py

This file defines the main flow for MDPI content retrieval (with images). Most of the per-article iteration logic is delegated to the retrieve_and_process_mdpi_articles task in tasks.py; flow.py itself is mainly responsible for the three preparatory sync/mapping steps described in the Overview.

mdpi_with_images()

  • Required Parameters:

  • name_of_config_file (str): Name of the configuration file. Default is "config-mdpi_with_images_test_shadman.json"

  • test_copy (bool): Flag to indicate if it is a test run. Default is False

  • Context for Use:

  • This function is the main entry point. It resolves the module context and ISSN list, then calls (in order) sync_tocs_from_mdpi_ftp to refresh the MDPI TOC XML, mdpi_isbn_abbreviation_mapping to build the ISSN→abbreviation mapping (using a JSON cache when possible), select_journals_to_harvest to filter that mapping down to only the journals in the current config, and sync_mdpi_files_from_ftp_server to download any new content zips for those journals. Finally, it calls retrieve_and_process_mdpi_articles, which performs the actual per-article unpacking and validation.

  • Example Usage:

  • python mdpi_with_images('config-mdpi_with_images_test_shadman.json', test_copy=False)

  • Example Output:

  • INFO: Module name: mdpi_with_images INFO: Set base path to: /mnt/.../ INFO: Module context: [{'ISSN': '2673-4532', 'position': 3, 'previous_module': 'validate_metadata', ...}] INFO: ISSN list: ['2673-4532'] INFO: Created mapping for 412 journals INFO: Copying Journal Actuators (ISSN 2076-0825) INFO: Have to work on 1 journals INFO: Start MDPI article files retrieval for ISSN: 2673-4532. INFO: Fetched files for 8 out of 10 articles in journal: /mnt/.../2673-4532--2026-01-10-...

tasks.py

This file contains the task definitions used in the MDPI-with-images flow. It handles syncing the TOC and content zip files via rclone, computing the ISSN-to-abbreviation mapping, opening and validating each article's content zip, and copying the validated members into the article's temp module directory.

sync_tocs_from_mdpi_ftp()

  • Context for Use:

  • A Prefect @task that runs rclone copy to fetch all_journal_toc.xml from the MDPI FTP remote (mdpi_server:MDPI_corpus_xmls) into the local TOC cache folder, using checksum comparison so unchanged files aren't re-copied. Returns the local path to the synced TOC file.

mdpi_isbn_abbreviation_mapping()

  • Required Parameters:

  • toc_file (path): Path to the MDPI TOC XML file

  • dump_file (path, optional): Path to a JSON cache of the ISSN→abbreviation mapping
  • issn_list (list, optional): The ISSNs that must be present in the cache for it to be considered valid
  • recreate (bool): Force rebuilding the mapping from the TOC XML regardless of cache state. Default False

  • Context for Use:

  • A Prefect @task that returns a dict mapping each journal's ISSN to its MDPI abbreviation (e.g. Actuators → act), used later to compute expected zip and file names. If a valid cache file already contains all ISSNs in issn_list, the cache is used as-is; otherwise the mapping is rebuilt from the TOC XML (via parse_mdpi_abbreviation_mapping_xml) and the cache file is rewritten.

select_journals_to_harvest()

  • Required Parameters:

  • issn_list (list): ISSNs to process, from the current module config

  • issn_folder_dictionary (dict): The full ISSN→abbreviation mapping

  • Context for Use:

  • A Prefect @task that filters the full ISSN→abbreviation mapping down to only the journals listed in issn_list, returning a dict of {issn: journal_abbreviation} for those journals only. This dict is then used both to enumerate which journal folders need syncing (via their abbreviation-based folder names) and to look up each individual article's zip location.

sync_mdpi_files_from_ftp_server()

  • Required Parameters:

  • journal_dict (dict): {issn: journal_abbreviation}, as produced by select_journals_to_harvest

  • update_skiplist (bool): Whether to append newly-copied zip filenames to the persistent skip-list file. Default True

  • Context for Use:

  • A Prefect @task that, for each journal in journal_dict, lists all remote zip files matching <abbreviation>-*.zip on the content rclone remote (sub12:), subtracts filenames already present in the skip-list file (utils.get_path_mdpi_skiplist()), and copies any remaining new zip files to the local mdpi_new download root using rclone copy --files-from. If update_skiplist is True, the newly-copied filenames are appended to the skip-list so they are not re-downloaded on future runs.

retrieve_and_process_mdpi_articles()

  • Required Parameters:

  • module_name (str): Name of the current module

  • module_context (list): The resolved module context (journals + previous-module info)
  • abbrev_mapping (dict): The full ISSN→abbreviation mapping
  • previous_module (str): Name of the previous module
  • issn_list (list): ISSNs to process
  • current_datetime (dict): Dictionary containing the current date and timestamp
  • test_copy (bool): Flag to indicate if it is a test run. Default False

  • Context for Use:

  • This is the main Prefect @task that performs the per-article work, following the standard previous_module / temp-<module> folder convention. For each article, it checks whether the content was already harvested under an earlier previous-module version (via is_full_text_already_harvested, skipping if so), then opens a ZipHandler as a context manager to locate, open, and validate the article's expected content zip. If the ZipHandler reports a fatal issue, an error CSV entry is written (with zip_path, error_category, and error_message custom fields) and the module folder becomes a stop folder. Otherwise, the validated PDF, XML, and image members are copied into pdf/, xml/, and images/ subfolders respectively, and a history.csv entry is written.

  • Example Usage:

  • This function is called directly from flow.py as the final step; it is not typically invoked standalone.


The lower-level helper functions and classes in tasks.py are listed below.

ZipHandler (class)

  • Constructor Parameters:

  • current_module_temp_dir (pathlib.Path): Path to the current temp module directory

  • abbrev_mapping (dict): The full ISSN→abbreviation mapping
  • logger (optional): A logger instance; falls back to get_run_logger() or a standard logging.Logger if not provided

  • Context for Use:

  • This is the core validation engine of the module, designed to be used as a context manager (with ZipHandler(...) as zip_handler:) so the underlying zip file is always closed. On construction, it reads metadata-article.json to derive the article's ISSN, MDPI abbreviation, zero-padded volume/issue, and zero-padded starting page number (parsed from the URL in resource.primary.URL), from which it computes both the expected zip file name (<abbrev>-<VV>-<II>.zip) and the expected per-file prefix (<abbrev>-<VV>-<PPPPP>) that valid members inside that zip must start with. It then locates and opens the zip file (if found), and scans its members, classifying each as PDF, XML, image (.jpg/.jpeg/.png/.tif/.tiff), or unexpected-other based on extension, filtering only to members whose filename starts with the computed prefix.

  • Validation rules: exactly one PDF and exactly one XML member are required (missing or duplicate counts are both fatal); zero or more image members are allowed (missing images is recorded but not fatal); any unexpected-other file matching the prefix is fatal. A fatal issue sets stop_processing = True, meaning the calling flow will route the article into a stop folder.

  • Key methods:

  • needs_stop_processing() — returns True if any fatal issue was recorded

  • diagnostics() — returns a ZipDiagnostics dataclass summarizing all issues found, for statistics/logging
  • get_first_fatal() — returns the (ZipIssue, message) tuple for the first fatal issue encountered, used to populate the error CSV
  • get_pdf_member() / get_xml_member() / get_image_members() / get_other_members() — return the validated member name(s) inside the zip
  • copy_member(member_name, target_path) — stream-copies a single member out of the zip into target_path, creating the target directory if needed
  • get_zip_path() / get_zip_name() / get_prefix() — expose the resolved zip location, zip filename, and expected file-name prefix

  • Example Usage:

  • python with ZipHandler(current_module_temp_dir, abbrev_mapping, logger=logger) as zip_handler: if not zip_handler.needs_stop_processing(): pdf_path = zip_handler.copy_member(zip_handler.get_pdf_member(), target_dir / "pdf")

ZipIssue (enum)

  • Values: ZIP_NOT_FOUND, MISSING_PDF, MISSING_XML, TOO_MANY_PDF, TOO_MANY_XML, UNEXPECTED_OTHERS, IMAGES_MISSING

  • Context for Use:

  • Enumerates every validation issue ZipHandler can detect. All are treated as fatal (stop-processing) except IMAGES_MISSING, which is recorded for statistics but does not block the article from being processed (image files are optional per the module's spec).

ZipDiagnostics (dataclass)

  • Fields: zip_not_found, missing_pdf, missing_xml, too_many_pdf, too_many_xml, image_files_missing, unexpected_other_file, unexpected_other_count, pdf_count, xml_count, image_count, others

  • Context for Use:

  • A frozen, immutable snapshot of a ZipHandler's validation outcome, returned by ZipHandler.diagnostics(). Consumed by flow.py (via retrieve_and_process_mdpi_articles) to populate the various per-journal statistics counters and article-name lists used in the end-of-journal summary logging.

get_mdpi_download_root() / get_mdpi_with_images_download_root()

  • Context for Use:

  • get_mdpi_download_root() returns the folder where the MDPI TOC XML and abbreviation cache are stored (<data_root_not_backed_up>/<DOWNLOADED_DATA_DIRECTORY>/mdpi). get_mdpi_with_images_download_root() returns the separate folder where content zip files are synced to (.../mdpi_new) — note these are two distinct download roots used for different purposes within the same module.

get_mdpi_toc_file_path() / get_abbrevcache_path()

  • Context for Use:

  • Return the expected local paths of the TOC XML file and the abbreviation-mapping JSON cache, respectively, both under get_mdpi_download_root().

parse_mdpi_abbreviation_mapping_xml()

  • Required Parameters:

  • toc_file (path): Path to the MDPI TOC XML file

  • logger: Logger instance for progress reporting

  • Context for Use:

  • Parses the TOC XML's /journals/journal nodes and builds a {issn: abbreviation} dict from each node's issn and abbr child elements. This is the "recreate from XML" path used by mdpi_isbn_abbreviation_mapping when the cache is missing, stale, or forced to rebuild.

all_issns_in_mdpi_abbrev_list()

  • Required Parameters:

  • issn_list (list): ISSNs to check

  • abbrev_dict (dict): The cached ISSN→abbreviation mapping

  • Context for Use:

  • Returns True if every ISSN in issn_list is present as a key in abbrev_dict. Used to decide whether a cached abbreviation mapping is still sufficient, or whether it needs to be rebuilt from the TOC XML because it's missing one or more requested journals.

is_full_text_already_harvested()

  • Required Parameters:

  • module_name (str): Name of the current module

  • previous_module (str): Name of the previous module
  • article_dir_path (pathlib.Path): Path to the article directory

  • Context for Use:

  • Checks whether any previous-module version of this article (not just the latest one) already carries a processed-by-module-mdpi_with_images indicator, to skip redundant re-downloading.