3 # Reference implementation for the geohashing algorithm described in xkcd #426
4 # See http://wiki.xkcd.com/ProjectRainbow/Main_Page
6 # Dan Boger (zigdon@gmail.com)
11 use Digest
::MD5 qw
/md5_hex/;
18 # get the date from the commandline, or assume it's today's date
19 my $date = shift || "";
21 my $datestring = UnixDate
( ParseDate
($date), "%Y-%m-%d" );
23 die "Usage: $0 [-e] <date> [DJI opening]"
24 unless $datestring and $datestring =~ /^\d\d\d\d-\d\d-\d\d$/;
26 # get the DJIA from the commandline, or try to retrieve it from google
29 $djia = &download_djia
($date, $opts{e
});
32 print "Date: $datestring, DJIA: $djia\n";
34 # calculate the MD5 of the combined date and DJIA
35 my $md5 = md5_hex
("$datestring-$djia");
36 print "MD5($datestring-$djia): $md5\n";
39 my ( $md5x, $md5y ) = ( substr( $md5, 0, 16 ), substr( $md5, 16, 16 ) );
40 print "Split: $md5x, $md5y\n";
42 # transform into a fraction
43 my ( $fx, $fy ) = ( 0, 0 );
44 while ( length $md5x or length $md5y ) {
45 my $d = substr( $md5x, -1, 1, "" );
49 $d = substr( $md5y, -1, 1, "" );
53 printf "Fractions: %0.16f, %0.16f\n", $fx, $fy;
57 my $use_30w_rule = shift;
60 print "Adjusting for 30W\n";
61 $date = DateCalc
(ParseDate
($datestring), "-1 day");
64 my $datestring = UnixDate
( ParseDate
($date), "%Y-%m-%d" );
68 import LWP
::Simple
'get';
69 require HTML
::TreeBuilder
;
72 'http://finance.google.com/finance/historical?cid=983582&startdate=%s&enddate=%s';
74 UnixDate
( DateCalc
( $date, "- 7 days" ), "%b+%d,+%Y" ),
75 UnixDate
( $date, "%b+%d,+%Y" ) );
76 print "Downloading DJIA from google: $URL\n";
78 die "Failed to get DJIA from google!" unless $page;
79 my $tree = HTML
::TreeBuilder
->new_from_content($page)
80 or die "Failed to parse google DJIA page!";
82 my $data = $tree->look_down( id
=> "prices" ) or die "No data div: $!";
83 foreach my $td ( $data->look_down( class => "firstcol" ) ) {
84 next if $td->as_text eq 'Date';
86 $djia = $td->right->as_text;
88 print "DJIA opening for $datestring is $djia\n";
94 die "Failed to retrieve DJIA from google!" unless $djia;