7 from lxml import html,etree
8 from optparse import OptionParser
9 from datetime import date,datetime
10 from mx.DateTime.ISO import ParseDateTimeUTC
11 from parsegpx import write_parsed_to_db
16 def get_page(uname,year,month):
20 req = urllib2.Request("http://www.gps-sport.net/services/getMonthlyTrainingDataHTML_V2.jsp?userName=%s&year=%s&month=%s&rnd=0.645673"% (uname,year,month), None, {'User-agent': 'Mozilla/5.0'})
21 page = urllib2.urlopen(req).read()
22 dom = html.document_fromstring(page)
24 for element, attribute, link, pos in dom.iterlinks():
25 if attribute == "href":
26 if link.startswith("/trainings/"):
27 dummy, dummy, link = link.split('/')
28 name, id = link.split('_')
29 trainings.append([ urllib2.unquote(name), id ])
33 def get_gpx_track(trid,name):
37 req = urllib2.urlopen("http://www.gps-sport.net/services/trainingGPX.jsp?trainingID=%s&tz=-180" % (trid))
39 xml = etree.parse(req)
43 def get_osm_list(username,password,year,month):
45 url = "https://www.openstreetmap.org/api/0.6/user/gpx_files"
46 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
47 passman.add_password(None, url, username, password)
48 authhandler = urllib2.HTTPBasicAuthHandler(passman)
49 opener = urllib2.build_opener(authhandler)
50 urllib2.install_opener(opener)
52 req = urllib2.urlopen(url)
53 xml = etree.parse(req)
57 for gpx_file in xml.getroot():
58 attrib = dict(gpx_file.attrib)
59 timestamp=datetime.fromtimestamp(ParseDateTimeUTC(attrib['timestamp']))
62 for descr in gpx_file.iter('description'):
63 description=descr.text
65 for tag in gpx_file.iter('tag'):
67 if timestamp.year==year and timestamp.month==month and description=='Run.GPS Track':
68 print id,description,sport,timestamp
69 record={ 'id': id, 'sport': sport }
74 def get_osm_gpx(username,password,track_id):
76 url = "https://www.openstreetmap.org/api/0.6/gpx/"+track_id+"/data"
77 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
78 passman.add_password(None, url, username, password)
79 authhandler = urllib2.HTTPBasicAuthHandler(passman)
80 opener = urllib2.build_opener(authhandler)
81 urllib2.install_opener(opener)
83 req = urllib2.urlopen(url)
84 xml = etree.parse(req)
88 def sync_osm(username,password,year,month,dir=".",verbose=False,force=False):
90 training_list = get_osm_list(username,password,year,month)
92 for training in training_list:
93 filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,month,training['sport'],training['id'])
94 dirname = "%s/%04d/%02d" % (dir,year,month)
96 if os.path.exists(filename) and not force:
99 print "training %s exists, skipping" % (filename)
108 xml = get_osm_gpx(username,password,training['id'])
111 print "writing training %s" % (filename)
116 gpx.author = username
117 gpx.FixNames(training['sport'])
118 gpx.ProcessTrackSegs()
119 for track in gpx.tracks:
120 track.sport=training['sport']
123 f = open(filename,"w")
124 f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
126 write_parsed_to_db(db,gpx,filename)
128 render_tiles.queue_render(db,filename)
132 def sync_folder(username,year,month,dir=".",verbose=False,force=False):
134 training_list = get_page(username,year,month)
135 for tr in training_list:
137 filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,(month+1),tr[0],tr[1])
138 dirname = "%s/%04d/%02d" % (dir,year,(month+1))
140 if os.path.exists(filename) and not force:
143 print "training %s exists, skipping" % (filename)
152 xml=get_gpx_track(tr[1],tr[0])
155 print "writing training %s" % (filename)
161 gpx.ProcessTrackSegs()
164 f = open(filename,"w")
165 f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
167 write_parsed_to_db(db,gpx,filename)
169 render_tiles.queue_render(db,filename)
176 parser = OptionParser()
177 parser.add_option("-d", "--dir", dest="dirname",
178 help="write data to directory", metavar="DIR")
179 parser.add_option("-q", "--quiet",
180 action="store_false", dest="verbose", default=True,
181 help="don't print status messages to stdout")
182 parser.add_option("-f", "--force",
183 action="store_true", dest="force", default=False,
184 help="rewrite all files")
185 parser.add_option("-y", "--yearmonth", dest="yearmonth",
186 help="year and month in YYYY-MM format", metavar="YYYY-MM")
187 parser.add_option("-u", "--username", dest="username",
188 help="Run.GPS username")
189 parser.add_option("-o", "--osm-username", dest="osmname",
190 help="OpenStreetMap username")
191 parser.add_option("-p", "--osm-passwd", dest="osmpasswd",
192 help="OpenStreetMap password")
193 (options, args) = parser.parse_args()
195 username = options.username
196 osmname = options.osmname
197 osmpwd = options.osmpasswd
198 if not (username or (osmname and osmpwd)):
199 print "Run.GPS username or OSM username/password is mandatory!"
203 pusername = username+'@Run.GPS'
205 pusername = osmname+'@OSM'
208 if options.yearmonth:
209 year,month = options.yearmonth.split('-')
210 month = int(month) -1
212 if month<0 or month>11:
218 print "Year and month should be in YYYY-MM format!"
222 outdir = options.dirname
226 db = outdir + '/gpx.db'
230 print "retrieving trainings for user %s, year %s, month %s to %s" % (pusername,year,month+1,outdir)
232 sync_folder(username,year,month,outdir,options.verbose,options.force)
234 sync_osm(osmname,osmpwd,year,month+1,outdir,options.verbose,options.force)
237 print "retrieving two last months for user %s to %s" % (pusername,outdir)
239 current_year = now.year
240 current_month = now.month
242 sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
244 sync_osm(osmname,osmpwd,current_year,current_month,outdir,options.verbose,options.force)
245 current_month = current_month -1
246 if current_month == 0:
248 current_year = current_year -1
250 sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
252 sync_osm(osmname,osmpwd,current_year,current_month,outdir,options.verbose,options.force)
254 if __name__ == "__main__":