-
Notifications
You must be signed in to change notification settings - Fork 2
/
save-for-address.py
executable file
·62 lines (53 loc) · 2.02 KB
/
save-for-address.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import operator
import optparse
import sys
from geocaching import Geo, tools
def main():
geo = Geo()
parser = optparse.OptionParser(usage="%prog radius address...",
description="Download caches for the given address and radius in km")
opts, args = parser.parse_args()
if len(args) < 2:
parser.print_help()
sys.exit(1)
radius = int(args[0])
address = ' '.join(args[1:])
print "Logged in as %s" % geo.login_from_config()
count, pages, et, url = geo.find_by_address(address, radius)
print "Found %d caches on %d result pages." % (count, pages)
print "Please enter the number of caches to download"
print "(or just hit enter for all):"
count = raw_input().strip()
if count == '':
count = None
else:
count = int(count)
caches = geo.get_search_results(et, url, count)
print "%-12s|%-8s|%s| %s" % ("Type", "Code", "X", "Title")
print "------------+--------+-+-----------------------------"
for cache in caches:
print "%-12s|%-8s|%s| %s" % (cache[0], cache[2], cache[3] and '-' or '+', cache[4])
print "Continue to download (only available caches will be downloaded)?"
yesno = raw_input().strip().lower()
if yesno[0] != 'y':
sys.exit(0)
valid = [cache[1] for cache in caches if not cache[3]]
for i, guid in enumerate(valid):
print ">>>>>>>>> Downloading information for cache %d of %d" % (i+1, len(valid))
gpx = geo.cache_gpx(guid)
if gpx is None:
print "Cache %s not found." % arg
continue
geocode = tools.geocode(gpx)
if geocode is None:
print "Can't parse cache %s, skipping", arg
continue
filename = "%s.gpx" % geocode
gpx.write(open(filename, "w"),
encoding="utf-8", xml_declaration=True,
pretty_print=True)
print ">>>>>>>>> Wrote %s" % filename
if __name__ == '__main__':
main()