#!/usr/bin/perl
# ====================================================================
# Copyright (c) 1999, MeepZor Consulting.
# All rights reserved.
#
# The use and distribution of this code or document is
# governed by version 1.0.1 of the MeepZor Consulting Public
# Licence (MCPL), which may be found on the Internet at
# .
#
#
# $Id: follow-rank,v 1.3 1999/12/12 18:44:22 coar Exp $
#
# This script uses the Perl Web-access modules to fetch an Amazon.com
# (or Amazon.co.uk) item detail page and extract the current sales rank.
# The rank is displayed on STDOUT, potentially prefixed with a timestamp.
#
# There is one required argument:
#
# follow-rank [-t] [-v] -u
#
# -t
# Include a timestamp prefix of the form "%Y-%M-%D-%H-%m:"
# -u
# Specify the full URL of a page containing the item's sales rank
# -v
# Turn on verbose reporting
#
#
# History:
#
# 1.0 Ken Coar
# Inspired by Steve Champeon's script posted to the StudioB CBP mailing list.
#
use Getopt::Std;
use LWP::UserAgent;
use HTTP::Request;
use DBI;
use DBD::mysql;
%args = ();
@errors = ();
&getopts("tu:v", \%args);
push(@errors, "no URL specified") if (! $args{'u'});
if ($#errors > -1) {
foreach (@errors) {
print STDERR $0, ": $_\n";
}
exit(1);
}
#
# Right, arguments have been fetched; let's get to work.
#
#
# Set things up..
#
$agent = new LWP::UserAgent;
$agent->agent("RankFollower/1.0.0" . $agent->agent);
$request = new HTTP::Request GET => $args{'u'};
#
# ..and go get the page.
#
$response = $agent->request($request);
#
# Presumably we've got it now, so look for the text we want. Get the numerical
# ranking, and edit out any commas.
#
$content = $response->content();
$content =~ m:^.*Sales Rank[^0-9]*([0-9,]+):s;
($rank = $1) =~ s:,::g;
#
# If we couldn't find the rank, don't display anything at all.
#
if (! $rank) {
print STDERR "$0: unable to obtain rank\n" if ($args{'v'});
exit(1);
}
if ($args{'t'}) {
my($null, $minute, $hour, $day, $month, $year);
($null, $minute, $hour, $day, $month, $year, $null, $null, $null) =
localtime;
$year += 1900;
$timestamp = sprintf("%04d-%02d-%02d-%02d-%02d:", $year, $month + 1, $day,
$hour, $minute);
}
else {
$timestamp = "";
}
print "$timestamp$rank\n";
print STDERR "Sales rank: $rank\n" if ($args{'v'});
exit 0;