]> git.street.me.uk Git - andy/viking.git/blame - tools/gcget
Preferences: include the file. oops.
[andy/viking.git] / tools / gcget
CommitLineData
28c82d8b
EB
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 GPS v2.
9#
05225ccd 10#
28c82d8b 11# requires module mechanize
05225ccd 12#
05225ccd 13
28c82d8b
EB
14USER="username"
15PASS="password"
16
17# docs needed!
18# this has some extra args in:
19# gcget lat,lon maxnumgcs [maxdist] [threshold]
20# threshold -- if find more than this # of geocaches, don't get ANY,
21# instead give warning and quit
22
23import sys
24ll = sys.argv[1].split(",")
25lat = ll[0]
26lon = ll[1]
27
28if len(sys.argv) >= 4:
29 maxdist = sys.argv[3]
30else:
31 maxdist = "999"
32if len(sys.argv) >= 5:
33 threshold = int(sys.argv[4])
34else:
35 threshold = 1000000;
36
37# rounds up to multiples of 20. 20
38n = int((int(sys.argv[2])+19)/20)
39
40import re
41from mechanize import Browser
42import ClientForm
43
44def getmagicnumber(b):
45 for i in range(16,0,-1):
46 if re.compile("pgrBottom..ctl%d" % i).search(b.response().get_data()):
47 return i
48 return 0
49
50b=Browser()
51b.open("http://geocaching.com/seek/")
52b.follow_link(text_regex="log in")
53b.select_form(nr=0)
54b["myUsername"] = USER
55b["myPassword"] = PASS
56b.submit()
57
58magicnumber = 0 # the ctl number of Next. get only once
59
60try: b.select_form("form4")
61except: pass
62b.select_form("form4")
63b["origin_lat"] = lat
64b["origin_long"] = lon
65b["dist"] = maxdist
66b.submit()
67
68thresholdre = re.compile("Total Records: <b>([0-9]*)</b>")
69m = thresholdre.search(b.response().get_data())
70if m:
71 if int(m.group(1)) > threshold:
72 sys.stderr.write("THRESHOLD %d > %d\n" % (int(m.group(1)), threshold))
73 sys.exit(4)
74 else:
75 records = int(m.group(1))
76 sys.stderr.write("ok found %d, getting min(%d,%d) gcs\n" % (int(m.group(1)), int(records), int(sys.argv[2])))
77else:
78 print "can't find total records"
79 sys.exit(0)
80
81pages = 0
82# (records+19)/20 is the max pages
83for ii in range(min(n,(records+19)/20)):
84 try:
85 b.select_form(nr=0)
86 b['CID'] = [i.name for i in b.find_control('CID').items]
87 b.submit()
88 except:
89 break
90
91 # only print one header, start of xml file
92 lines = b.response().get_data().split("\n")
93 if ii == 0:
94 print "\n".join(lines[0:2])
95
96 # core
97 print "\n".join(lines[2:-1])
98
99 print "</waypoint>"
100
101 pages += 1
102 sys.stderr.write("i")
103 sys.stderr.flush()
104
105 b.back()
106
107 if not magicnumber:
108 magicnumber = getmagicnumber(b)
109
110 b.select_form(nr=0)
111 [f for f in b.forms()][0].new_control("hidden", "pgrBottom$_ctl%d" % magicnumber, {})
112 b.submit()
113
114sys.stderr.write("\n")
115
116if pages:
117 print "</loc>"
118
119# f=open("delmeNOW","w")
120# f.write(b.response().get_data())
121# f.close()