]> git.street.me.uk Git - andy/viking.git/blob - tools/gcget
preferences / geocaching update
[andy/viking.git] / tools / gcget
1 #!/usr/bin/env python
2
3 #
4 # gcget -- screen scrape Geocaching.com's annoying web interface
5 #          aka SHOW ME THE CACHE!!!
6 #
7 # Copyright 2007, Evan Battaglia
8 # Distributed under the terms of the GPL v2.
9 #
10 #
11 # requires module mechanize
12 #
13
14 # DEFAULT USERNAME AND PASSWORD: THESE ARE OVERWRITTEN BY COMMAND-LINE OPTIONS
15
16 USER="username"
17 PASS="password"
18
19 # docs needed!
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
24
25 import sys
26 import getopt
27
28 def help():
29    print """gcget v0.1
30 This program is free software, distributed under the terms of the GNU GPL v2.
31
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.
36
37 If username and password are not given, will use default values hard-coded in script.
38
39 Happy caching!!!
40 """
41
42 #
43 # PARSE OPTIONS: USERNAME AND PASSWORD
44 #
45 #
46 #
47 try:
48   opts, args = getopt.gnu_getopt(sys.argv[1:], "u:p:", ["help"])
49 except getopt.GetoptError:
50   # print help information and exit:
51   help()
52   sys.exit(2)
53
54 for o, a in opts:
55   if o == "-p":
56     PASS = a
57   if o == "-u":
58     USER = a
59   if o == "--help" or o == "-h":
60     help()
61     sys.exit()
62
63 if len(args) < 2:
64   help()
65   sys.exit()
66
67 #########################
68
69 ll = args[0].split(",")
70 lat = ll[0]
71 lon = ll[1]
72
73 if len(args) >= 3:
74   maxdist = args[2]
75 else:
76   maxdist = "999"
77
78 if len(args) >= 4:
79   threshold = int(args[3])
80 else:
81   threshold = 1000000;
82
83 # rounds up to multiples of 20. 20
84 n = int((int(args[1])+19)/20)
85
86 import re
87 from mechanize import Browser
88 import ClientForm
89
90 def getmagicnumber(b):
91   for i in range(16,0,-1):
92     if re.compile("pgrBottom..ctl%d" % i).search(b.response().get_data()):
93       return i
94   return 0
95
96 b=Browser()
97 b.open("http://geocaching.com/seek/")
98 b.follow_link(text_regex="log in")
99 b.select_form(nr=0)
100 b["myUsername"] = USER
101 b["myPassword"] = PASS
102 b.submit()
103
104 magicnumber = 0 # the ctl number of Next. get only once
105
106 try: b.select_form("form4")
107 except: pass
108 b.select_form("form4")
109 b["origin_lat"] = lat
110 b["origin_long"] = lon
111 b["dist"] = maxdist
112 b.submit()
113
114 thresholdre = re.compile("Total Records: <b>([0-9]*)</b>")
115 m = thresholdre.search(b.response().get_data())
116 if m:
117   if int(m.group(1)) > threshold:
118     sys.stderr.write("THRESHOLD %d > %d\n" % (int(m.group(1)), threshold))
119     sys.exit(4)
120   else:
121     records = int(m.group(1))
122     sys.stderr.write("ok found %d, getting min(%d,%d) gcs\n" % (int(m.group(1)), int(records), int(args[1])))
123 else:
124   print "can't find total records"
125   sys.exit(0)
126
127 pages = 0
128 # (records+19)/20 is the max pages
129 for ii in range(min(n,(records+19)/20)):
130   try:
131     b.select_form(nr=0)
132     b['CID'] = [i.name for i in b.find_control('CID').items]
133     b.submit()
134   except:
135     break
136
137   # only print one header, start of xml file
138   lines = b.response().get_data().split("\n")
139   if ii == 0:
140     print "\n".join(lines[0:2])
141
142   # core
143   print "\n".join(lines[2:-1])
144
145   print "</waypoint>"
146
147   pages += 1
148   sys.stderr.write("i")
149   sys.stderr.flush()
150
151   b.back()
152
153   if not magicnumber:
154     magicnumber = getmagicnumber(b)
155
156   b.select_form(nr=0)
157   [f for f in b.forms()][0].new_control("hidden", "pgrBottom$_ctl%d" % magicnumber, {})
158   b.submit()
159
160 sys.stderr.write("\n")
161
162 if pages:
163   print "</loc>"
164
165 #  f=open("delmeNOW","w")
166 #  f.write(b.response().get_data())
167 #  f.close()