-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgc_grab_gpx.py
executable file
·134 lines (118 loc) · 4.52 KB
/
gc_grab_gpx.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (c) Bernhard Tittelbach <[email protected]>
# License: public domain, attribution appreciated
import sys
import os
import getopt
import re
import geocachingsitelib as gc
destination_dir_ = os.path.curdir
gc_username_ = None
gc_password_ = None
def usage():
print("Sytax:")
print(" %s [options] <gccode|pquid|pqname> [...]" % (sys.argv[0]))
print("Options:")
print(" -h | --help Show Help")
print(" -d dir | --gpxdir=dir Write gpx to this dir")
print(" -l | --listpq List PocketQueries")
print(" -a | --allpq Download all PocketQueries")
print(" -c | --createpqdir Create dir for PQ")
print(" -u username | --username=gc_user ")
print(" -p password | --password=gc_pass ")
print(" -i | --noninteractive Never prompt for pwd, just fail")
print("If username and password are not provided, we interactively")
print("ask for them the first time and store a session cookie. Unless -i is given")
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "u:p:hlad:ci", ["listpq","help","gpxdir=","username=","password=","allpq","createpqdir","noninteractive","debug"])
except getopt.GetoptError as e:
print("ERROR: Invalid Option: " +str(e))
usage()
sys.exit(1)
fetch_all_pqs_ = False
list_pqs_ = False
create_pq_dir_ = False
gc.be_interactive = True
for o, a in opts:
if o in ["-h","--help"]:
usage()
sys.exit()
elif o in ["-a","--allpq"]:
fetch_all_pqs_ = True
elif o in ["-l","--listpq"]:
list_pqs_ = True
elif o in ["-d","--gpxdir"]:
destination_dir_ = a
elif o in ["-c","--createpqdir"]:
create_pq_dir_ = True
elif o in ["-u","--username"]:
gc.gc_username = a
elif o in ["-p","--password"]:
gc.gc_password = a
elif o in ["-i","--noninteractive"]:
gc.be_interactive = False
elif o in ["--debug"]:
gc.gc_debug = True
re_gccode = re.compile(r'GC[a-z0-9]{1,6}',re.IGNORECASE)
re_pquid = re.compile(r'[a-f0-9-]{36}',re.IGNORECASE)
pquids = list(filter(re_pquid.match, args))
gccodes = list(filter(re_gccode.match, args))
pqnames = set(args) - set(pquids) - set(gccodes)
destination_dir_ = os.path.expanduser(destination_dir_)
if (list_pqs_ == False and fetch_all_pqs_ == False and len(args) <1) or not os.path.isdir(destination_dir_):
print("ERROR: No gccodes given or %s may not be a valid writeable directory" % destination_dir_)
usage()
sys.exit(1)
pqdict = {}
pqrevdict = {}
pq_to_get_tuplelist = []
if len(pqnames) > 0 or len(pquids) > 0 or fetch_all_pqs_ or list_pqs_:
try:
pqdict = gc.get_pq_names()
except gc.NotLoggedInError as e:
print("ERROR:", e)
sys.exit(1)
pqrevdict = dict(zip(pqdict.values(),pqdict.keys()))
for gccode in gccodes:
try:
fn = gc.download_gpx(gccode, destination_dir_)
print("downloaded %s to %s" % (fn, destination_dir_))
except gc.NotLoggedInError as e:
print("ERROR:", e)
sys.exit(1)
except Exception as e:
print("ERROR: GPX download of %s to %s failed" % (gccode, destination_dir_))
print(e)
if list_pqs_:
print("Listing downloadable PQs:")
for (pqname, pquid) in pqdict.items():
print(" %s : %s" % (pquid, pqname))
for pquid in pquids:
if not pquid in pqrevdict:
print("ERROR: a PQ with UID '%s' is not in the list of downloadable pocketquieries on geocaching.com" % pquid)
continue
pq_to_get_tuplelist.append((pqrevdict[pquid],pquid))
for pqname in pqnames:
if not pqname in pqdict:
print("ERROR: a PQ named '%s' is not in the list of downloadable pocketquieries on geocaching.com" % pqname)
continue
pq_to_get_tuplelist.append((pqname,pqdict[pqname]))
if fetch_all_pqs_:
pq_to_get_tuplelist = pqdict.items()
for (pqname,pquid) in pq_to_get_tuplelist:
if create_pq_dir_:
pq_save_dir = os.path.join(destination_dir_,pqname)
else:
pq_save_dir = destination_dir_
try:
if not os.path.exists(pq_save_dir):
os.mkdir(pq_save_dir)
fn = gc.download_pq(pquid, pq_save_dir)
print("downloaded %s and saved %s to %s" % (pqname, fn, pq_save_dir))
except gc.NotLoggedInError as e:
print("ERROR:", e)
sys.exit(1)
except Exception as e:
print("ERROR: download of PQ '%s' with id '%s' to %s failed" % (pqname, pquid, pq_save_dir))
print(e)