Zum Inhalt

crossrefcrawler

Overview

The crossrefcrawler module is the entry point of the entire harvesting pipeline for most journals, it queries the CrossRef API for a journal's metadata and, for each article deposited in that journal, creates the article's initial directory structure and writes its raw CrossRef metadata (metadata-article.json). Almost every other module, depends on the output of this module.

Unlike downstream modules, crossrefcrawler does not follow the previous_module chain, it is the first module, always. It also implements incremental, resumable harvesting per journal: rather than re-fetching a journal's entire CrossRef history on every run, it tracks which years have already been fully harvested (and the exact date of the last successful run) in a per-journal metafile, and only fetches the date range that's actually new since the last run.

Harvest granularity is per calendar year. For each year CrossRef reports as having deposits for a journal, the module determines one of four harvest strategies (see HarvestMode) depending on whether that year is the current year, and whether the module already ran during the current year:

  • Past year, never harvested: fetch the entire year (FULL_YEAR)
  • Past year, harvested up through the last run (which was also in that year): fetch only from the last run date to the end of that year (LAST_RUN_TIME_TO_END_YEAR)
  • Current year, first run this year: fetch from January 1st of the current year up to yesterday (BEGINNING_YEAR_TO_YESTERDAY)
  • Current year, already ran earlier this year: fetch only from the last run date up to yesterday (LAST_RUN_TIME_TO_YESTERDAY)

Fully-harvested past years are skipped entirely on subsequent runs, and the current year is never marked "fully harvested" (since it's still ongoing), it's always re-checked against the last run date instead.

The module also flags entries that CrossRef returns but that don't actually look like article records (see add_stop_processing_metadata), so that downstream modules can recognize and skip them via the standard STOP_PROCESSING_META_FIELD.

The module contains two primary Python files:

  1. flow.py
  2. tasks.py

flow.py

This file defines the main flow for CrossRef metadata harvesting using the Prefect library, and uses prefect's get_run_logger to log various stages along with the module context. It resolves the module context and any configured metadata filters, then delegates all per-journal and per-article work to tasks.get_metadata_journal_works.

crossrefcrawler()

  • Required Parameters:

  • name_of_config_file (str): Name of the configuration file.

  • Context for Use:

  • This function is the main entry point. It resolves the module context (which journals to harvest) and the metadata filters configured for this run (via get_metadata_filters, e.g. CrossRef API filter parameters beyond the date range), then calls tasks.get_metadata_journal_works with both.

  • Example Usage:

  • python crossrefcrawler('example-config.json')

  • Example Output:

  • INFO: Path to log file: /mnt/.../crossrefcrawler/....log INFO: Module name: crossrefcrawler INFO: Set base path to: /mnt/.../wdm-test INFO: Set temp base path to: /mnt/.../wdm-test INFO: Module context: [{'ISSN': '2948-2143', 'position': 0, ...}] INFO: Root directory for harvesting: /mnt/.../Automatische_Prozessierung INFO: Start crossref metadata retrieval for ISSN: 2948-2143. INFO: For journal 2948-2143, crossrefcrawler is the first module in the workflow as expected. INFO: Journal metadata file .../journal-metadata.json created successfully INFO: Finished harvesting for articles deposited in the year 2025. INFO: No. of successfully processed journals: 1

tasks.py

This file contains the task definitions used in the CrossRef harvesting flow. It handles fetching journal- and article-level metadata from the CrossRef REST API, deciding which date ranges to fetch per year, creating each article's initial directory and metadata file, and tracking harvesting progress in a per-journal metafile.

get_metadata_journal_works()

  • Required Parameters:

  • module_context (list): The resolved module context (journals to process)

  • module_name (str): Name of the current module
  • current_datetime (dict): Dictionary containing the current date and timestamp
  • metadata_filters (dict): Additional CrossRef API filter parameters from the config file

  • Context for Use:

  • This is the main Prefect @task and entry point for all per-journal processing. For each journal in module_context, it first verifies this journal is configured with crossrefcrawler as its first module (position 0) — if not, it logs an error and skips the journal (since this module cannot build on existing previous-module data). It then fetches and saves the journal-level metadata (write_metadata_journal_file), determines which years CrossRef reports deposits for, and for each year selects and executes one of the four harvest strategies described in the Overview via fetch_save_articles_metadata. On success, all created temp article directories for that year are renamed to their permanent (non-temp) names, and the metafile is updated to record that year as harvested. After all years for a journal are processed, the metafile records the successful run timestamp. A summary of which ISSNs succeeded and which failed is logged at the end.

  • Example Usage:

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

write_metadata_journal_file()

  • Required Parameters:

  • issn (str): The journal's ISSN

  • Context for Use:

  • Fetches journal-level metadata from the CrossRef API (Journals().journal(issn=issn)) and writes it to journal-metadata.json in the journal's directory (creating the directory if it doesn't exist). Returns the fetched metadata dict, or None if CrossRef returned nothing.

