File: main.py

package info (click to toggle)
pytrainer 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,892 kB
  • sloc: python: 15,949; perl: 6,100; xml: 195; sql: 151; makefile: 83; sh: 45
file content (127 lines) | stat: -rwxr-xr-x 5,282 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#Copyright (C) DJ dj@namurlug.org   http://blog.dedj.be

#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 xmlrpclib
import SOAPpy
import os
from pytrainer.lib.date import second2time
from pytrainer.lib.soapUtils import *

from optparse import OptionParser


class Main:
    def __init__(self,options):
        # on definit les parametres de connection au blog
        self.xmlrpcserver = options.xmlrpcserver
        self.blogid = options.blogid
        self.user = options.bloguser
        self.password = options.blogpass
        self.category_nbr = options.blogcategory
        #if you want accept comment, replace False by True
        self.comment = False
        #or ping
        self.ping = False


        self.category = [{'isPrimary': True, "categoryId" : self.category_nbr}]

        self.error = ""
        self.log =""
        self.idrecord = options.idrecord
        self.webserviceserver = SOAPpy.SOAPProxy("http://localhost:8081/")

        #we try the connection to the xml/rpc server
        try :
            self.connect = xmlrpclib.Server(self.xmlrpcserver)
            self.error = False
        except :
            print("can't connect the server")

    def loadRecordInfo(self):
        record = self.webserviceserver.getRecordInfo(self.idrecord)
        self.sport = record["sport"]
        self.date = record["date"]
        self.distance = record["distance"]
        self.time = second2time(float(record["time"]))
        self.heure = self.time[0]
        self.minute = self.time[1]
        self.seconde = self.time[2]
        self.beats = record["beats"]
        self.comments = record["comments"]
        self.average = record["average"]
        self.calories = record["calories"]
        self.title = record["title"]
        self.upositive = record["upositive"]
        self.unegative = record["unegative"]

    def construction(self):
        #this methode create the content post content, the section between the '''   are on html  thake wath you want
        #self.date, self.distance, self.time, self.beats, self.comments, self.average, self.calories, self.title, self.title, self.upositive, self.unegative, self.sport
        description_table = '''
                <p>%s %s</p>
                <p>With a average of %s km/h</p><br />
                <p>I have lost %s kcalories and my heart have %s pulsations (on average)</p>
                ''' %(self.sport, self.title,self.average, self.calories, self.beats)
        return description_table

    def chapeau(self):
        chapeau_table = '''
                <p>A %s score on a distance of %s km on %sh %sm %s </p><br />
                ''' %(self.sport,self.distance,self.heure, self.minute, self.seconde)
        return chapeau_table

    def run(self):
        #we load all info for the record
        self.loadRecordInfo()
        blog_desc = self.construction()
        blog_chap = self.chapeau()

        if self.error == False:
            #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)
            server = xmlrpclib.Server(options.xmlrpcserver)
            content = {'title' : self.date, 'description' : blog_desc, 'mt_allow_comments' : self.comment, 'mt_allow_pings' : self.ping,'mt_excerpt' : blog_chap}
            post = server.metaWeblog.newPost(self.blogid, self.user, self.password, content , True)
            #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
            change_cat = server.mt.setPostCategories(post,self.user,self.password, self.category)
            return "The post has been submited"
            self.webserviceserver.stop

        else:
            return self.log


parser = OptionParser()
parser.add_option("-d", "--device", dest="device")
parser.add_option("-k", "--xmlrpcserver", dest="xmlrpcserver")
parser.add_option("-u", "--bloguser", dest="bloguser")
parser.add_option("-p", "--blogpass", dest="blogpass")
parser.add_option("-l", "--blogid", dest="blogid")
parser.add_option("-c", "--blogcategory", dest="blogcategory")
parser.add_option("-g", "--gpxfile", dest="gpxfile")
parser.add_option("-i", "--idrecord", dest="idrecord")
(options,args) =  parser.parse_args()

try :
    x = Main(options)
    x.run()
    print(("le score du %s a ete envoye"  % x.date))
except xmlrpclib.Fault as f:
    print(("ERROR on the server\n   Code %i\n   Reason %s" % (f.faultCode,
                                                 f.faultString)))