+++ /dev/null
-#!/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
-from parsegpx import write_parsed_to_db
-import pygpx
-
-def get_page(uname,year,month):
-
- trainings = []
-
- req = urllib2.Request("http://www.gps-sport.net/embedCalendar.jsp?userName=%s&year=%s&month=%s"% (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):
-
- req = urllib2.urlopen("http://www.gps-sport.net/services/trainingGPX.jsp?trainingID=%s" % (trid))
-
- xml = etree.parse(req)
-
- return xml
-
-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/%s_%s.gpx" % (dir,tr[0],tr[1])
-
- if os.path.exists(filename) and not force:
-
- if verbose:
- print "training %s exists, skipping" % (filename)
-
- else:
-
- 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)
-
-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")
- (options, args) = parser.parse_args()
-
- username = options.username
- if not username:
- print "Run.GPS username is mandatory!"
- return
-
- 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" % (username,year,month+1,outdir)
- sync_folder(username,year,month,outdir,options.verbose,options.force)
- else:
- if options.verbose:
- print "retrieving two last months for user %s to %s" % (username,outdir)
- now = date.today()
- current_year = now.year
- current_month = now.month
- sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
- current_month = current_month -1
- if current_month == 0:
- current_month = 12
- current_year = current_year -1
- sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
-
-if __name__ == "__main__":
-
- main()
-
\ No newline at end of file