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:
- Sync the MDPI table-of-contents (TOC) XML from the MDPI FTP server, which is used to map ISSN → MDPI journal abbreviation.
- Build (or load from cache) the ISSN → abbreviation mapping, used to compute the expected zip filename for each article.
- 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:
flow.pytasks.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 isFalse -
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_ftpto refresh the MDPI TOC XML,mdpi_isbn_abbreviation_mappingto build the ISSN→abbreviation mapping (using a JSON cache when possible),select_journals_to_harvestto filter that mapping down to only the journals in the current config, andsync_mdpi_files_from_ftp_serverto download any new content zips for those journals. Finally, it callsretrieve_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
@taskthat runsrclone copyto fetchall_journal_toc.xmlfrom 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 mappingissn_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. DefaultFalse -
Context for Use:
-
A Prefect
@taskthat 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 inissn_list, the cache is used as-is; otherwise the mapping is rebuilt from the TOC XML (viaparse_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
@taskthat filters the full ISSN→abbreviation mapping down to only the journals listed inissn_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 byselect_journals_to_harvest -
update_skiplist(bool): Whether to append newly-copied zip filenames to the persistent skip-list file. DefaultTrue -
Context for Use:
-
A Prefect
@taskthat, for each journal injournal_dict, lists all remote zip files matching<abbreviation>-*.zipon 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 localmdpi_newdownload root usingrclone copy --files-from. Ifupdate_skiplistisTrue, 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 mappingprevious_module(str): Name of the previous moduleissn_list(list): ISSNs to processcurrent_datetime(dict): Dictionary containing the current date and timestamp-
test_copy(bool): Flag to indicate if it is a test run. DefaultFalse -
Context for Use:
-
This is the main Prefect
@taskthat performs the per-article work, following the standardprevious_module/temp-<module>folder convention. For each article, it checks whether the content was already harvested under an earlier previous-module version (viais_full_text_already_harvested, skipping if so), then opens aZipHandleras a context manager to locate, open, and validate the article's expected content zip. If theZipHandlerreports a fatal issue, an error CSV entry is written (withzip_path,error_category, anderror_messagecustom fields) and the module folder becomes a stop folder. Otherwise, the validated PDF, XML, and image members are copied intopdf/,xml/, andimages/subfolders respectively, and ahistory.csventry is written. -
Example Usage:
-
This function is called directly from
flow.pyas 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 toget_run_logger()or a standardlogging.Loggerif 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 readsmetadata-article.jsonto derive the article's ISSN, MDPI abbreviation, zero-padded volume/issue, and zero-padded starting page number (parsed from the URL inresource.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()— returnsTrueif any fatal issue was recorded diagnostics()— returns aZipDiagnosticsdataclass summarizing all issues found, for statistics/loggingget_first_fatal()— returns the(ZipIssue, message)tuple for the first fatal issue encountered, used to populate the error CSVget_pdf_member()/get_xml_member()/get_image_members()/get_other_members()— return the validated member name(s) inside the zipcopy_member(member_name, target_path)— stream-copies a single member out of the zip intotarget_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
ZipHandlercan detect. All are treated as fatal (stop-processing) exceptIMAGES_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 byZipHandler.diagnostics(). Consumed byflow.py(viaretrieve_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/journalnodes and builds a{issn: abbreviation}dict from each node'sissnandabbrchild elements. This is the "recreate from XML" path used bymdpi_isbn_abbreviation_mappingwhen 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
Trueif every ISSN inissn_listis present as a key inabbrev_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_imagesindicator, to skip redundant re-downloading.