#! /usr/bin/perl -w use strict; use LWP::UserAgent; use Cwd; # version 0.1 ## This is an example perl script that will pull the correct data ## files for NGI's Daily Gas Price Index, NGI's Weekly Gas Price Index ## and NGI's Bidweek Survey. This is only an example and will probably ## need to be changed somewhat depending on your specific needs. ## At a minimum you will need to fill in values for the username and ## password and will probably want to fill in the outputdir variable ## as well. my $username = ""; my $password = ""; my $outputdir = ""; $outputdir = getcwd unless $outputdir; my ($day, $month, $year, $wday) = ( localtime )[3,4,5,6]; $year += 1900; $month += 1; my $ua = new LWP::UserAgent; # get the file for the daily series if ($wday >= 1 and $wday <= 5) { my $daily_filename = sprintf("%0.4d%0.2d%0.2d", $year, $month, $day) . "td.txt" ; my $req = HTTP::Request->new(GET => "http://intelligencepress.com/subscribers/daily/delimited/$daily_filename"); $req->authorization_basic($username, $password); my $res = $ua->request($req); my $content; if ($res->is_success) { $content = $res->content; # write the output to a file open (OUTPUT, ">$outputdir/$daily_filename") or die "can't write to $daily_filename\n"; print OUTPUT $content; close OUTPUT; # You could do something else here like parse and load data into # your database. } else { warn $res->status_line . " while retrieving $daily_filename\n"; } } # get the file for the weekly series if ( $wday == 1 ) { my $weekly_filename = sprintf("%0.4d%0.2d%0.2d", $year, $month, $day) . "wt.txt" ; my $req = HTTP::Request->new(GET => "http://intelligencepress.com/subscribers/weekly/delimited/$weekly_filename"); $req->authorization_basic($username, $password); my $res = $ua->request($req); my $content; if ($res->is_success) { $content = $res->content; # write the output to a file open (OUTPUT, ">$outputdir/$weekly_filename") or die "can't write to $weekly_filename\n"; print OUTPUT $content; close OUTPUT; # You could do something else here like parse and load data into # your database. } else { warn $res->status_line . " while retrieving $weekly_filename\n"; } } # get the file for the bidweek series if ( $day == 1 ) { my $bidweek_filename = sprintf("%0.4d%0.2d%0.2d", $year, $month, $day) . "bw.txt" ; my $req = HTTP::Request->new(GET => "http://intelligencepress.com/subscribers/weekly/delimited/$bidweek_filename"); $req->authorization_basic($username, $password); my $res = $ua->request($req); my $content; if ($res->is_success) { $content = $res->content; # write the output to a file open (OUTPUT, ">$outputdir/$bidweek_filename") or die "can't write to $bidweek_filename\n"; print OUTPUT $content; close OUTPUT; # You could do something else here like parse and load data into # your database. } else { warn $res->status_line . " while retrieving $bidweek_filename\n"; } }