4 # gcget -- screen scrape Geocaching.com's annoying web interface
5 # aka SHOW ME THE CACHE!!!
7 # Copyright 2007, Evan Battaglia
8 # Distributed under the terms of the GPL v2.
11 # requires module mechanize
14 # DEFAULT USERNAME AND PASSWORD: THESE ARE OVERWRITTEN BY COMMAND-LINE OPTIONS
20 # this has some extra args in:
21 # gcget lat,lon maxnumgcs [maxdist] [threshold]
22 # threshold -- if find more than this # of geocaches, don't get ANY,
23 # instead give warning and quit
30 This program is free software, distributed under the terms of the GNU GPL v2.
32 Usage: gcget [-u username] [-p password] lat,lon maxnumberofgcs [maxdistance] [threshold]
33 Downloads up to maxnumberofgcs at a distance of up to maxdistance from lat,lon.
34 If we number of geocaches within maxdistance is above threshold, don't download any
35 geocaches, just give a warning and quit.
37 If username and password are not given, will use default values hard-coded in script.
43 # PARSE OPTIONS: USERNAME AND PASSWORD
48 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "u:p:d", ["help"])
49 except getopt
.GetoptError
:
50 # print help information and exit:
61 if o
== "--help" or o
== "-h":
71 #########################
73 ll
= args
[0].split(",")
83 threshold
= int(args
[3])
87 # rounds up to multiples of 20. 20
88 n
= int((int(args
[1])+19)/20)
91 from mechanize
import Browser
94 def getmagicnumber(b
):
95 for i
in range(16,0,-1):
96 if re
.compile("pgrBottom..ctl%d" % i
).search(b
.response().get_data()):
101 b
.open("http://geocaching.com/seek/")
102 b
.follow_link(text
="log in")
104 b
["myUsername"] = USER
105 b
["myPassword"] = PASS
108 magicnumber
= 0 # the ctl number of Next. get only once
110 try: b
.select_form("form4")
113 b
.select_form("form4")
115 print >> sys
.stderr
, "Invalid username/password"
117 f
=open("gcget.badlogin.html","w")
118 f
.write(b
.response().get_data())
120 print >> sys
.stderr
, "Dumping last HTML page recieved into gcget.badlogin.html"
123 b
["origin_lat"] = lat
124 b
["origin_long"] = lon
128 thresholdre
= re
.compile("Total Records: <b>([0-9]*)</b>")
129 m
= thresholdre
.search(b
.response().get_data())
131 if int(m
.group(1)) > threshold
:
132 sys
.stderr
.write("THRESHOLD %d > %d\n" % (int(m
.group(1)), threshold
))
135 records
= int(m
.group(1))
136 sys
.stderr
.write("ok found %d, getting min(%d,%d) gcs\n" % (int(m
.group(1)), int(records
), int(args
[1])))
138 print "can't find total records"
142 # (records+19)/20 is the max pages
143 for ii
in range(min(n
,(records
+19)/20)):
146 b
['CID'] = [i
.name
for i
in b
.find_control('CID').items
]
151 # only print one header, start of xml file
152 lines
= b
.response().get_data().split("\n")
154 print "\n".join(lines
[0:2])
157 print "\n".join(lines
[2:-1])
162 sys
.stderr
.write("i")
168 magicnumber
= getmagicnumber(b
)
171 [f
for f
in b
.forms()][0].new_control("hidden", "pgrBottom$_ctl%d" % magicnumber
, {})
174 sys
.stderr
.write("\n")
179 # f=open("delmeNOW","w")
180 # f.write(b.response().get_data())