Skip to content

Base Downloader

BaseGTFSDownloader

BaseGTFSDownloader(api_key: Optional[str] = None)

Bases: ABC

Abstract base class for a GTFS catalog/source downloader client.

Subclasses must implement search_feeds(). download_feeds() is shared across all sources and downloads whatever GTFSFeedMetadata list is passed to it.

Attributes:

Name Type Description
api_key

The API key used to authenticate with the source, or None if the source requires no authentication.

Initialize the downloader with an API key.

Parameters:

Name Type Description Default
api_key Optional[str]

API key for the source. If not provided, it is resolved via downloaders.utils.config.get_api_key: the API_KEY_ENV_VAR environment variable, then a local API keys file, if either is set up.

None
Source code in pyGTFSHandler/downloaders/base.py
def __init__(self, api_key: Optional[str] = None):
    """Initialize the downloader with an API key.

    Args:
        api_key: API key for the source. If not provided, it is
            resolved via `downloaders.utils.config.get_api_key`: the
            `API_KEY_ENV_VAR` environment variable, then a local API
            keys file, if either is set up.
    """
    self.api_key = get_api_key(self.SOURCE_NAME, api_key, self.API_KEY_ENV_VAR)

search_feeds abstractmethod

search_feeds(*args, **kwargs) -> List[GTFSFeedMetadata]

Search the source's catalog for GTFS feeds.

Returns:

Type Description
List[GTFSFeedMetadata]

A list of GTFSFeedMetadata describing the matching feeds.

Source code in pyGTFSHandler/downloaders/base.py
@abstractmethod
def search_feeds(self, *args, **kwargs) -> List[GTFSFeedMetadata]:
    """Search the source's catalog for GTFS feeds.

    Returns:
        A list of `GTFSFeedMetadata` describing the matching feeds.
    """
    raise NotImplementedError

download_feeds

download_feeds(feeds: List[GTFSFeedMetadata], download_folder: str, overwrite: bool = False, unzip: bool = True) -> List[str]

Download previously found feeds to a local folder.

Parameters:

Name Type Description Default
feeds List[GTFSFeedMetadata]

Feeds to download, as returned by search_feeds().

required
download_folder str

Directory to store (and, if unzip, extract) the downloaded feeds into.

required
overwrite bool

If True, re-download and replace files that already exist on disk.

False
unzip bool

If True, extract each ZIP after downloading and delete the ZIP file.

True

Returns:

Type Description
List[str]

Absolute paths to the downloaded feeds (ZIP files, or

List[str]

extracted folders if unzip is True).

Source code in pyGTFSHandler/downloaders/base.py
def download_feeds(
    self,
    feeds: List[GTFSFeedMetadata],
    download_folder: str,
    overwrite: bool = False,
    unzip: bool = True,
) -> List[str]:
    """Download previously found feeds to a local folder.

    Args:
        feeds: Feeds to download, as returned by `search_feeds()`.
        download_folder: Directory to store (and, if `unzip`,
            extract) the downloaded feeds into.
        overwrite: If True, re-download and replace files that
            already exist on disk.
        unzip: If True, extract each ZIP after downloading and delete
            the ZIP file.

    Returns:
        Absolute paths to the downloaded feeds (ZIP files, or
        extracted folders if `unzip` is True).
    """
    return download_feeds(
        feeds, download_folder, overwrite=overwrite, unzip=unzip
    )