Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/sales.git
#!/bin/bash

# ########################################################################
#
# PURPOSE
#
# Filter data from Thrifty Foods webpage. This creates a CSV file that
# records any price changes since previous webpage downloads.
#
# DEPENDENCIES
#
# The hashed() function must be available to create a safe filename for
# the product name.
#
# ########################################################################

scrape() {
  local DIR_SUBSCRIBER=$1
  local VENDOR_NAME=$2
  local PRODUCT_NAME=$3
  local PRODUCT_PAGE=$4
  local POSTAL_CODE=$5

  local URL="https://www.thriftyfoods.com/product/$PRODUCT_PAGE"

  # Write the message body and subject to these files.
  local PATH_BODY="$DIR_SUBSCRIBER/$FILE_MESSAGE_BODY"
  local PATH_SUBJECT="$DIR_SUBSCRIBER/$FILE_MESSAGE_SUBJECT"

  # Store product data inside a file hashed from the product's name.
  local PATH_PRODUCT="$DIR_SUBSCRIBER/$(hashed $PRODUCT_NAME).csv"

  # Download the product webpage.
  # Determine the price.
  # Extract bulk cost (per 100G).
  # Get the dollar amount.
  # Remove the dollar sign.
  PRICE_CURRENT=$(lynx -nolist -nolog -accept_all_cookies -dump "$URL" | \
    grep -A1 "Comparison price:" | \
    tail -1 | \
    awk '{print $1}' | \
    tr -d '$')

  PRICE_PREVIOUS=0

  if [ -s "$PATH_PRODUCT" ]; then
    # Extract the previously recorded price from the end of the file.
    PRICE_PREVIOUS=$(tail -1 $PATH_PRODUCT | awk -F "," '{print $3}')
  fi

  # Determine whether the price has changed.
  PRICE_CHANGED=$(echo "$PRICE_PREVIOUS != $PRICE_CURRENT" | bc)

  # Only notify when a different dollar amount is found.
  if [ "$PRICE_CHANGED" -eq "1" ]; then
    local DATE_CURRENT=$(date +%0F)
    local TIME_CURRENT=$(date +%0R)

    # Save the new value (in CSV format) for a later comparison.
    echo "$DATE_CURRENT,$TIME_CURRENT,$PRICE_CURRENT" >> $PATH_PRODUCT

    # Reformat the date to be human-friendly.
    TIMESTAMP_CURRENT="$(date +"$DATE_FORMAT") $TIME_CURRENT"

    # Set the subject line with the final subscribed product and price.
    echo "[$VENDOR_NAME] $PRODUCT_NAME now \$$PRICE_CURRENT" > $PATH_SUBJECT

    # Put full details in the message body.
    cat >> "$PATH_BODY" << EOL

$PRODUCT_NAME
-------------------------
Price is \$$PRICE_CURRENT (was \$$PRICE_PREVIOUS) as of $TIMESTAMP_CURRENT.
EOL
  fi
}