Handle doc subdirectory
[viking.git] / tools / gcget
blobf00b9473204a50d647931e67577598a6a13aa9e9
1 #!/usr/bin/perl
3 # gcget 34.25,-118.25 100 > yeah.loc
4 # ./gcget "Johnstown,CA" 10|gc2gpspoint - -|viking -
6 # have to make viking-remote allow multiple points. maybe just regular input + maps ?
8 $USERNAME="username";
9 $PASSWORD="password";
11 $what = $ARGV[0];
12 if (!$what) {
13 print "Nothing to do!\nUsage: gcget zip/lat,lon/lat.mm.mmm,lon.mm.mmm/address [max]";
14 exit(0);
18 $zip = $lat = $lon = "";
19 if ( $what =~ /^(-?[0-9]{0,3}(\.[0-9]{0,9})?),(-?[0-9]{0,3}(\.[0-9]{0,9})?)$/ ) {
20 ($lat,$lon) = ($1,$3);
21 } elsif ( $what =~ /^(-?[0-9]{1,3})\.([0-9]{1,2}\.[0-9]{0,3}),(-?[0-9]{1,3})\.([0-9]{1,2}\.[0-9]{0,3})$/ ) {
22 $lat = $1 + ($1 < 0 ? -1 : 1) * $2 / 60;
23 $lon = $3 + ($3 < 0 ? -1 : 1) * $4 / 60;
24 } elsif ( $what =~ /^[0-9]{5}$/ ) {
25 $zip = $what;
26 } else {
27 $address = `gpsaddress.pl "$what"`;
28 if (not (($address) and $address =~ /^(-?[0-9]{1,3}(\.[0-9]{1,9})?) (-?[0-9]{1,3}(\.[0-9]{1,9})?)$/ )) {
29 die "gpsaddress failed looking up address '$what'\n"
30 } else {
31 ($lat,$lon) = ($1,$3);
35 # turn on perl's safety features
36 use warnings;
37 use WWW::Mechanize;
38 use HTML::TableContentParser;
39 $browser = WWW::Mechanize->new();
41 $browser->get("http://www.geocaching.com/seek/");
42 $browser->follow("log in");
43 print STDERR $browser->title() . "\n";
44 $browser->form_number(1);
45 $browser->field("myUsername", $USERNAME);
46 $browser->field("myPassword", $PASSWORD);
47 $browser->click();
48 print STDERR $browser->title() . "\n\n";
50 if ( $zip ) {
51 $browser->form(1);
52 $browser->field("zip", $zip);
53 } else {
54 $browser->form(5);
55 $browser->field("origin_lat", $lat);
56 $browser->field("origin_long", $lon);
58 $browser->click();
60 $gcsdone = 0;
61 $maxgcs = $ARGV[1] ? $ARGV[1] : 1000000;
63 print STDERR "\nPages: ";
65 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<loc version=\"1.0\" src=\"Groundspeak\">\n";
68 while ( $gcsdone < $maxgcs ) {
70 $p = HTML::TableContentParser->new();
71 $tables = $p->parse($browser->content());
73 $t = ${$tables}[10];
74 @rows = @{$t->{rows}};
75 $r = $rows[8];
77 @cs = @{$r->{cells}};
79 $browser->form_number(1);
81 for ( $i = 0; $i < 20; $i++ ) {
82 $r = $rows[4+(2*$i)];
83 @cs = @{$r->{cells}};
84 if ( $gcsdone < $maxgcs && (! $cs[1]->{data}) ) {
85 if ( $cs[7]->{data} =~ /name='CID' value='(.*)'>/ ) {
86 $browser->tick("CID", $1);
87 $gcsdone++;
92 $browser->click("Download");
93 @lines = split "\n", $browser->content();
94 shift @lines; shift @lines; pop @lines;
95 print join "\n", @lines;
96 print "</waypoint>\n";
98 $browser->back();
99 $form = $browser->form_name("Form1");
100 $nextlink = $browser->find_link( text => "Next" );
101 if ( $nextlink ) {
102 if ( ($nextlink->url()) =~ /_ctl([0-9]+)/ ) {
103 $form->push_input("hidden", {"name" => "ResultsPager:_ctl$1", value => ""});
104 } else {
105 print STDERR "\n\nERROR\n\n";
106 print "</loc>\n";
107 exit(0);
109 } else {
110 print STDERR "\n\nDONE\n\n";
111 print "</loc>\n";
112 exit(0);
114 $browser->submit();
116 print STDERR "i";
120 print STDERR "\n\nREACHED MAX\n\n";
121 print "</loc>\n";