52904300165504b455309aadcd914074ff8c55c0
[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/embedCalendar.jsp?userName=%s&year=%s&month=%s"% (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   req = urllib2.urlopen("http://www.gps-sport.net/services/trainingGPX.jsp?trainingID=%s" % (trid))
35   
36   xml = etree.parse(req)
37
38   return xml
39
40 def sync_folder(username,year,month,dir=".",verbose=False,force=False):
41
42     training_list = get_page(username,year,month)
43     for tr in training_list:
44
45       filename = "%s/%s_%s.gpx" % (dir,tr[0],tr[1])   
46
47       if os.path.exists(filename) and not force:
48
49         if verbose:
50           print "training %s exists, skipping" % (filename)
51
52       else:  
53     
54         xml=get_gpx_track(tr[1],tr[0])
55
56         if verbose:
57           print "writing training %s" % (filename)
58
59         gpx = pygpx.GPX()
60         gpx.ReadTree(xml)
61
62         gpx.FixNames(tr[0])
63         gpx.ProcessTrackSegs()
64         
65         xml = gpx.XMLTree();
66         f = open(filename,"w")
67         f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
68         f.close
69         write_parsed_to_db(db,gpx,filename)
70         try:
71           render_tiles.queue_render(db,filename)
72         except:
73           None  
74
75 def main():
76
77     global db;
78     parser = OptionParser()
79     parser.add_option("-d", "--dir", dest="dirname",
80       help="write data to directory", metavar="DIR")
81     parser.add_option("-q", "--quiet",
82       action="store_false", dest="verbose", default=True,
83       help="don't print status messages to stdout")
84     parser.add_option("-f", "--force",
85       action="store_true", dest="force", default=False,
86       help="rewrite all files")
87     parser.add_option("-y", "--yearmonth", dest="yearmonth",
88       help="year and month in YYYY-MM format", metavar="YYYY-MM")                                                          
89     parser.add_option("-u", "--username", dest="username",
90       help="Run.GPS username")                                                          
91     (options, args) = parser.parse_args()
92
93     username = options.username
94     if not username:
95       print "Run.GPS username is mandatory!"
96       return
97
98     try:
99       if options.yearmonth:
100         year,month = options.yearmonth.split('-')
101         month = int(month) -1
102         year = int(year)
103         if month<0 or month>11:
104           raise invalid_number
105       else:
106         year = None
107         month = None
108     except:
109       print "Year and month should be in YYYY-MM format!"
110       return 
111       
112     if options.dirname:  
113       outdir = options.dirname
114     else:
115       outdir = '.'
116     
117     db = outdir + '/gpx.db'
118     
119     if year:
120       if options.verbose:
121         print "retrieving trainings for user %s, year %s, month %s to %s" % (username,year,month+1,outdir)
122       sync_folder(username,year,month,outdir,options.verbose,options.force)
123     else:
124       if options.verbose:
125         print "retrieving two last months for user %s to %s" % (username,outdir)
126       now = date.today()
127       current_year = now.year
128       current_month = now.month
129       sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
130       current_month = current_month -1
131       if current_month == 0:
132         current_month = 12
133         current_year = current_year -1
134       sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
135
136 if __name__ == "__main__":
137
138     main()                    
139