| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
#Copyright (C) DJ [email protected] http://blog.dedj.be |
| 5 |
|
| 6 |
#This program is free software; you can redistribute it and/or |
| 7 |
#modify it under the terms of the GNU General Public License |
| 8 |
#as published by the Free Software Foundation; either version 2 |
| 9 |
#of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
#This program is distributed in the hope that it will be useful, |
| 12 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
#GNU General Public License for more details. |
| 15 |
|
| 16 |
#You should have received a copy of the GNU General Public License |
| 17 |
#along with this program; if not, write to the Free Software |
| 18 |
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
|
| 20 |
import xmlrpclib |
| 21 |
import SOAPpy |
| 22 |
import os |
| 23 |
from pytrainer.lib.date import second2time |
| 24 |
from pytrainer.lib.soapUtils import * |
| 25 |
|
| 26 |
from optparse import OptionParser |
| 27 |
|
| 28 |
|
| 29 |
class Main: |
| 30 |
def __init__(self,options): |
| 31 |
# on definit les parametres de connection au blog |
| 32 |
self.xmlrpcserver = options.xmlrpcserver |
| 33 |
self.blogid = options.blogid |
| 34 |
self.user = options.bloguser |
| 35 |
self.password = options.blogpass |
| 36 |
self.category_nbr = options.blogcategory |
| 37 |
#if you want accept comment, replace False by True |
| 38 |
self.comment = False |
| 39 |
#or ping |
| 40 |
self.ping = False |
| 41 |
|
| 42 |
|
| 43 |
self.category = [{'isPrimary': True, "categoryId" : self.category_nbr}] |
| 44 |
|
| 45 |
self.error = "" |
| 46 |
self.log ="" |
| 47 |
self.idrecord = options.idrecord |
| 48 |
self.webserviceserver = SOAPpy.SOAPProxy("http://localhost:8081/") |
| 49 |
|
| 50 |
#we try the connection to the xml/rpc server |
| 51 |
try : |
| 52 |
self.connect = xmlrpclib.Server(self.xmlrpcserver) |
| 53 |
self.error = False |
| 54 |
except : |
| 55 |
print("can't connect the server") |
| 56 |
|
| 57 |
def loadRecordInfo(self): |
| 58 |
record = self.webserviceserver.getRecordInfo(self.idrecord) |
| 59 |
self.sport = record["sport"] |
| 60 |
self.date = record["date"] |
| 61 |
self.distance = record["distance"] |
| 62 |
self.time = second2time(float(record["time"])) |
| 63 |
self.heure = self.time[0] |
| 64 |
self.minute = self.time[1] |
| 65 |
self.seconde = self.time[2] |
| 66 |
self.beats = record["beats"] |
| 67 |
self.comments = record["comments"] |
| 68 |
self.average = record["average"] |
| 69 |
self.calories = record["calories"] |
| 70 |
self.title = record["title"] |
| 71 |
self.upositive = record["upositive"] |
| 72 |
self.unegative = record["unegative"] |
| 73 |
|
| 74 |
def construction(self): |
| 75 |
#this methode create the content post content, the section between the ''' are on html thake wath you want |
| 76 |
#self.date, self.distance, self.time, self.beats, self.comments, self.average, self.calories, self.title, self.title, self.upositive, self.unegative, self.sport |
| 77 |
description_table = ''' |
| 78 |
<p>%s %s</p> |
| 79 |
<p>With a average of %s km/h</p><br /> |
| 80 |
<p>I have lost %s kcalories and my heart have %s pulsations (on average)</p> |
| 81 |
''' %(self.sport, self.title,self.average, self.calories, self.beats) |
| 82 |
return description_table |
| 83 |
|
| 84 |
def chapeau(self): |
| 85 |
chapeau_table = ''' |
| 86 |
<p>A %s score on a distance of %s km on %sh %sm %s </p><br /> |
| 87 |
''' %(self.sport,self.distance,self.heure, self.minute, self.seconde) |
| 88 |
return chapeau_table |
| 89 |
|
| 90 |
def run(self): |
| 91 |
#we load all info for the record |
| 92 |
self.loadRecordInfo() |
| 93 |
blog_desc = self.construction() |
| 94 |
blog_chap = self.chapeau() |
| 95 |
|
| 96 |
if self.error == False: |
| 97 |
#post_description = "Du " + str(self.sport) + " sur une distance de " + str(self.distance) +"km " + " en " + str(self.time) + " ce qui nous fait une moyenne de " + str(self.average) |
| 98 |
server = xmlrpclib.Server(options.xmlrpcserver) |
| 99 |
content = {'title' : self.date, 'description' : blog_desc, 'mt_allow_comments' : self.comment, 'mt_allow_pings' : self.ping,'mt_excerpt' : blog_chap} |
| 100 |
post = server.metaWeblog.newPost(self.blogid, self.user, self.password, content , True) |
| 101 |
#we change the post categories because, i( or it's the false of xml/rpc) can't select the categories before the upload on the blog |
| 102 |
change_cat = server.mt.setPostCategories(post,self.user,self.password, self.category) |
| 103 |
return "The post has been submited" |
| 104 |
self.webserviceserver.stop |
| 105 |
|
| 106 |
else: |
| 107 |
return self.log |
| 108 |
|
| 109 |
|
| 110 |
parser = OptionParser() |
| 111 |
parser.add_option("-d", "--device", dest="device") |
| 112 |
parser.add_option("-k", "--xmlrpcserver", dest="xmlrpcserver") |
| 113 |
parser.add_option("-u", "--bloguser", dest="bloguser") |
| 114 |
parser.add_option("-p", "--blogpass", dest="blogpass") |
| 115 |
parser.add_option("-l", "--blogid", dest="blogid") |
| 116 |
parser.add_option("-c", "--blogcategory", dest="blogcategory") |
| 117 |
parser.add_option("-g", "--gpxfile", dest="gpxfile") |
| 118 |
parser.add_option("-i", "--idrecord", dest="idrecord") |
| 119 |
(options,args) = parser.parse_args() |
| 120 |
|
| 121 |
try : |
| 122 |
x = Main(options) |
| 123 |
x.run() |
| 124 |
print(("le score du %s a ete envoye" % x.date)) |
| 125 |
except xmlrpclib.Fault as f: |
| 126 |
print(("ERROR on the server\n Code %i\n Reason %s" % (f.faultCode, |
| 127 |
f.faultString))) |