← Back to team overview

cairo-dock-team team mailing list archive

[Merge] lp:~eduardo-mucelli/cairo-dock-plug-ins-extras/Quote into lp:cairo-dock-plug-ins-extras

 

Eduardo Mucelli Rezende Oliveira has proposed merging lp:~eduardo-mucelli/cairo-dock-plug-ins-extras/Quote into lp:cairo-dock-plug-ins-extras.

Requested reviews:
  Cairo-Dock Team (cairo-dock-team)


Quote applet, adding support to Bash.org, and Xkcdb.com
-- 
https://code.launchpad.net/~eduardo-mucelli/cairo-dock-plug-ins-extras/Quote/+merge/41488
Your team Cairo-Dock Team is requested to review the proposed merge of lp:~eduardo-mucelli/cairo-dock-plug-ins-extras/Quote into lp:cairo-dock-plug-ins-extras.
=== modified file 'Quote/Quote'
--- Quote/Quote	2010-11-21 15:12:33 +0000
+++ Quote/Quote	2010-11-22 16:42:27 +0000
@@ -15,7 +15,8 @@
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 #    GNU General Public License for more details.
 
-# This applet provides a "Quote of the day" feature
+# This applet provides a "Quote of the day" feature from some internet sources
+# such as Quotationspage.com, Bash.org, and Xkcdb.com
 
 import gobject, dbus, os, urllib, ConfigParser, itertools
 from dbus.mainloop.glib import DBusGMainLoop
@@ -26,11 +27,69 @@
 
 DBusGMainLoop(set_as_default=True)
 
+quotationspage, bash, xkcdb = range(3)                                                     # quotationspage = 0, bash = 1, xkcdb = 2
+
 class AgentOpener(FancyURLopener):
     """Masked user-agent otherwise the access would be forbidden"""
     version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'
 
-class TranslatorParser(SGMLParser):
+class BashParser(SGMLParser):
+
+    def reset(self):                              
+        SGMLParser.reset(self)
+        self.quote = []
+        self.inside_p_element = False                                               # indica se o parser esta dentro de <p></p> tag
+        self.current_quote = ""
+
+    def start_p(self, attrs):
+        for name, value in attrs:
+            if name == "class" and value == "qt":                                   # <p class="qt">...</p>
+                self.inside_p_element = True
+    
+    def end_p(self):
+        self.inside_p_element = False
+        self.quote.append(self.current_quote)                                       # adiciona o conteudo completo da tag
+        self.current_quote = ""                                                     # reinicia o armazenador do conteudo
+
+    def handle_data(self, text):
+        if self.inside_p_element:                                                   # estamos dentro de <dt><a>...</a></dt>
+            text = text.replace('<', '[')                                           # se a string contem '<nome>', gera o erro na hora do ShowDialog
+            text = text.replace('>', ']')                                           # pango_layout_set_markup_with_accel: Unknown tag 'nome'
+            self.current_quote += text                                              # concatena tudo que tiver dentro da tag
+
+    def parse(self, page):
+        self.feed(page)                                                             # feed the parser with the page's html
+        self.close()
+
+class XkcdbParser(SGMLParser):
+
+    def reset(self):                              
+        SGMLParser.reset(self)
+        self.quote = []
+        self.inside_span_element = False                                               # indica se o parser esta dentro de <p></p> tag
+        self.current_quote = ""
+
+    def start_span(self, attrs):
+        for name, value in attrs:
+            if name == "class" and value == "quote":                                   # <p class="qt">...</p>
+                self.inside_span_element = True
+    
+    def end_span(self):
+        self.inside_span_element = False
+        self.quote.append(self.current_quote)                                       # adiciona o conteudo completo da tag
+        self.current_quote = ""                                                     # reinicia o armazenador do conteudo
+
+    def handle_data(self, text):
+        if self.inside_span_element:                                                   # estamos dentro de <dt><a>...</a></dt>
+            text = text.replace('<', '[')                                           # se a string contem '<nome>', gera o erro na hora do ShowDialog
+            text = text.replace('>', ']')                                           # pango_layout_set_markup_with_accel: Unknown tag 'nome'
+            self.current_quote += text                                              # concatena tudo que tiver dentro da tag
+
+    def parse(self, page):
+        self.feed(page)                                                             # feed the parser with the page's html
+        self.close() 
+
+class QuotationspageParser(SGMLParser):
 
     def reset(self):                              
         SGMLParser.reset(self)
@@ -88,23 +147,37 @@
 
 class Interface:
 
-    def __init__(self):
+    def __init__(self, source):
+        self.source = source
         self.author = []
         self.quote = []
-    
+
     def fetch(self):
-        parser = TranslatorParser()                                                 # create the parser
+        if (self.source == quotationspage):
+            parser = QuotationspageParser()                                         # create the parser
+            url = "http://www.quotationspage.com/qotd.html";
+        elif (self.source == bash):
+            parser = BashParser()                                                   # create the parser
+            url = "http://bash.org/?random";
+        else:
+            parser = XkcdbParser()
+            url = "http://www.xkcdb.com/?random";
+
         opener = AgentOpener()                                                      # opens the web connection with masked user-agent
