~goncharov-i-v/+junk/ForismaticApplet

« back to all changes in this revision

Viewing changes to Forismatic.py

  • Committer: Ivan Goncharov
  • Date: 2010-07-27 18:20:59 UTC
  • Revision ID: vanuch@vanuch-20100727182059-2wdhuhzz2cjxyk89
move Forismatic class to separete file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
import os
 
4
import os.path
 
5
import pynotify
 
6
import urllib
 
7
import xml.dom.minidom
 
8
 
 
9
 
 
10
class Forismatic:
 
11
        """Return random quote form www.forismatic.com"""
 
12
        def __init__(self, conf,path = None):
 
13
                #self.theme_path = theme_path
 
14
                self.conf = conf
 
15
                if path == None:
 
16
                        self.path = '/usr/local/ForismaticApplet'
 
17
                else:
 
18
                        self.path = path
 
19
                
 
20
                
 
21
        def get_quote(self):
 
22
                #saved_quote_path=os.path.join(cur_dir, 'forismatic') # if quote write to file
 
23
                #print logo_path
 
24
                #0) read file with saved quote
 
25
                """with codecs.open(saved_quote_path,
 
26
                                                encoding='utf-8',mode='r') as file_r:
 
27
                        quote = file_r.readlines()
 
28
                file_r.closed
 
29
                quote=u"".join(quote)
 
30
                """
 
31
                #read from GConf
 
32
                (self.QuoteText,self.quoteAuthor,self.QuoteLink)=self.conf.get_Quote()
 
33
                #print "%s  \n   %s :: %s"%(self.QuoteText,self.quoteAuthor,self.QuoteLink)
 
34
                if self.quoteAuthor == "":
 
35
                        quote=self.QuoteText
 
36
                else:
 
37
                        quote=self.QuoteText+"\n"+self.quoteAuthor
 
38
                #print quote
 
39
                #0.5) print Notify
 
40
                return quote
 
41
                
 
42
        def show_notify(self,body):
 
43
                (self.TimePeriod, self.Theme, self.Lang) = self.conf.get_settings()
 
44
                cur_dir=os.path.abspath(self.path)
 
45
                self.theme_path = os.path.join(self.path,'Themes',self.Theme)
 
46
                logo_path=os.path.join(self.theme_path,'forismatic_logo48x48.png')
 
47
                print logo_path
 
48
                title='Forismatic'
 
49
                
 
50
                icon='file://'+logo_path
 
51
                notif = pynotify.Notification (title, body, icon);
 
52
                notif.show ()
 
53
                
 
54
        def save_qote(self):
 
55
                #1)get quote form forismatic
 
56
                #exampl of quote:
 
57
                """
 
58
                <forismatic>
 
59
                        <quote>
 
60
                                <quoteText>
 
61
                                        Для любви не существует вчера, любовь не думает \
 
62
                                        о завтра. 
 
63
                                        Она жадно тянется к нынешнему дню, но этот день \
 
64
                                        нужен ей весь, неограниченный, не омраченный. 
 
65
                                </quoteText>
 
66
                                <quoteAuthor>
 
67
                                        Генрих Гейне 
 
68
                                </quoteAuthor>
 
69
                                <senderName>
 
70
                                </senderName>
 
71
                                <senderLink>
 
72
                                        http://ru.forismatic.com/8d9098bc35/
 
73
                                </senderLink>
 
74
                                <quoteLink>
 
75
                                        http://ru.forismatic.com/13d15ebb9a/
 
76
                                </quoteLink>
 
77
                        </quote>
 
78
                </forismatic>
 
79
                """
 
80
                #TODO: add check of bad response
 
81
                self.lang = u"".join(self.quote_lang())
 
82
                print self.lang
 
83
                params = urllib.urlencode({'method': 'getQuote',
 
84
                 'format': 'xml','key': '','lang': self.lang },False)
 
85
                
 
86
                #print params   #somesing wrong
 
87
                params2=u"".join('method=getQuote&format=xml&key=&lang=en')
 
88
                print params2
 
89
                f =urllib.urlopen("http://www.forismatic.com/api/1.0/",params2)
 
90
                quote_xml = f.read()
 
91
                #2)parseQuote:
 
92
                quoteText = "can not get quote"
 
93
                quoteAuthor = ""
 
94
                quoteLink = "error"
 
95
                
 
96
                quote=xml.dom.minidom.parseString(quote_xml)
 
97
                #text
 
98
                text = quote.getElementsByTagName('quoteText')
 
99
                for node in text[0].childNodes:
 
100
                        if node.nodeType == node.TEXT_NODE:
 
101
                                quoteText=node.data
 
102
                #author         
 
103
                text = quote.getElementsByTagName('quoteAuthor')
 
104
                for node in text[0].childNodes:
 
105
                        if node.nodeType == node.TEXT_NODE:
 
106
                                quoteAuthor =node.data
 
107
                                quoteAuthor = "--"+ quoteAuthor
 
108
                                quoteAuthor = quoteAuthor.rjust(30," ")
 
109
                #link                                                   
 
110
                text = quote.getElementsByTagName('quoteLink')
 
111
                for node in text[0].childNodes:
 
112
                        if node.nodeType == node.TEXT_NODE:
 
113
                                quoteLink =node.data
 
114
                #write to file                  
 
115
                """with codecs.open(saved_quote_path, 
 
116
                                                encoding='utf-8',mode='w') as file_w:
 
117
                        file_w.write(quoteText)
 
118
                        file_w.write("\n"+quoteAuthor)
 
119
                file_w.closed"""
 
120
                #write to GConf
 
121
                self.conf.set_Quote(quoteText, quoteAuthor, quoteLink)
 
122
                print "%s \n     %s \n  %s "%(quoteText, quoteAuthor, quoteLink)
 
123
        
 
124
        def quote_lang(self):
 
125
                self.Language=self.conf.get_settings()[2]
 
126
                if self.Language == 'English' or self.Language == 'english' \
 
127
                        or self.Language == 'en':
 
128
                        return 'en'
 
129
                elif self.Language == 'Russian' or self.Language == 'russian' \
 
130
                        or self.Language == 'ru':
 
131
                        return 'ru'
 
132
                else: 
 
133
                        return None
 
134
 
 
135
if __name__ == '__main__':
 
136
        import Preference
 
137
        conf = Preference.Config()
 
138
        path = os.getcwd()
 
139
        
 
140
        quote=Forismatic(conf,path)
 
141
        quote.save_qote()