Skip to main content

Automatic coupon clipper for Safeway's online "Safeway for U" coupons

Project description

Automatic Safeway coupon clipper

PyPI PyPI - Python Version Build codecov GitHub stars

safeway-coupons is a script that will log in to an account on safeway.com, and attempt to select all of the "Safeway for U" electronic coupons on the site so they don't have to each be clicked manually.

Design notes

Safeway's sign in page is protected by a web application firewall (WAF). safeway-coupons performs authentication using a headless instance of Google Chrome. Authentication may fail based on your IP's reputation, either by presenting a CAPTCHA or denying sign in attempts altogether. safeway-coupons currently does not have support for prompting the user to solve CAPTCHAs.

Once a signed in session is established, coupon clipping is performed using HTTP requests via requests.

Installation and usage with Docker

A Docker container is provided which runs safeway-coupons with cron. The cron schedule and your Safeway account details may be configured using environment variables, or with an accounts file.

Example docker-compose.yaml with configuration via environment variables:

version: "3.7"

services:
  safeway-coupons:
    image: ghcr.io/smkent/safeway-coupons:latest
    environment:
      CRON_SCHEDULE: "0 2 * * *"  # Run at 2:00 AM UTC each day
      # TZ: Antarctica/McMurdo  # Optional time zone to use instead of UTC
      SMTPHOST: your.smtp.host
      SAFEWAY_ACCOUNT_USERNAME: your.safeway.account.email@example.com
      SAFEWAY_ACCOUNT_PASSWORD: very_secret
      SAFEWAY_ACCOUNT_MAIL_FROM: your.email@example.com
      SAFEWAY_ACCOUNT_MAIL_TO: your.email@example.com
      # EXTRA_ARGS: --debug  # Optional
    restart: unless-stopped

Example docker-compose.yaml with configuration via accounts file:

version: "3.7"

services:
  safeway-coupons:
    image: ghcr.io/smkent/safeway-coupons:latest
    environment:
      CRON_SCHEDULE: "0 2 * * *"  # Run at 2:00 AM UTC each day
      # TZ: Antarctica/McMurdo  # Optional time zone to use instead of UTC
      SMTPHOST: your.smtp.host
      SAFEWAY_ACCOUNTS_FILE: /accounts_file
      # EXTRA_ARGS: --debug  # Optional
    restart: unless-stopped
    volumes:
      - path/to/safeway_accounts_file:/accounts_file:ro

Start the container by running:

docker-compose up -d

Debugging information can be viewed in the container log:

docker-compose logs -f

Installation from PyPI

Prerequisites

  • Google Chrome (for authentication performed via Selenium).
  • Optional: sendmail (for email support)

Installation

safeway-coupons is available on PyPI:

pip install safeway-coupons

Usage

For best results, run this program once a day or so with a cron daemon.

For full usage options, run

safeway-coupons --help

Configuration

safeway-coupons can clip coupons for one or more Safeway accounts in a single run, depending on the configuration method used.

If a sender email address is configured, a summary email will be sent for each Safeway account via sendmail. The email recipient defaults to the Safeway account email address, but can be overridden for each account.

Accounts are searched via the following methods in the listed order. Only one account configuration method may be used at a time.

With environment variables

A single Safeway account can be configured with environment variables:

  • SAFEWAY_ACCOUNT_USERNAME: Account email address (required)
  • SAFEWAY_ACCOUNT_PASSWORD: Account password (required)
  • SAFEWAY_ACCOUNT_MAIL_FROM: Sender address for email summary
  • SAFEWAY_ACCOUNT_MAIL_TO: Recipient address for email summary

With config file

Multiple Safeway accounts can be provided in an ini-style config file, with a section for each account. For example:

email_sender = sender@example.com   ; optional

[safeway.account@example.com]       ; required
password = 12345                    ; required
notify = your.email@example.com     ; optional

Provide the path to your config file using the -c or --accounts-config option:

safeway-coupons -c path/to/config/file

Development

Poetry installation

Via pipx:

pip install pipx
pipx install poetry
pipx inject poetry poetry-pre-commit-plugin

Via pip:

pip install poetry
poetry self add poetry-pre-commit-plugin

Invocation with docker-compose

safeway-coupons can be executed within a Docker container using docker-compose.dev.yaml.

To use, first create an accounts file in the same directory with your safeway-coupons accounts configuration. Then, execute safeway-coupons within a container using docker-compose:

docker-compose -f docker-compose.dev.yaml up --build

The container will run safeway-coupons once, attempt to clip one coupon, and then stop.

To change the safeway-coupons arguments, modify the command value in docker-compose.dev.yaml.

When finished with development tasks, the docker-compose state can be cleaned up with:

docker-compose -f docker-compose.dev.yaml down

Development tasks

  • Setup: poetry install
  • Run static checks: poetry run poe lint or poetry run pre-commit run --all-files
  • Run static checks and tests: poetry run poe test

Created from smkent/cookie-python using cookiecutter

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safeway_coupons-0.3.0.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

safeway_coupons-0.3.0-py3-none-any.whl (27.6 kB view details)

Uploaded Python 3

File details

Details for the file safeway_coupons-0.3.0.tar.gz.

File metadata

  • Download URL: safeway_coupons-0.3.0.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for safeway_coupons-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c00263662e313f40831a3dede4457df5cd4f1651667212a8c588c7fd81a5daf7
MD5 2fc02244c6741b5f4f24057f1eddf4d0
BLAKE2b-256 c007c0dc75041e4f2d5a060f88c5cbd3458a575e576b65861cf265ee62c2639f

See more details on using hashes here.

Provenance

The following attestation bundles were made for safeway_coupons-0.3.0.tar.gz:

Publisher: cd.yml on smkent/safeway-coupons

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file safeway_coupons-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for safeway_coupons-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 407fd506f569b498e5bfa8ad3e4db70d46a960395249fd03aeec45f4773a3709
MD5 c1624ae2a43c2562245034d4dd235ecc
BLAKE2b-256 7ecdea2ea90c0e7b899b1a0c7212e697d0c1ca8d93ec7e7167a1029320a780ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for safeway_coupons-0.3.0-py3-none-any.whl:

Publisher: cd.yml on smkent/safeway-coupons

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page