-        url = "http://www.quotationspage.com/qotd.html";
+        
         try:
             page = opener.open(url)                                                 # get the HTML
         except IOError:
-            print ("Problem to open the remote quotation site")
+            print ("Problem to open %s" % (url))
         else:
             parser.parse(page.read())                                               # feed the parser to get the specific content: translated text
             page.close()                                                            # lets close the page connection
-            self.quote = parser.quote
-            self.author = parser.author
+            if (self.source == quotationspage):  
+                self.quote = parser.quote
+                self.author = parser.author
+            elif (self.source == bash or self.source == xkcdb):
+                self.quote = parser.quote
+                self.quote = filter(None, self.quote)                               # retira os '' do array
         return self.quote, self.author
 
 class Quote:
@@ -115,8 +188,9 @@
         applet_path = "/org/cairodock/CairoDock/%s" % applet_name                   # path where our object is stored on the bus
         applet_object = bus.get_object("org.cairodock.CairoDock", applet_path)
         icon = dbus.Interface(applet_object, "org.cairodock.CairoDock.applet")
+        configuration = os.path.expanduser("~/.config/cairo-dock/current_theme/plug-ins/%s/%s.conf") % (applet_name, applet_name)
 
-        applet = Applet(icon)
+        applet = Applet(icon, configuration)
         applet.start()
         
         loop = gobject.MainLoop()
@@ -125,31 +199,48 @@
 
 class Applet:
 
-    def __init__(self, icon):
+    def __init__(self, icon, configuration):
         self.icon = icon
+        self.configuration = configuration                                          # configuration file
         self.authors = None
         self.quotes = None
-        self.dialog_active_time = 10                                                # time in seconds that the dialog window will be active
+        self.dialog_active_time = 30                                                # time in seconds that the dialog window will be active
+        self.source = quotationspage
         
     def start(self):
         log ("Applet started")
         self.connect_to_callbacks()
+        self.read_configuration_parameters()
         self.get_quotes_from_web()
 
     def connect_to_callbacks(self):                                                 # when reiceves the signal named as 1st parameter ...
         self.icon.connect_to_signal("on_click", self.action_on_click)               # ... chama a funcao callback que eh o segundo parametro
+        self.icon.connect_to_signal("on_reload_module", self.action_on_reload)
+
+    def read_configuration_parameters(self):
+        reader = ConfigParser.RawConfigParser()
+        reader.read(self.configuration)
+        self.source = reader.getint('Configuration', 'source')                      # get the source of quotations
+
+    def action_on_click(self, param):
+        self.show_quote()
+
+    def action_on_reload(self, config_has_changed):
+	    if config_has_changed:
+		    self.read_configuration_parameters()                                    # refresh the source of quotations
+            self.get_quotes_from_web()                                              # refresh the quotations
 
     def get_quotes_from_web(self):
-        interface = Interface()
+        interface = Interface(self.source)
         quote, author = interface.fetch()                                           # N-esima quote refere-se ao N-esimo autor. "quote[x] ~ author[x]"
         self.quotes = itertools.cycle(quote)
         self.authors = itertools.cycle(author)
 
-    def action_on_click(self, param):
-        self.show_quote()
-
     def show_quote(self):
-        quotation = "\"%s\" ~ %s" % (self.quotes.next(), self.authors.next())
+        if (self.source == quotationspage):
+            quotation = "\"%s\" ~ %s" % (self.quotes.next(), self.authors.next())
+        else:
+            quotation = "%s" % self.quotes.next()
         self.icon.ShowDialog(quotation, self.dialog_active_time)
 
 if __name__ == '__main__':

=== modified file 'Quote/Quote.conf'
--- Quote/Quote.conf	2010-11-21 15:12:33 +0000
+++ Quote/Quote.conf	2010-11-22 16:42:27 +0000
@@ -1,4 +1,4 @@
-#!en;0.0.1
+#!en;0.0.2
 
 #[gtk-about]
 [Icon]
@@ -89,3 +89,5 @@
 
 #[gtk-preferences]
 [Configuration]
+#l[Quotationspage.com;Bash.org;Xkcdb.com] Quote source:
+source = 0

=== modified file 'Quote/auto-load.conf'
--- Quote/auto-load.conf	2010-11-21 15:12:33 +0000
+++ Quote/auto-load.conf	2010-11-22 16:42:27 +0000
@@ -4,10 +4,10 @@
 author = Eduardo Mucelli Rezende Oliveira
 
 # A short description of the applet and how to use it.
-description = This applet provides the so wonderful "Quotes of the day" feature.
+description = This applet provides a "Quote of the day" feature from some internet sources \nsuch as Quotationspage.com, Bash.org, and Xkcdb.com
 
 # Category of the applet : 2 = files, 3 = internet, 4 = Desktop, 5 = accessory, 6 = system, 7 = fun
 category = 5
 
 # Version of the applet; change it everytime you change something in the config file. Don't forget to update the version both in this file and in the config file.
-version = 0.0.1
+version = 0.0.2


Follow ups