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:
flow.pytasks.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 callstasks.get_metadata_journal_workswith 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 modulecurrent_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
@taskand entry point for all per-journal processing. For each journal inmodule_context, it first verifies this journal is configured withcrossrefcrawleras its first module (position0) — 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 viafetch_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 tojournal-metadata.jsonin the journal's directory (creating the directory if it doesn't exist). Returns the fetched metadata dict, orNoneif CrossRef returned nothing.
fetch_save_articles_metadata()
-
Required Parameters:
-
issn(str): The journal's ISSN year(int): The year to fetchmodule_name(str): Name of the current modulecurrent_datetime(dict): Dictionary containing the current date and timestampharvest_mode(HarvestMode): Which of the four date-range strategies to usemetadata_filters(dict): Additional CrossRef API filter parameters-
last_run(optional): The last successful run date, required for the twoLAST_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 ISSNmodule_name(str): Name of the current modulecurrent_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 theInputinhistory.csv -
Context for Use:
-
Creates the article's temp module directory (named
temp-crossrefcrawler_<deposit_date>, where the deposit date is derived from the CrossRefdepositedtimestamp 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 asmetadata-article.json, and writes ahistory.csventry recording the CrossRef query URL as input andmetadata-article.jsonas output. Returns the created temp directory path — this is later renamed to its permanent name byrename_temp_dironce 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]toTruewhen the CrossRef record doesn't look like a genuine article, either because itstypefield 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'smetadata-article.jsonalongside 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-yearfield, 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.restfulJournals().works(issn).filter(...)query object for the givenharvest_mode, settingfrom_deposit_date/until_deposit_dateaccordingly (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_pathreturns the path to the journal's harvesting-progress metafile (constants.METAFILE_NAME) inside its journal directory.get_metafile_datareads and returns its JSON contents, orNoneif 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_harvestedlist, 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_runtimestamp, or-1if the metafile doesn't exist or has no recorded last run, used to decide which of the twoLAST_RUN_TIME_*harvest modes applies.
is_current_year()
-
Required Parameters:
-
year(int) -
Context for Use:
-
Returns
Trueif the given year is the current calendar year. Used both to select the harvest mode and to preventupdate_metafilefrom ever marking the current (still-ongoing) year as fully harvested.
update_metafile()
-
Required Parameters:
-
issn(str) action(MetaFileUpdateActions): EitherADD_YEAR(append a completed year toyears_harvested) orADD_LAST_SUCCESSFUL_RUN_TIME(record the current run's timestamp ascrossref_last_run)-
year(int, required only forADD_YEAR) -
Context for Use:
-
Updates the journal's metafile after a successful harvest step.
ADD_YEARis 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).