4 # THIS IS NO LONGER SUPPORTED
7 This script no longer works after the geocaching.com website update see:
8 http://blog.geocaching.com/2011/05/preview-of-geocaching-com-may-4th-website-release/
10 It is now recommended to use the geo-* tools instead:
17 # One day this script could be resurrected, as the geo-* tools are not perhaps the fastest...
21 # gcget -- screen scrape Geocaching.com's annoying web interface
22 # aka SHOW ME THE CACHE!!!
24 # Copyright 2007, Evan Battaglia
25 # Distributed under the terms of the GPL v2.
28 # requires module mechanize
31 # DEFAULT USERNAME AND PASSWORD: THESE ARE OVERWRITTEN BY COMMAND-LINE OPTIONS
37 # this has some extra args in:
38 # gcget lat,lon maxnumgcs [maxdist] [threshold]
39 # threshold -- if find more than this # of geocaches, don't get ANY,
40 # instead give warning and quit
47 This program is free software, distributed under the terms of the GNU GPL v2.
49 Usage: gcget [-u username] [-p password] lat,lon maxnumberofgcs [maxdistance] [threshold]
50 Downloads up to maxnumberofgcs at a distance of up to maxdistance from lat,lon.
51 If we number of geocaches within maxdistance is above threshold, don't download any
52 geocaches, just give a warning and quit.
54 If username and password are not given, will use default values hard-coded in script.
60 # PARSE OPTIONS: USERNAME AND PASSWORD
65 opts, args = getopt.gnu_getopt(sys.argv[1:], "u:p:d", ["help"])
66 except getopt.GetoptError:
67 # print help information and exit:
78 if o == "--help" or o == "-h":
88 #########################
89 #ll = args[0].split(",")
92 #The following line replaced the previous 3 lines.
93 lat, lon = args[0].split(",")
101 threshold = int(args[3])
105 # rounds up to multiples of 20. 20
106 n = int((int(args[1])+19)/20)
109 from mechanize import Browser
112 # get magic number for "Next" button.
113 # this is normally 16 (link hidden is $ctl16), unless there are less than 10 pages of results,
114 # in which case it will be less (e.g. 09 for 3 pages of results)
115 def getmagicnumber(b):
116 for i in range(16,0,-1):
117 if re.compile("pgrBottom.ctl%02d" % i).search(b.response().get_data()):
122 b.open("http://geocaching.com/seek/")
123 b.follow_link(text="Log in")
125 # The Username and Password fields on the Login form changed
126 b["ctl00$ContentBody$myUsername"] = USER
127 b["ctl00$ContentBody$myPassword"] = PASS
130 magicnumber = 0 # the ctl number of Next. get only once
132 try: b.select_form("form4")
135 b.select_form("form4")
137 b.select_form("form4")
138 print >> sys.stderr, "Invalid username/password"
140 f=open("gcget.badlogin.html","w")
141 f.write(b.response().get_data())
143 print >> sys.stderr, "Dumping last HTML page recieved into gcget.badlogin.html"
146 b["origin_lat"] = lat
147 b["origin_long"] = lon
151 thresholdre = re.compile("Total Records: <b>([0-9]*)</b>")
152 m = thresholdre.search(b.response().get_data())
154 if int(m.group(1)) > threshold:
155 sys.stderr.write("THRESHOLD %d > %d\n" % (int(m.group(1)), threshold))
158 records = int(m.group(1))
159 sys.stderr.write("ok found %d, getting min(%d,%d) gcs\n" % (int(m.group(1)), int(records), int(args[1])))
161 print "can't find total records"
165 # (records+19)/20 is the max pages
166 for ii in range(min(n,(records+19)/20)):
169 b['CID'] = [i.name for i in b.find_control('CID').items]
174 # only print one header, start of xml file
175 lines = b.response().get_data().split("\n")
177 print "\n".join(lines[0:2])
180 print "\n".join(lines[2:-1])
185 sys.stderr.write("i")
191 magicnumber = getmagicnumber(b)
193 # print "couldn't find magic number!" # why does this happen?
197 [f for f in b.forms()][0].new_control("hidden", "ctl00$ContentBody$pgrBottom$ctl%02d" % magicnumber, {})
200 sys.stderr.write("\n")
205 # f=open("delmeNOW","w")
206 # f.write(b.response().get_data())