X-Git-Url: https://git.rvb.name/pyrungps.git/blobdiff_plain/8031e085646c31a307fe8ec64bf8993991a302ce..94f62add438cdf546fdae207ede72c05daf66c00:/pyrungps.py diff --git a/pyrungps.py b/pyrungps.py deleted file mode 100644 index 8d13b8d..0000000 --- a/pyrungps.py +++ /dev/null @@ -1,261 +0,0 @@ -#!/usr/bin/env python -# coding: UTF-8 - -import urllib2 -#import sys -import os -from lxml import html,etree -from optparse import OptionParser -from datetime import date,datetime -from mx.DateTime.ISO import ParseDateTimeUTC -from parsegpx import write_parsed_to_db -import pygpx - -import render_tiles - -import generate_image - -def get_page(uname,year,month): - - trainings = [] - - 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'}) - page = urllib2.urlopen(req).read() - dom = html.document_fromstring(page) - - for element, attribute, link, pos in dom.iterlinks(): - if attribute == "href": - if link.startswith("/trainings/"): - dummy, dummy, link = link.split('/') - name, id = link.split('_') - trainings.append([ urllib2.unquote(name), id ]) - - return trainings - -def get_gpx_track(trid,name): - - print "trid=",trid - - req = urllib2.urlopen("http://www.gps-sport.net/services/trainingGPX.jsp?trainingID=%s&tz=-180" % (trid)) - - xml = etree.parse(req) - - return xml - -def get_osm_list(username,password,year,month): - - url = "https://www.openstreetmap.org/api/0.6/user/gpx_files" - passman = urllib2.HTTPPasswordMgrWithDefaultRealm() - passman.add_password(None, url, username, password) - authhandler = urllib2.HTTPBasicAuthHandler(passman) - opener = urllib2.build_opener(authhandler) - urllib2.install_opener(opener) - - req = urllib2.urlopen(url) - xml = etree.parse(req) - - res=[] - - for gpx_file in xml.getroot(): - attrib = dict(gpx_file.attrib) - timestamp=datetime.fromtimestamp(ParseDateTimeUTC(attrib['timestamp'])) - id = attrib['id'] - description=None - for descr in gpx_file.iter('description'): - description=descr.text - sport=None - for tag in gpx_file.iter('tag'): - sport=tag.text - if timestamp.year==year and timestamp.month==month and description=='Run.GPS Track': - print id,description,sport,timestamp - record={ 'id': id, 'sport': sport } - res.append(record) - - return res - -def get_osm_gpx(username,password,track_id): - - url = "https://www.openstreetmap.org/api/0.6/gpx/"+track_id+"/data" - passman = urllib2.HTTPPasswordMgrWithDefaultRealm() - passman.add_password(None, url, username, password) - authhandler = urllib2.HTTPBasicAuthHandler(passman) - opener = urllib2.build_opener(authhandler) - urllib2.install_opener(opener) - - req = urllib2.urlopen(url) - xml = etree.parse(req) - - return xml - -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) - - 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_folder(username,year,month,dir=".",verbose=False,force=False): - - training_list = get_page(username,year,month) - for tr in training_list: - - filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,(month+1),tr[0],tr[1]) - dirname = "%s/%04d/%02d" % (dir,year,(month+1)) - - 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_gpx_track(tr[1],tr[0]) - - if verbose: - print "writing training %s" % (filename) - - gpx = pygpx.GPX() - gpx.ReadTree(xml) - - gpx.FixNames(tr[0]) - gpx.ProcessTrackSegs() - - 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 main(): - - global db; - parser = OptionParser() - parser.add_option("-d", "--dir", dest="dirname", - help="write data to directory", metavar="DIR") - parser.add_option("-q", "--quiet", - action="store_false", dest="verbose", default=True, - help="don't print status messages to stdout") - parser.add_option("-f", "--force", - action="store_true", dest="force", default=False, - help="rewrite all files") - parser.add_option("-y", "--yearmonth", dest="yearmonth", - help="year and month in YYYY-MM format", metavar="YYYY-MM") - parser.add_option("-u", "--username", dest="username", - help="Run.GPS username") - parser.add_option("-o", "--osm-username", dest="osmname", - help="OpenStreetMap username") - parser.add_option("-p", "--osm-passwd", dest="osmpasswd", - help="OpenStreetMap password") - (options, args) = parser.parse_args() - - username = options.username - osmname = options.osmname - osmpwd = options.osmpasswd - if not (username or (osmname and osmpwd)): - print "Run.GPS username or OSM username/password is mandatory!" - return - - if username: - pusername = username+'@Run.GPS' - elif osmname: - pusername = osmname+'@OSM' - - try: - if options.yearmonth: - year,month = options.yearmonth.split('-') - month = int(month) -1 - year = int(year) - if month<0 or month>11: - raise invalid_number - else: - year = None - month = None - except: - print "Year and month should be in YYYY-MM format!" - return - - if options.dirname: - outdir = options.dirname - else: - outdir = '.' - - db = outdir + '/gpx.db' - - if year: - if options.verbose: - print "retrieving trainings for user %s, year %s, month %s to %s" % (pusername,year,month+1,outdir) - if username: - sync_folder(username,year,month,outdir,options.verbose,options.force) - elif osmname: - sync_osm(osmname,osmpwd,year,month+1,outdir,options.verbose,options.force) - else: - if options.verbose: - print "retrieving two last months for user %s to %s" % (pusername,outdir) - now = date.today() - current_year = now.year - current_month = now.month - if username: - sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force) - elif osmname: - sync_osm(osmname,osmpwd,current_year,current_month,outdir,options.verbose,options.force) - current_month = current_month -1 - if current_month == 0: - current_month = 12 - current_year = current_year -1 - if username: - sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force) - elif osmname: - sync_osm(osmname,osmpwd,current_year,current_month,outdir,options.verbose,options.force) - - generate_image.render_all(db,'/etc/mapnik-osm-carto-data/veloroad-transparent.xml',400,400) - -if __name__ == "__main__": - - main() - \ No newline at end of file