7 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
11 from urllib.parse import quote,unquote
12 from lxml import html,etree
13 from optparse import OptionParser
14 from datetime import date,datetime
15 from dateutil.parser import isoparse
16 from pyrungps.parsegpx import write_parsed_to_db,check_db_for_training
17 from pyrungps.pygpx import GPX
18 from io import BytesIO
20 from tempfile import NamedTemporaryFile
22 from pprint import pprint
24 from pyrungps.render_tiles import queue_render
26 from pyrungps.generate_image import render_all
28 def get_page(uname,year,month):
32 req = requests.get("http://www.gps-sport.net/services/getMonthlyTrainingDataHTML_V2.jsp?userName=%s&year=%s&month=%s&rnd=0.645673"% (uname,year,month), headers = {'User-agent': 'Mozilla/5.0'}, verify=False)
33 page = req.text.encode('utf-8')
34 dom = html.document_fromstring(page)
36 for element, attribute, link, pos in dom.iterlinks():
37 if attribute == "href":
38 if link.startswith("/trainings/"):
39 dummy, dummy, link = link.split('/')
40 name, *ids = link.split('_')
42 trainings.append([ unquote(name), id ])
46 def get_gpx_track(trid,name):
50 req = requests.get("http://www.gps-sport.net/services/trainingGPX.jsp?trainingID=%s&tz=-180" % (trid), verify=False)
52 xml = etree.fromstring(req.text.encode('utf-8'))
56 def get_osm_list(username,password,year,month):
58 url = "https://www.openstreetmap.org/api/0.6/user/gpx_files"
60 req = requests.get(url,auth=(username,password))
62 xml = etree.fromstring(req.text.encode('utf-8'))
67 attrib = dict(gpx_file.attrib)
69 timestamp=datetime.fromtimestamp(isoparse(attrib['timestamp']))
72 for descr in gpx_file.iter('description'):
73 description=descr.text
75 for tag in gpx_file.iter('tag'):
77 if timestamp.year==year and timestamp.month==month and description=='Run.GPS Track':
78 record={ 'id': id, 'sport': sport }
85 def get_osm_gpx(username,password,track_id):
87 url = "https://www.openstreetmap.org/api/0.6/gpx/"+track_id+"/data"
89 req = requests.get(url,auth=(username,password))
90 xml = etree.fromstring(req.text.encode('utf-8'))
94 def get_db_gpx(dbx,track_id):
96 md, res = dbx.files_download(track_id)
98 fs = NamedTemporaryFile(suffix='.tcx',delete = False)
100 fs.write(res.content)
102 fs = NamedTemporaryFile(suffix='.gpx',delete = False)
106 os.system("gpsbabel -i gtrnctr -f "+tmp_tcx_name+" -o gpx -F "+tmp_gpx_name)
108 with open (tmp_gpx_name, "r") as gpxfile:
111 os.remove(tmp_gpx_name)
112 os.remove(tmp_tcx_name)
114 xml = etree.fromstring(data.encode('utf-8'))
117 def get_dbx_list(dbx,username,year,month):
121 for entry in dbx.files_list_folder('').entries:
122 if entry.name == u'Приложения':
123 for entry_app in dbx.files_list_folder(entry.id).entries:
124 if entry_app.name == u'Run.GPS':
125 print("id="+entry_app.id)
126 gpx_list_id=entry_app.id
134 for file in dbx.files_list_folder(gpx_list_id).entries:
136 filename,ext = os.path.splitext(file.name)
139 fyear = int(filename[0:4])
140 fmonth = int(filename[5:7])
141 if fyear == year and fmonth == month:
142 sport = filename[41:]
143 timestamp = datetime.strptime(filename[0:19],'%Y-%m-%dT%H_%M_%S')
144 record={ 'id': file.id, 'timestamp': timestamp, 'sport': sport }
148 fyear = int(filename[0:4])
149 fmonth = int(filename[5:7])
150 if fyear == year and fmonth == month:
151 sport = filename[18:]
152 timestamp = datetime.strptime(filename[0:16],'%Y-%m-%d_%H%M%S')
153 record={ 'id': file.id, 'timestamp': timestamp, 'sport': sport }
161 def sync_db(dbx,username,year,month,dir=".",verbose=False,force=False):
163 training_list = get_dbx_list(dbx,username,year,month)
165 for training in training_list:
167 filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,month,training['sport'],training['id'][3:])
168 dirname = "%s/%04d/%02d" % (dir,year,month)
170 if os.path.exists(filename) and not force:
173 print("training %s exists, skipping" % (filename))
182 xml = get_db_gpx(dbx,training['id'])
185 print("writing training %s" % (filename))
190 sport = training['sport']
191 timestamp = gpx.tracks[0].start_time()
193 print(sport, timestamp)
195 if check_db_for_training(db,sport,timestamp):
196 print("The same training found")
199 gpx.author = username
200 gpx.FixNames(training['sport'])
201 gpx.ProcessTrackSegs()
202 for track in gpx.tracks:
203 track.sport=training['sport']
206 f = open(filename,"wb")
207 f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
209 write_parsed_to_db(db,gpx,filename)
211 queue_render(db,filename)
216 def sync_osm(username,password,year,month,dir=".",verbose=False,force=False):
218 training_list = get_osm_list(username,password,year,month)
220 for training in training_list:
222 filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,month,training['sport'],training['id'])
223 dirname = "%s/%04d/%02d" % (dir,year,month)
225 if os.path.exists(filename) and not force:
228 print("training %s exists, skipping" % (filename))
237 xml = get_osm_gpx(username,password,training['id'])
240 print("writing training %s" % (filename))
245 sport = training['sport']
246 timestamp = gpx.tracks[0].start_time()
248 if check_db_for_training(db,sport,timestamp):
249 print("The same training found")
252 gpx.author = username
253 gpx.FixNames(training['sport'])
254 gpx.ProcessTrackSegs()
255 for track in gpx.tracks:
256 track.sport=training['sport']
259 f = open(filename,"wb")
260 f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
262 write_parsed_to_db(db,gpx,filename)
264 queue_render(db,filename)
268 def sync_folder(username,year,month,dir=".",verbose=False,force=False):
270 training_list = get_page(username,year,month)
271 for tr in training_list:
274 filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,(month+1),tr[0],tr[1])
275 dirname = "%s/%04d/%02d" % (dir,year,(month+1))
277 if os.path.exists(filename) and not force:
280 print("training %s exists, skipping" % (filename))
289 xml=get_gpx_track(tr[1],tr[0])
292 print("writing training %s" % (filename))
298 timestamp = gpx.tracks[0].start_time()
300 if check_db_for_training(db,sport,timestamp):
301 print("The same training found")
305 gpx.ProcessTrackSegs()
308 f = open(filename,"wb")
309 f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
311 write_parsed_to_db(db,gpx,filename)
313 queue_render(db,filename)
323 parser = OptionParser()
324 parser.add_option("-d", "--dir", dest="dirname",
325 help="write data to directory", metavar="DIR")
326 parser.add_option("-q", "--quiet",
327 action="store_false", dest="verbose", default=True,
328 help="don't print status messages to stdout")
329 parser.add_option("-f", "--force",
330 action="store_true", dest="force", default=False,
331 help="rewrite all files")
332 parser.add_option("-y", "--yearmonth", dest="yearmonth",
333 help="year and month in YYYY-MM format", metavar="YYYY-MM")
334 parser.add_option("-u", "--username", dest="username",
335 help="Run.GPS username")
336 parser.add_option("-o", "--osm-username", dest="osmname",
337 help="OpenStreetMap username")
338 parser.add_option("-p", "--osm-passwd", dest="osmpasswd",
339 help="OpenStreetMap password")
340 parser.add_option("-b", "--dropbox", dest="dropboxauth",
341 help="DropBox Auth token")
342 (options, args) = parser.parse_args()
344 username = options.username
345 osmname = options.osmname
346 osmpwd = options.osmpasswd
347 dbauth = options.dropboxauth
348 if not (username or (osmname and osmpwd) or dbauth):
349 print("Run.GPS username or OSM username/password or Dropbox Auth token is mandatory!")
353 pusername = username+'@Run.GPS'
355 pusername = osmname+'@OSM'
358 dbx = dropbox.Dropbox(dbauth)
359 pusername = dbx.users_get_current_account().email+'@DBX'
362 if options.yearmonth:
363 year,month = options.yearmonth.split('-')
364 month = int(month) -1
366 if month<0 or month>11:
372 print("Year and month should be in YYYY-MM format!")
376 outdir = options.dirname
380 db = outdir + '/gpx.db'
384 print("retrieving trainings for user %s, year %s, month %s to %s" % (pusername,year,month+1,outdir))
386 sync_folder(username,year,month,outdir,options.verbose,options.force)
388 sync_osm(osmname,osmpwd,year,month+1,outdir,options.verbose,options.force)
390 sync_db(dbx,pusername,year,month+1,outdir,options.verbose,options.force)
393 print("retrieving two last months for user %s to %s" % (pusername ,outdir))
395 current_year = now.year
396 current_month = now.month
398 sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
400 sync_osm(osmname,osmpwd,current_year,current_month,outdir,options.verbose,options.force)
402 sync_db(dbx,pusername,current_year,current_month,outdir,options.verbose,options.force)
403 current_month = current_month -1
404 if current_month == 0:
406 current_year = current_year -1
408 sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
410 sync_osm(osmname,osmpwd,current_year,current_month,outdir,options.verbose,options.force)
412 sync_db(dbx,pusername,current_year,current_month,outdir,options.verbose,options.force)
414 render_all(db,'/etc/mapnik-osm-carto-data/veloroad-imposm.xml',640,640)
416 if __name__ == "__main__":