Skip to content

Extract

Runs the ETL extraction phase. Downloads data from external sources (API, email, database) and saves them locally as configured in the datapackage.

# Run extract using the default datapackage.yaml descriptor
dpetl extract

# Extract emails received today only
dpetl extract --today-email   # or -t

# Include package name in email subject search pattern
dpetl extract --add-package-name   # or -a

# Add a delay between resource extractions (in seconds)
dpetl extract --delay 5   # or -d 5

Each resource in the descriptor must declare an extraction mode (email, api, cli or sql) inside its dpetl_extract property (see Example). If a resource is missing this property, the whole package extraction stops immediately.

Tip: Set dpetl_extract: false on a package or resource to skip it.

API Extractor

Reads the request settings from the resource's sources property:

  • method: required (e.g. get, post).

  • path: required. Treated as a base URL, not the full file URL.

    dpetl appends the file name from resource.path to it (e.g. https://api.example.com + data/invoices.csv → https://api.example.com/invoices.csv).

  • timeout: optional, in seconds (defaults to 30).

  • params, headers, stream: optional, passed directly to the request.

Once the URL is built, dpetl makes the request and saves the response to resource.path.

If the resource declares extrapaths (multiple files for the same resource), the same steps run once per path, reusing the same base URL.

Email Extractor

Connects to your IMAP server using environment variables (EMAIL_USER, EMAIL_PWD, EMAIL_IMAP), applying proxy settings (HTTP_PROXY/HTTPS_PROXY) if set.

It then searches for the most recent e-mail matching criteria:

  • subject: if you don't set it in the datapackage (criteria.subject), it defaults to the resource name (or {package_name}_{resource_name} with --add-package-name).

  • date_gte: if you don't set it in the datapackage (criteria.date_gte), it defaults to the most recent e-mail (no date filter). Passing --today-email sets it to today's date.

  • any other filter supported by imap-tools (sender, folder, etc.) can also be set.

Once a matching e-mail is found, its first attachment is saved to resource.path. If there is more than one attachment, the extra ones are saved next to it, named {name}_1{ext}, {name}_2{ext}, and so on.

If the resource declares extrapaths (multiple files for the same resource), the same search-and-save logic runs once per path.

CLI Extractor

Executes local command-line commands to generate or fetch data for the resource.

Configure with mode: cli and a list of arguments under dpetl_extract:

  • arguments: required. A list of shell commands to run sequentially.

Each command is executed in order. If a command fails, the error is logged and execution continues with the next command.

SQL Extractor

Connects to your database using the environment variable DATABASE_URL, which holds a SQLAlchemy URL (e.g. postgresql://user:pwd@host/db). Requires the sql extra (pip install dpetl[sql]) and the driver of your database.

It then runs the query set in dpetl_extract, using the first one it finds:

  • query: optional. The SQL query to run.

  • table: optional. Runs SELECT * FROM <table>.

  • query_file: optional (defaults to sql/<resource name>.sql). File with the query, relative to the descriptor.

Once the query runs, its result is saved to resource.path. Available formats: csv and xlsx.

  • encoding: optional (defaults to utf-8).

  • delimiter: optional (defaults to ,).

Example

Resources use dpetl_extract to declare how each file is downloaded. See the complete example.

resources:
  # API extraction
  - name: invoices
    path: data/invoices.csv
    sources:   # Request settings
      - method: get   # required (e.g. get, post)
        path: https://api.example.com   # required
        timeout: 30   # optional (Defaults to 30 seconds)
        params: {}   # optional
        headers: {}   # optional
        stream: false   # optional (Defaults to false)
    dpetl_extract:
      mode: api

  # CLI extraction
  - name: external_data
    path: data/external_data.csv
    dpetl_extract:
      mode: cli
      arguments:   # required. List of shell commands to run sequentially
        - curl -o data/external_data.csv https://api.example.com/export
        - python scripts/clean_data.py data/external_data.csv

  # Email extraction
  - name: payroll_from_email
    path: data/payroll.xlsx
    dpetl_extract:
      mode: email
      mailbox: INBOX   # optional (Defaults to INBOX)
      criteria:   # optional
        subject: "Payroll Report"   # optional (Defaults to resource name. See also the flag --add-package-name)
        from_: "finance@example.com"   # optional
        date_gte: 2024-01-01   # optional (See also the flag --today-email)

  # SQL extraction
  - name: sales
    path: data/sales.csv   # .xlsx saves as Excel, .csv.gz as compressed CSV
    dpetl_extract:
      mode: sql
      query: SELECT * FROM sales WHERE year = 2026   # optional
      table: sales   # optional. Runs SELECT * FROM sales
      query_file: sql/sales.sql   # optional (Defaults to sql/<resource name>.sql)
      encoding: utf-8   # optional (Defaults to utf-8)
      delimiter: ';'   # optional (Defaults to ,)

See the API reference.