7 from lxml import html,etree
8 from optparse import OptionParser
9 from datetime import date
10 from parsegpx import write_parsed_to_db
15 def get_page(uname,year,month):
19 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'})
20 page = urllib2.urlopen(req).read()
21 dom = html.document_fromstring(page)
23 for element, attribute, link, pos in dom.iterlinks():
24 if attribute == "href":
25 if link.startswith("/trainings/"):
26 dummy, dummy, link = link.split('/')
27 name, id = link.split('_')
28 trainings.append([ urllib2.unquote(name), id ])
32 def get_gpx_track(trid,name):
36 req = urllib2.urlopen("http://www.gps-sport.net/services/trainingGPX.jsp?trainingID=%s&tz=-180" % (trid))
38 xml = etree.parse(req)
42 def sync_folder(username,year,month,dir=".",verbose=False,force=False):
44 training_list = get_page(username,year,month)
45 for tr in training_list:
47 filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,(month+1),tr[0],tr[1])
48 dirname = "%s/%04d/%02d" % (dir,year,(month+1))
50 if os.path.exists(filename) and not force:
53 print "training %s exists, skipping" % (filename)
62 xml=get_gpx_track(tr[1],tr[0])
65 print "writing training %s" % (filename)
71 gpx.ProcessTrackSegs()
74 f = open(filename,"w")
75 f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
77 write_parsed_to_db(db,gpx,filename)
79 render_tiles.queue_render(db,filename)
86 parser = OptionParser()
87 parser.add_option("-d", "--dir", dest="dirname",
88 help="write data to directory", metavar="DIR")
89 parser.add_option("-q", "--quiet",
90 action="store_false", dest="verbose", default=True,
91 help="don't print status messages to stdout")
92 parser.add_option("-f", "--force",
93 action="store_true", dest="force", default=False,
94 help="rewrite all files")
95 parser.add_option("-y", "--yearmonth", dest="yearmonth",
96 help="year and month in YYYY-MM format", metavar="YYYY-MM")
97 parser.add_option("-u", "--username", dest="username",
98 help="Run.GPS username")
99 (options, args) = parser.parse_args()
101 username = options.username
103 print "Run.GPS username is mandatory!"
107 if options.yearmonth:
108 year,month = options.yearmonth.split('-')
109 month = int(month) -1
111 if month<0 or month>11:
117 print "Year and month should be in YYYY-MM format!"
121 outdir = options.dirname
125 db = outdir + '/gpx.db'
129 print "retrieving trainings for user %s, year %s, month %s to %s" % (username,year,month+1,outdir)
130 sync_folder(username,year,month,outdir,options.verbose,options.force)
133 print "retrieving two last months for user %s to %s" % (username,outdir)
135 current_year = now.year
136 current_month = now.month
137 sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
138 current_month = current_month -1
139 if current_month == 0:
141 current_year = current_year -1
142 sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
144 if __name__ == "__main__":