# -*- coding: utf-8 -*-
#Copyright (C) Fiz Vazquez vud1@sindominio.net
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import os
import sys
import shutil
import logging
from gi.repository import Gtk
import httplib2
import wordpresslib #TODO remove need for this library
from pytrainer.extensions.googlemaps import Googlemaps
import pytrainer.lib.points as Points
from pytrainer.lib.date import Date
from pytrainer.lib.uc import UC
class wordpress:
def __init__(self, parent = None, pytrainer_main = None, conf_dir = None, options = None):
#TODO could use some logging
self.parent = parent
self.pytrainer_main = pytrainer_main
self.uc = UC()
self.options = options
self.conf_dir = conf_dir
self.tmpdir = self.pytrainer_main.profile.tmpdir
self.data_path = self.pytrainer_main.profile.data_path
self.googlemaps = Googlemaps(data_path=self.data_path, pytrainer_main=pytrainer_main)
self.wp = None
def run(self, id, activity=None):
#Show user something is happening
msg = _("Posting to Wordpress blog")
md = Gtk.MessageDialog(self.pytrainer_main.windowmain.window1, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.INFO, Gtk.ButtonsType.NONE, msg)
md.set_title(_("Wordpress Extension Processing"))
md.set_modal(True)
md.show()
while Gtk.events_pending(): # This allows the GUI to update
Gtk.main_iteration() # before completion of this entire action
logging.debug("before request posting")
options = self.options
self.wordpressurl = options["wordpressurl"]
self.user = options["wordpressuser"]
self.password = options["wordpresspass"]
self.gpxfile = "%s/gpx/%s.gpx" %(self.conf_dir,id)
self.googlekey = options["googlekey"]
self.idrecord = id
self.activity = activity
self.wordpresscategory = options["wordpresscategory"]
debug_msg = "%s, %s, %s, %s, %s, %s" % (self.wordpressurl, self.user, self.gpxfile, self.googlekey, self.idrecord, self.wordpresscategory)
logging.debug(debug_msg)
try:
self.wp = wordpresslib.WordPressClient(self.wordpressurl, self.user, self.password) #TODO remove need for wordpresslib??
self.error = False
except Exception as e:
self.error = True
self.log = "Url, user or pass are incorrect. Please, check your configuration"
self.loadRecordInfo()
if self.title is None or self.title == "":
self.title = "Untitled Activity"
blog_table = self.createTable()
blog_route = self.createRoute()
blog_body = self.createBody()
blog_figureHR = self.createFigureHR()
blog_figureStage = self.createFigureStage()
blog_foot = self.createFoot()
self.description = ""
xmlstuff = '''
Gpx-format Kml-format (GoogleEarth)
''' %(hfile,gfile,kfile)
return description_route
def loadRecordInfo(self):
#date = Date()
#record = self.pytrainer_main.record.getrecordInfo(self.idrecord)[0]
#"sports.name,date,distance,time,beats,comments,average,calories,id_record,title,upositive,unegative,maxspeed,maxpace,pace,maxbeats,date_time_utc,date_time_local",
self.sport = self.activity.sport_name
self.date = self.activity.date
self.distance = self.activity.distance
self.time = "%d:%02d:%02d" % (self.activity.time_tuple)
self.beats = self.activity.beats
self.comments = self.activity.comments
self.average = self.activity.average
self.calories = self.activity.calories
self.title = self.activity.title
self.upositive = self.activity.upositive
self.unegative = self.activity.unegative
self.maxspeed = self.activity.maxspeed
self.maxpace = self.activity.maxpace
self.pace = self.activity.pace
self.maxbeats = self.activity.maxbeats
def createBody(self):
if self.comments is None or self.comments == "":
return ""
else:
return ''' Description:
%s
''' %self.comments
def createTable(self):
description_table = '''
| Activity: | %s | Date: | %s |
| Distance (%s): | %.2f | Time (hh:mm:ss): | %s |
| Max speed (%s): | %.2f | Avg speed (%s): | %.2f |
| Max pace (%s): | %.2f | Avg pace (%s): | %.2f |
| Max pulse: | %s | Avg pulse: | %s |
| Acc elevation (+%s): | %.2f | Acc elevation (-%s): | %.2f |