fetch_save_articles_metadata()

  • Required Parameters:

  • issn (str): The journal's ISSN

  • year (int): The year to fetch
  • module_name (str): Name of the current module
  • current_datetime (dict): Dictionary containing the current date and timestamp
  • harvest_mode (HarvestMode): Which of the four date-range strategies to use
  • metadata_filters (dict): Additional CrossRef API filter parameters
  • last_run (optional): The last successful run date, required for the two LAST_RUN_TIME_* harvest modes

  • Context for Use:

  • Fetches the CrossRef works matching the given ISSN, year, and harvest mode (via get_articles_metadata), and for each work, flags it if it doesn't look like a genuine article (add_stop_processing_metadata) and creates its initial temp directory and metadata file (initiate_article_space). Returns a de-duplicated list of the created temp module directory paths.

initiate_article_space()

  • Required Parameters:

  • item (dict): A single CrossRef work record

  • issn (str): The journal's ISSN
  • module_name (str): Name of the current module
  • current_datetime (dict): Dictionary containing the current date and timestamp
  • journal_crossref_url (str): The CrossRef API query URL used to fetch this batch of works, recorded as the Input in history.csv

  • Context for Use:

  • Creates the article's temp module directory (named temp-crossrefcrawler_<deposit_date>, where the deposit date is derived from the CrossRef deposited timestamp on the item), removing any stale temp directory of the same name left over from an earlier failed run. Writes the raw CrossRef work record as metadata-article.json, and writes a history.csv entry recording the CrossRef query URL as input and metadata-article.json as output. Returns the created temp directory path — this is later renamed to its permanent name by rename_temp_dir once the whole year's harvest completes successfully.

add_stop_processing_metadata()

  • Required Parameters:

  • item (dict): A single CrossRef work record (mutated in place)

  • Context for Use:

  • Sets item[constants.STOP_PROCESSING_META_FIELD] to True when the CrossRef record doesn't look like a genuine article, either because its type field is 'journal' rather than 'journal-article', or because none of the typical article-indicator fields (abstract, page, container-title) are present. This flag is written into the article's metadata-article.json alongside the rest of the CrossRef data, for downstream modules to check.


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

get_metadata_journal()

  • Required Parameters:

  • issn (str): The journal's ISSN

  • Context for Use:

  • Thin wrapper around the crossref.restful.Journals().journal(issn=issn) call, returning the raw journal metadata dict from CrossRef.

get_available_years()

  • Required Parameters:

  • metadata_journal (dict): The journal-level metadata dict from CrossRef

  • Context for Use:

  • Extracts the list of years for which CrossRef reports at least one deposit for this journal, from the breakdowns.dois-by-issued-year field, sorted ascending.

get_articles_metadata()

  • Required Parameters:

  • issn, year, harvest_mode, metadata_filters, last_run (optional)

  • Context for Use:

  • Builds and returns the appropriate crossref.restful Journals().works(issn).filter(...) query object for the given harvest_mode, setting from_deposit_date/until_deposit_date accordingly (see the four strategies described in the Overview). Any additional filter parameters from the config file are passed through via **metadata_filters.

get_metafile_path() / get_metafile_data()

  • Required Parameters:

  • issn (str): The journal's ISSN

  • Context for Use:

  • get_metafile_path returns the path to the journal's harvesting-progress metafile (constants.METAFILE_NAME) inside its journal directory. get_metafile_data reads and returns its JSON contents, or None if the file doesn't exist yet (i.e. this journal has never been successfully harvested before).

is_year_already_harvested()

  • Required Parameters:

  • issn (str), year (int)

  • Context for Use:

  • Checks whether the given year already appears in the metafile's years_harvested list, meaning it can be safely skipped on this run.

get_last_run_year()

  • Required Parameters:

  • issn (str)

  • Context for Use:

  • Returns the year portion of the metafile's crossref_last_run timestamp, or -1 if the metafile doesn't exist or has no recorded last run, used to decide which of the two LAST_RUN_TIME_* harvest modes applies.

is_current_year()

  • Required Parameters:

  • year (int)

  • Context for Use:

  • Returns True if the given year is the current calendar year. Used both to select the harvest mode and to prevent update_metafile from ever marking the current (still-ongoing) year as fully harvested.

update_metafile()

  • Required Parameters:

  • issn (str)

  • action (MetaFileUpdateActions): Either ADD_YEAR (append a completed year to years_harvested) or ADD_LAST_SUCCESSFUL_RUN_TIME (record the current run's timestamp as crossref_last_run)
  • year (int, required only for ADD_YEAR)

  • Context for Use:

  • Updates the journal's metafile after a successful harvest step. ADD_YEAR is a no-op (returns immediately) if the given year is the current year, since the current year should never be treated as fully complete.

rename_temp_dir()

  • Required Parameters:

  • temp_dir_path (pathlib.Path): The temp module directory to rename

  • Context for Use:

  • Strips the temp- prefix (constants.TEMP_DIRECTORY_IDENTIFIER) from the directory name and renames it to its permanent form. Called once per created article directory, after the entire year's harvest for that journal has completed successfully (unlike downstream modules, there is no per-article rename-on-success, the rename happens in bulk at the end of a successful year).