2009-11-10

Bash Script to Web Scrape Forex Data

I like to track the value of the Loonie in US dollars; ie, CAD to USD. I often thought to myself that it would be fantastic to have a software application that can email me foreign exchange rate information on a periodic basis.

Using a script employing bash, grep, awk, and sed, this is possible, especially if you run the script as a cron job. This script does a web scrape of data from fxware.com.

The script fxgrab provides a convenient way to send an email notification of foreign exchange rate data via a bash script in a POSIX-like environment (such as in Cygwin or Linux). Feel free to use the code listed below for your own applications/utilities. The variable FROMCURR represents the currency converted to another currency (TOCURR). Enjoy!


#!/bin/bash


# fxgrab
# Copyright (c) 2009 Mike Quentel

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# CONFIGURATION -- REPLACE THE FOLLOWING VARIABLES TO RUN THIS:
# FROMCURR = THE CURRENCY YOU ARE CONVERTING FROM.
# TOCURR = THE CURRENCY YOU ARE CONVERTING TO.
# FROM = WHO THE EMAIL IS FROM.
# SUBJECT = EMAIL SUBJECT.
# SMPTPSERVER = THE SERVER USED TO SEND THE EMAIL.
# PORT = EMAIL PORT.
# USER = EMAIL USER.
# PASSWD = EMAIL PASSWORD.
# RECIPIENT = EMAIL ADDRESS OF MESSAGE RECIPIENT.


CURRENT_DATE=$(date "+%A %Y%m%d %H:%M:%S")
FROMCURR="CAD"
TOCURR="USD"
FROM="replytothisaccount@someserver.com"
SUBJECT="FOREX NOTIFICATION"
SMTPSERVER="smtp.myisp.com"
PORT="587"
USER="myispuseraccount"
PASSWD="myisppassword"
RECIPIENT="somerecipient@someaddress.com"

cd /var/tmp
wget --post-data "f=$FROMCURR&t=$TOCURR" -o /dev/null -O forex.asp http://www.fxware.com/forex-currency/forex.asp
FX=$(grep -oE toa.*[0-9] forex.asp | awk '{print $3}' | sed -e 's/16px;">//g')
MESSAGE="Foreign exchange data accessed at $CURRENT_DATE on this server. 1 $FROMCURR = $FX $TOCURR"
echo $MESSAGE | email -f "$FROM" -s "$SUBJECT" -r "$SMTPSERVER" -p $PORT -u "$USER" -i "$PASSWD" -m login $RECIPIENT
rm -f forex.asp

No comments: