+def get_dbx_list(dbx,username,year,month):
+
+ gpx_list_id = None
+
+ for entry in dbx.files_list_folder('').entries:
+ if entry.name == u'Приложения':
+ for entry_app in dbx.files_list_folder(entry.id).entries:
+ if entry_app.name == u'Run.GPS':
+ gpx_list_id=entry_app.id
+ break
+ break
+
+ res = []
+
+ if gpx_list_id:
+
+ for file in dbx.files_list_folder(gpx_list_id).entries:
+
+ filename,ext = os.path.splitext(file.name)
+ if ext == '.tcx':
+ try:
+ fyear = int(filename[0:4])
+ fmonth = int(filename[5:7])
+ if fyear == year and fmonth == month:
+ sport = filename[18:]
+ timestamp = datetime.strptime(filename[0:16],'%Y-%m-%d_%H%M%S')
+ record={ 'id': file.id, 'timestamp': timestamp, 'sport': sport }
+ res.append(record)
+ except:
+ None
+
+ return res
+
+
+def sync_db(dbx,username,year,month,dir=".",verbose=False,force=False):
+
+ training_list = get_dbx_list(dbx,username,year,month)
+
+ for training in training_list:
+
+ filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,month,training['sport'],training['id'][3:])
+ dirname = "%s/%04d/%02d" % (dir,year,month)
+
+ if os.path.exists(filename) and not force:
+
+ if verbose:
+ print "training %s exists, skipping" % (filename)
+
+ else:
+
+ try:
+ os.makedirs(dirname)
+ except:
+ None
+
+ xml = get_db_gpx(dbx,training['id'])
+
+ if verbose:
+ print "writing training %s" % (filename)
+
+ gpx = pygpx.GPX()
+ gpx.ReadTree(xml)
+
+ sport = training['sport']
+ timestamp = gpx.tracks[0].start_time()
+
+ print sport, timestamp
+
+ if check_db_for_training(db,sport,timestamp):
+ print "The same training found"
+ continue
+
+ gpx.author = username
+ gpx.FixNames(training['sport'])
+ gpx.ProcessTrackSegs()
+ for track in gpx.tracks:
+ track.sport=training['sport']
+
+ xml = gpx.XMLTree();
+ f = open(filename,"w")
+ f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
+ f.close
+ write_parsed_to_db(db,gpx,filename)
+ try:
+ render_tiles.queue_render(db,filename)
+ except:
+ None
+
+
+def sync_osm(username,password,year,month,dir=".",verbose=False,force=False):
+
+ training_list = get_osm_list(username,password,year,month)
+
+ for training in training_list:
+
+ filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,month,training['sport'],training['id'])
+ dirname = "%s/%04d/%02d" % (dir,year,month)
+
+ if os.path.exists(filename) and not force:
+
+ if verbose:
+ print "training %s exists, skipping" % (filename)
+
+ else:
+
+ try:
+ os.makedirs(dirname)
+ except:
+ None
+
+ xml = get_osm_gpx(username,password,training['id'])
+
+ if verbose:
+ print "writing training %s" % (filename)
+
+ gpx = pygpx.GPX()
+ gpx.ReadTree(xml)
+
+ sport = training['sport']
+ timestamp = gpx.tracks[0].start_time()
+
+ if check_db_for_training(db,sport,timestamp):
+ print "The same training found"
+ continue
+
+ gpx.author = username
+ gpx.FixNames(training['sport'])
+ gpx.ProcessTrackSegs()
+ for track in gpx.tracks:
+ track.sport=training['sport']
+
+ xml = gpx.XMLTree();
+ f = open(filename,"w")
+ f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
+ f.close
+ write_parsed_to_db(db,gpx,filename)
+ try:
+ render_tiles.queue_render(db,filename)
+ except:
+ None
+