Debian Codegraph @

extensions/wordpress/wordpress.py

raw
1 # -*- coding: utf-8 -*-
2
3 #Copyright (C) Fiz Vazquez [email protected]
4
5 #This program is free software; you can redistribute it and/or
6 #modify it under the terms of the GNU General Public License
7 #as published by the Free Software Foundation; either version 2
8 #of the License, or (at your option) any later version.
9
10 #This program is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #GNU General Public License for more details.
14
15 #You should have received a copy of the GNU General Public License
16 #along with this program; if not, write to the Free Software
17 #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 import os
20 import sys
21 import shutil
22 import logging
23
24 from gi.repository import Gtk
25 import httplib2
26
27 import wordpresslib #TODO remove need for this library
28 from pytrainer.extensions.googlemaps import Googlemaps
29 import pytrainer.lib.points as Points
30 from pytrainer.lib.date import Date
31 from pytrainer.lib.uc import UC
32
33 class wordpress:
34 def __init__(self, parent = None, pytrainer_main = None, conf_dir = None, options = None):
35 #TODO could use some logging
36 self.parent = parent
37 self.pytrainer_main = pytrainer_main
38 self.uc = UC()
39 self.options = options
40 self.conf_dir = conf_dir
41 self.tmpdir = self.pytrainer_main.profile.tmpdir
42 self.data_path = self.pytrainer_main.profile.data_path
43 self.googlemaps = Googlemaps(data_path=self.data_path, pytrainer_main=pytrainer_main)
44 self.wp = None
45
46 def run(self, id, activity=None):
47 #Show user something is happening
48 msg = _("Posting to Wordpress blog")
49 md = Gtk.MessageDialog(self.pytrainer_main.windowmain.window1, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.INFO, Gtk.ButtonsType.NONE, msg)
50 md.set_title(_("Wordpress Extension Processing"))
51 md.set_modal(True)
52 md.show()
53 while Gtk.events_pending(): # This allows the GUI to update
54 Gtk.main_iteration() # before completion of this entire action
55 logging.debug("before request posting")
56 options = self.options
57 self.wordpressurl = options["wordpressurl"]
58 self.user = options["wordpressuser"]
59 self.password = options["wordpresspass"]
60 self.gpxfile = "%s/gpx/%s.gpx" %(self.conf_dir,id)
61 self.googlekey = options["googlekey"]
62 self.idrecord = id
63 self.activity = activity
64 self.wordpresscategory = options["wordpresscategory"]
65 debug_msg = "%s, %s, %s, %s, %s, %s" % (self.wordpressurl, self.user, self.gpxfile, self.googlekey, self.idrecord, self.wordpresscategory)
66 logging.debug(debug_msg)
67 try:
68 self.wp = wordpresslib.WordPressClient(self.wordpressurl, self.user, self.password) #TODO remove need for wordpresslib??
69 self.error = False
70 except Exception as e:
71 self.error = True
72 self.log = "Url, user or pass are incorrect. Please, check your configuration"
73 self.loadRecordInfo()
74 if self.title is None or self.title == "":
75 self.title = "Untitled Activity"
76 blog_table = self.createTable()
77 blog_route = self.createRoute()
78 blog_body = self.createBody()
79
80 blog_figureHR = self.createFigureHR()
81 blog_figureStage = self.createFigureStage()
82 blog_foot = self.createFoot()
83
84 self.description = "<![CDATA["+blog_body+blog_table+blog_route+blog_figureHR+blog_figureStage+blog_foot+"]]>"
85 xmlstuff = '''<methodCall>
86 <methodName>metaWeblog.newPost</methodName>
87 <params>
88 <param>
89 <value>
90 <string>MyBlog</string>
91 </value>
92 </param>
93 <param>
94 <value>%s</value>
95 </param>
96 <param>
97 <value>
98 <string>%s</string>
99 </value>
100 </param>
101 <param>
102 <struct>
103 <member>
104 <name>categories</name>
105 <value>
106 <array>
107 <data>
108 <value>%s</value>
109 </data>
110 </array>
111 </value>
112 </member>
113 <member>
114 <name>description</name>
115 <value>%s</value>
116 </member>
117 <member>
118 <name>title</name>
119 <value>%s</value>
120 </member>
121 </struct>
122 </param>
123 <param>
124 <value>
125 <boolean>1</boolean>
126 </value>
127 </param>
128 </params>
129 </methodCall>
130 ''' % (self.user, self.password, self.wordpresscategory, self.description, self.title)
131
132 #POST request to Wordpress blog
133 h = httplib2.Http()
134 res, content = h.request(self.wordpressurl, 'POST', body=xmlstuff)
135 logging.debug("after request posting")
136 logging.debug("Got response status: %s, reason: %s, content: %s" % (res.status, res.reason, content))
137 if res.reason == 'OK':
138 res_msg = "Successfully posted to Wordpress."
139 else:
140 res_msg = "Some error occured\nGot a status %s, reason %s\nContent was: %s" % (res.status, res.reason, content)
141 #Close 'Please wait' dialog
142 md.destroy()
143 #Show the user the result
144 md = Gtk.MessageDialog(self.pytrainer_main.windowmain.window1, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, res_msg)
145 md.set_title(_("Wordpress Extension Upload Complete"))
146 md.set_modal(False)
147 md.run()
148 md.destroy()
149
150 def createRoute(self):
151 gpxpath = "%s/gpstrace.gpx.txt" % self.tmpdir
152 #htmlpath = "%s/index.html" % self.tmpdir
153 kmlpath = "%s/gps.kml.txt" % self.tmpdir
154 description_route = ''
155 if os.path.isfile(self.gpxfile):
156 #copy gpx file to new name
157 shutil.copy(self.gpxfile, gpxpath)
158 #create the html file
159 #htmlpath = googlemaps.drawMap(self.gpxfile,self.googlekey,htmlpath) #TODO fix to use main googlemaps and remove extensions copy
160 htmlpath = self.googlemaps.drawMap(self.activity)
161 #create the kml file
162 #TODO fix to remove gpsbabel
163 subprocess.call(["gpsbabel",
164 "-t",
165 "-i", "gpx",
166 "-f", self.gpxfile,
167 "-o", "kml,points=0,line_color=ff0000ff",
168 "-F", kmlpath])
169
170 #gfile = self.wp.newMediaObject(self.gpxfile)
171 gfile = self.wp.newMediaObject(gpxpath)
172 hfile = self.wp.newMediaObject(htmlpath)
173 kfile = self.wp.newMediaObject(kmlpath)
174
175 description_route = '''<strong>Map: </strong> <br/>
176 <iframe width='480' height='480' src='%s'> Need frame support </iframe><br/>
177 <a href='%s'>Gpx-format</a> <a href='%s'>Kml-format (GoogleEarth)</a><br/><br/>''' %(hfile,gfile,kfile)
178 return description_route
179
180 def loadRecordInfo(self):
181 #date = Date()
182 #record = self.pytrainer_main.record.getrecordInfo(self.idrecord)[0]
183 #"sports.name,date,distance,time,beats,comments,average,calories,id_record,title,upositive,unegative,maxspeed,maxpace,pace,maxbeats,date_time_utc,date_time_local",
184 self.sport = self.activity.sport_name
185 self.date = self.activity.date
186 self.distance = self.activity.distance
187 self.time = "%d:%02d:%02d" % (self.activity.time_tuple)
188 self.beats = self.activity.beats
189 self.comments = self.activity.comments
190 self.average = self.activity.average
191 self.calories = self.activity.calories
192 self.title = self.activity.title
193 self.upositive = self.activity.upositive
194 self.unegative = self.activity.unegative
195 self.maxspeed = self.activity.maxspeed
196 self.maxpace = self.activity.maxpace
197 self.pace = self.activity.pace
198 self.maxbeats = self.activity.maxbeats
199
200 def createBody(self):
201 if self.comments is None or self.comments == "":
202 return ""
203 else:
204 return '''<b> Description: </b><br/>%s<br/>''' %self.comments
205
206 def createTable(self):
207 description_table = '''
208 <br/>
209 <table border=0>
210 <tr>
211 <td><strong>Activity:</strong></td>
212 <td>%s</td>
213 <td><strong>Date:</strong></td>
214 <td>%s</td>
215 </tr>
216 <tr>
217 <td><strong>Distance (%s):</strong></td>
218 <td>%.2f</td>
219 <td><strong>Time (hh:mm:ss):</strong></td>
220 <td>%s</td>
221 </tr>
222 <tr>
223 <td><strong>Max speed (%s):</strong></td>
224 <td>%.2f</td>
225 <td><strong>Avg speed (%s):</strong></td>
226 <td>%.2f</td>
227 </tr>
228 <tr>
229 <td><strong>Max pace (%s):</strong></td>
230 <td>%.2f</td>
231 <td><strong>Avg pace (%s):</strong></td>
232 <td>%.2f</td>
233 </tr>
234 <tr>
235 <td><strong>Max pulse:</strong></td>
236 <td>%s</td>
237 <td><strong>Avg pulse:</strong></td>
238 <td>%s</td>
239 </tr>
240 <tr>
241 <td><strong>Acc elevation (+%s):</strong></td>
242 <td>%.2f</td>
243 <td><strong>Acc elevation (-%s):</strong></td>
244 <td>%.2f</td>
245 </tr>
246 </table>
247 ''' %( self.sport, self.date, self.uc.unit_distance, self.distance, self.time, self.uc.unit_speed, self.maxspeed,
248 self.uc.unit_speed, self.average, self.uc.unit_pace, self.maxpace, self.uc.unit_pace, self.pace,
249 self.maxbeats, self.beats, self.uc.unit_height, self.upositive, self.uc.unit_height, self.unegative)
250 return description_table
251
252 def createFigureHR(self):
253 hr_fig_path = "%s/hr.png" % self.tmpdir #TODO ensure png exists
254 blog_figures = ''
255 # If there are no graphs, return empty string.
256 if os.path.isfile(hr_fig_path):
257 #the graph files are created because the graph tabs are automatically visited (which invokes graph generation)
258 hrfile = self.wp.newMediaObject(hr_fig_path)
259 blog_figures = '''<br/> <img src='%s' /> ''' %hrfile
260 return blog_figures
261
262 def createFigureStage(self):
263 stage_fig_path = "/tmp/stage.png" #TODO fix, correct tmp dir and ensure png exists
264 blog_figures = ''
265 # If there are no graphs, return empty string.
266 if os.path.isfile(stage_fig_path):
267 #the graph files are created because the graph tabs are automatically visited (which invokes graph generation)
268 stagefile = self.wp.newMediaObject(stage_fig_path)
269 blog_figures = '''<br/> <img src='%s' /> ''' %stagefile
270 return blog_figures
271
272 def createFoot(self):
273 return ''' <br/><center>Powered by <a href='https://github.com/pytrainer/pytrainer'>pytrainer</a></center>'''
274
275 def createTitle(self):
276 if self.title==None:
277 self.error = True
278 self.log = "A Title must be defined. Please, configure the record properly"
279 return self.title
280
281 def createCategory(self):
282 if self.wordpresscategory==None:
283 self.error = True
284 self.log = "A wordpress category must be defined. Please, configure the wordpress extension"
285 else:
286 return ([self.wordpresscategory])