bff314aee32a87d1587c53f9107926d892303494
[pyrungps.git] / pyrungps.py
1 #!/usr/bin/env python
2 # coding: UTF-8
3
4 import urllib2
5 #import sys
6 import os
7 from lxml import html,etree
8 from optparse import OptionParser
9 from datetime import date
10 from parsegpx import write_parsed_to_db
11 import pygpx
12
13 import render_tiles
14
15 def get_page(uname,year,month):
16   
17   trainings = []
18
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)
22
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 ])
29       
30   return trainings      
31
32 def get_gpx_track(trid,name):
33
34   print "trid=",trid
35
36   req = urllib2.urlopen("http://www.gps-sport.net/services/trainingGPX.jsp?trainingID=%s&tz=-180" % (trid))
37   
38   xml = etree.parse(req)
39
40   return xml
41
42 def sync_folder(username,year,month,dir=".",verbose=False,force=False):
43
44     training_list = get_page(username,year,month)
45     for tr in training_list:
46
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))
49
50       if os.path.exists(filename) and not force:
51
52         if verbose:
53           print "training %s exists, skipping" % (filename)
54
55       else:  
56
57         try:
58           os.makedirs(dirname)    
59         except:
60           None
61           
62         xml=get_gpx_track(tr[1],tr[0])
63
64         if verbose:
65           print "writing training %s" % (filename)
66
67         gpx = pygpx.GPX()
68         gpx.ReadTree(xml)
69
70         gpx.FixNames(tr[0])
71         gpx.ProcessTrackSegs()
72         
73         xml = gpx.XMLTree();
74         f = open(filename,"w")
75         f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
76         f.close
77         write_parsed_to_db(db,gpx,filename)
78         try:
79           render_tiles.queue_render(db,filename)
80         except:
81           None  
82
83 def main():
84
85     global db;
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()
100
101     username = options.username
102     if not username:
103       print "Run.GPS username is mandatory!"
104       return
105
106     try:
107       if options.yearmonth:
108         year,month = options.yearmonth.split('-')
109         month = int(month) -1
110         year = int(year)
111         if month<0 or month>11:
112           raise invalid_number
113       else:
114         year = None
115         month = None
116     except:
117       print "Year and month should be in YYYY-MM format!"
118       return 
119       
120     if options.dirname:  
121       outdir = options.dirname
122     else:
123       outdir = '.'
124     
125     db = outdir + '/gpx.db'
126     
127     if year:
128       if options.verbose:
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)
131     else:
132       if options.verbose:
133         print "retrieving two last months for user %s to %s" % (username,outdir)
134       now = date.today()
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:
140         current_month = 12
141         current_year = current_year -1
142       sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
143
144 if __name__ == "__main__":
145
146     main()                    
147