-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcgpx.py
97 lines (77 loc) · 2.54 KB
/
gcgpx.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
import requests
import bs4
import re
import glob
import os
import time
import xml.dom.minidom as minidom
import random
import sys
defaultpath = 'H:\\gpx\\27-4-15'
filemask = 'GC*.GPX'
gpxage = 24*3600
maxdelay = 10.0
cipher = {}
for i in range(13):
cipher[chr(i+65)] = chr(i+78)
cipher[chr(i+78)] = chr(i+65)
cipher[chr(i+97)] = chr(i+110)
cipher[chr(i+110)] = chr(i+97)
cipherre = re.compile('|'.join(cipher.keys()))
if len(sys.argv)>1:
path = sys.argv[1]
else:
path = defaultpath
searchpath = os.path.join(path,filemask)
print('Searching '+searchpath)
files = [filename for filename in glob.glob(searchpath) if time.time() - os.stat(filename).st_mtime < gpxage]
print('Found '+str(len(files))+' gpx files')
for filename in files:
print('Processing '+filename)
gpxdom = minidom.parse(filename)
try:
geocode = gpxdom.getElementsByTagName('wpt')[0].getElementsByTagName('name')[0].firstChild.data
except AttributeError:
print('Did not find reference code in '+filename)
continue
try:
sdescnode = gpxdom.getElementsByTagName('groundspeak:short_description')[0].firstChild
except:
print('Did not find short description node in '+filename)
sdescnode = None
try:
ldescnode = gpxdom.getElementsByTagName('groundspeak:long_description')[0].firstChild
except:
print('Did not find long description node in '+filename)
ldescnode = None
try:
hintnode = gpxdom.getElementsByTagName('groundspeak:encoded_hints')[0].firstChild
except:
print('Did not find hint node in '+filename)
hintnode = None
geopage = requests.get('http://www.geocaching.com/geocache/'+geocode)
geohtml = bs4.BeautifulSoup(geopage.text, "html.parser")
hintobj = geohtml.find("div",id="div_hint")
if hintobj is None:
print('Did not find hint for '+geocode)
hint = ""
elif hintnode is not None:
hint = re.search("\s*(.*)",hintobj.text).group(1)
hint = cipherre.sub(lambda x: cipher[x.group()], hint)
hintnode.data = hint
shortdescobj = geohtml.find("span",id="ctl00_ContentBody_ShortDescription")
if shortdescobj is None:
print('Did not find short description for '+geocode)
shortdesc = ""
elif sdescnode is not None:
sdescnode.data = shortdescobj.text
longdescobj = geohtml.find("span",id="ctl00_ContentBody_LongDescription")
if longdescobj is None:
print('Did not find long description for '+geocode)
longdesc = ""
elif ldescnode is not None:
ldescnode.data = longdescobj.text
gpxfile = open(os.path.join(path,geocode+'.gpx'),'wb')
gpxfile.write(gpxdom.toxml('utf-8'))
gpxfile.close()
time.sleep(random.random()*maxdelay)