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