#! /usr/bin/perl use warnings; use strict; use 5.010; use WWW::Mechanize::GZip; use HTML::TableExtract qw(tree); use open ':std', OUT => ':utf8'; use Prompt::Timeout; use constant TIMEOUT => 3; use constant MAXTRIES => 8; my $site = 'http://www.fourmilab.ch/yoursky/cities.html'; my $mech = 'WWW::Mechanize::GZip'->new; $mech->get($site); $mech->follow_link( text => 'Portland OR' ); my $lub = 2457204.63659; #least upper bound my $glb = 2457207.63659; #greatest lower bound $mech->set_fields(qw'date 2'); my $guess = median( $lub, $glb ); say "guess is $guess"; $mech->set_fields( jd => $guess ); $mech->click_button( value => "Update" ); my $te = 'HTML::TableExtract'->new; $te->parse( $mech->content ); my $table = ( $te->tables )[3]; my $table_tree = $table->tree; my $venus = $table_tree->cell( 4, 1 )->as_text; say "say venus is *$venus*"; my $jupiter = $table_tree->cell( 7, 1 )->as_text; $te->delete; say "say jupiter is *$jupiter*"; my $vstr = string_to_second($venus); my $jstr = string_to_second($jupiter); say "vstr is $vstr"; say "jstr is $jstr"; my $upper = $lub; my $lower = $glb; my $equal; my $equal_sec; my $now_string = localtime; my $filename = 'planet1.txt'; open( my $jh, '>>', $filename ) or warn "Could not open file '$filename' $!"; say $jh " venus jupiter jd $now_string"; my $attempts = 1; while ( ( abs( $jstr - $vstr ) gt 0 ) ) { #wait 5 secs my $default = (($attempts ge MAXTRIES)) ? 'N' : 'Y'; my $answer = prompt( "Make query number $attempts?", $default, TIMEOUT ); exit if $answer =~ /^N/i; $guess = median( $upper, $lower ); say "guess is $guess"; $mech->set_fields( jd => $guess ); $mech->click_button( value => "Update" ); #say $mech->dump_forms; $te = 'HTML::TableExtract'->new; $te->parse( $mech->content ); $table = ( $te->tables )[3]; $table_tree = $table->tree; $venus = $table_tree->cell( 4, 1 )->as_text; say "say venus is *$venus*"; $jupiter = $table_tree->cell( 7, 1 )->as_text; say "say jupiter is *$jupiter*"; $vstr = string_to_second($venus); say "vstr is $vstr"; $jstr = string_to_second($jupiter); say "jstr is $jstr"; say $jh " $vstr $jstr $guess "; if ( $jstr gt $vstr ) { $upper = $guess; } elsif ( $vstr gt $jstr ) { $lower = $guess; } else { $equal = $guess; say "equal, while condition should fail $equal"; $equal_sec = $vstr; } $te->delete; $attempts++; } my $equal_ra = second_to_string($equal_sec); say "equal_ra is $equal_ra"; say $jh "equal seconds is $equal_sec and equal ra is $equal_ra"; sub median { my ( $upper, $lower ) = @_; my $return = ( $upper + $lower ) / 2.0; return $return; } sub string_to_second { my $string = shift; my $return = 9000; if ( my $success = $string =~ /^(\d*)h\s+(\d*)m\s+(\d*)s$/ ) { #say "success is $success"; say " $1 $2 $3"; $return = 3600 * $1 + 60 * $2 + $3; } else { say "string was misformed"; } return $return; } sub second_to_string { my $seconds = shift; say "seconds are $seconds"; my $hours = int( $seconds / 3600 ); say "hours are $hours"; my $remainder = $seconds % 3600; say "remainder is $remainder"; my $minutes = int( $remainder / 60 ); say "minutes are $minutes"; my $sec = $remainder % 60; my $return = join( '', $hours, 'h ', $minutes, 'm ', $sec, 's' ); return $return; }