]> git.street.me.uk Git - andy/viking.git/blame - tools/gcget
Replace remaining gettext calls with '_'
[andy/viking.git] / tools / gcget
CommitLineData
28c82d8b
EB
1#!/usr/bin/env python
2
5f96aa1a
RN
3#
4# THIS IS NO LONGER SUPPORTED
5#
6print """
7This 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/
9
10It is now recommended to use the geo-* tools instead:
11 http://geo.rkkda.com/
12"""
13import sys
14sys.exit(1)
15
16#
17# One day this script could be resurrected, as the geo-* tools are not perhaps the fastest...
18#
19
28c82d8b
EB
20#
21# gcget -- screen scrape Geocaching.com's annoying web interface
22# aka SHOW ME THE CACHE!!!
23#
24# Copyright 2007, Evan Battaglia
a5c8699d 25# Distributed under the terms of the GPL v2.
28c82d8b 26#
05225ccd 27#
28c82d8b 28# requires module mechanize
05225ccd 29#
05225ccd 30
a5c8699d
EB
31# DEFAULT USERNAME AND PASSWORD: THESE ARE OVERWRITTEN BY COMMAND-LINE OPTIONS
32
28c82d8b
EB
33USER="username"
34PASS="password"
35
36# docs needed!
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
41
42import sys
a5c8699d
EB
43import getopt
44
45def help():
46 print """gcget v0.1
47This program is free software, distributed under the terms of the GNU GPL v2.
48
49Usage: gcget [-u username] [-p password] lat,lon maxnumberofgcs [maxdistance] [threshold]
50Downloads up to maxnumberofgcs at a distance of up to maxdistance from lat,lon.
51If we number of geocaches within maxdistance is above threshold, don't download any
52 geocaches, just give a warning and quit.
53
54If username and password are not given, will use default values hard-coded in script.
55
56Happy caching!!!
57"""
58
59#
60# PARSE OPTIONS: USERNAME AND PASSWORD
61#
62#
63#
64try:
c5f63dfe 65 opts, args = getopt.gnu_getopt(sys.argv[1:], "u:p:d", ["help"])
a5c8699d
EB
66except getopt.GetoptError:
67 # print help information and exit:
68 help()
69 sys.exit(2)
70
c5f63dfe
EB
71DEBUG = False
72
a5c8699d
EB
73for o, a in opts:
74 if o == "-p":
75 PASS = a
76 if o == "-u":
77 USER = a
78 if o == "--help" or o == "-h":
79 help()
80 sys.exit()
c5f63dfe
EB
81 if o == "-d":
82 DEBUG = True
a5c8699d
EB
83
84if len(args) < 2:
85 help()
86 sys.exit()
87
88#########################
391505be
MC
89#ll = args[0].split(",")
90#lat = ll[0]
91#lon = ll[1]
92#The following line replaced the previous 3 lines.
93lat, lon = args[0].split(",")
baa015c0 94
a5c8699d
EB
95if len(args) >= 3:
96 maxdist = args[2]
28c82d8b
EB
97else:
98 maxdist = "999"
a5c8699d
EB
99
100if len(args) >= 4:
101 threshold = int(args[3])
28c82d8b
EB
102else:
103 threshold = 1000000;
104
105# rounds up to multiples of 20. 20
a5c8699d 106n = int((int(args[1])+19)/20)
28c82d8b
EB
107
108import re
109from mechanize import Browser
110import ClientForm
111
baa015c0
EB
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)
28c82d8b
EB
115def getmagicnumber(b):
116 for i in range(16,0,-1):
baa015c0 117 if re.compile("pgrBottom.ctl%02d" % i).search(b.response().get_data()):
28c82d8b 118 return i
baa015c0 119 return None
28c82d8b
EB
120
121b=Browser()
122b.open("http://geocaching.com/seek/")
baa015c0 123b.follow_link(text="Log in")
28c82d8b 124b.select_form(nr=0)
391505be
MC
125# The Username and Password fields on the Login form changed
126b["ctl00$ContentBody$myUsername"] = USER
127b["ctl00$ContentBody$myPassword"] = PASS
28c82d8b
EB
128b.submit()
129
130magicnumber = 0 # the ctl number of Next. get only once
131
132try: b.select_form("form4")
133except: pass
c3c9aa0b
EB
134try:
135 b.select_form("form4")
136except:
baa015c0 137 b.select_form("form4")
c3c9aa0b 138 print >> sys.stderr, "Invalid username/password"
c5f63dfe
EB
139 if DEBUG:
140 f=open("gcget.badlogin.html","w")
141 f.write(b.response().get_data())
142 f.close()
143 print >> sys.stderr, "Dumping last HTML page recieved into gcget.badlogin.html"
c3c9aa0b 144 sys.exit()
c5f63dfe 145
28c82d8b
EB
146b["origin_lat"] = lat
147b["origin_long"] = lon
148b["dist"] = maxdist
149b.submit()
150
151thresholdre = re.compile("Total Records: <b>([0-9]*)</b>")
152m = thresholdre.search(b.response().get_data())
153if m:
154 if int(m.group(1)) > threshold:
155 sys.stderr.write("THRESHOLD %d > %d\n" % (int(m.group(1)), threshold))
156 sys.exit(4)
157 else:
158 records = int(m.group(1))
a5c8699d 159 sys.stderr.write("ok found %d, getting min(%d,%d) gcs\n" % (int(m.group(1)), int(records), int(args[1])))
28c82d8b
EB
160else:
161 print "can't find total records"
162 sys.exit(0)
163
164pages = 0
165# (records+19)/20 is the max pages
166for ii in range(min(n,(records+19)/20)):
167 try:
168 b.select_form(nr=0)
169 b['CID'] = [i.name for i in b.find_control('CID').items]
170 b.submit()
171 except:
172 break
173
174 # only print one header, start of xml file
175 lines = b.response().get_data().split("\n")
176 if ii == 0:
177 print "\n".join(lines[0:2])
178
179 # core
180 print "\n".join(lines[2:-1])
181
182 print "</waypoint>"
183
184 pages += 1
185 sys.stderr.write("i")
186 sys.stderr.flush()
187
188 b.back()
189
190 if not magicnumber:
191 magicnumber = getmagicnumber(b)
baa015c0 192 if not magicnumber:
48cd009e 193# print "couldn't find magic number!" # why does this happen?
baa015c0 194 break
28c82d8b
EB
195
196 b.select_form(nr=0)
baa015c0 197 [f for f in b.forms()][0].new_control("hidden", "ctl00$ContentBody$pgrBottom$ctl%02d" % magicnumber, {})
28c82d8b
EB
198 b.submit()
199
200sys.stderr.write("\n")
201
202if pages:
203 print "</loc>"
204
205# f=open("delmeNOW","w")
206# f.write(b.response().get_data())
207# f.close()
baa015c0 208