Author: simplet
Date: 2008-12-08 12:55:51 +0100 (Mon, 08 Dec 2008)
New Revision: 3041

Modified:
   software_suite_v2/software/scripts/rss_monde/trunk/rss_monde.py
Log:
multiple sources available.


Modified: software_suite_v2/software/scripts/rss_monde/trunk/rss_monde.py
===================================================================
--- software_suite_v2/software/scripts/rss_monde/trunk/rss_monde.py     
2008-12-08 11:50:52 UTC (rev 3040)
+++ software_suite_v2/software/scripts/rss_monde/trunk/rss_monde.py     
2008-12-08 11:55:51 UTC (rev 3041)
@@ -1,78 +1,201 @@
 #!/usr/bin/python
-# -*- coding: UTF8 -*-
+# -*- coding: latin1 -*-
 
 """
 ======================================================================
 
- Read RSS news from "le journal le Monde".
+ Read RSS news from different french newspapers / web sites.
+
+      Simplet <simplet _at_ ptigeek _dot_ net>
+
+
+ NOTA
+ ====
+ There is a lot of redundencies in the code. This is intended.
+ Some rss feeds are "standard" and one function can parse them all; but
+ other rss feeds are totaly "exotics" and they require a specific parsing.
+ I made the choice to have one function for each feeds.
  
- Simplet <simplet _at_ ptigeek _dot_ net>
-    
+ 
+
+ Gadget:
+ Soon to be converted to a gadget with a combo box to choose your
+ source and the number of news to be read
+   
 ======================================================================
 """
 
 __author__  = 'Simplet <simplet _at_ ptigeek _dot_ net>'
-__appname__ = 'Rss gadget'
+__appname__ = 'Rss script'
 __version__ = '0.0.1'
-__date__    = '2008/11/26'
+__date__    = '2008/12/04'
 __license__ = 'GPL'
 
+
 import re
 import urllib2
-import time
 from xml.dom import minidom
 
 from tuxisalive.api import *
 
+
+iLimite = 10
+infos = []
+
+
+#####################################################################################
+
+
 def tux_speak(text):
     tux.tts.speak(text,"Julie",100)
 
 
-# remove all html tags from the string
 def strip_html_tags(value):
-    return re.sub(r'<[^>]*?>', '', value.replace('&lt;','<').replace('&gt;', 
'>').replace('&quot;', '\"').replace('&amp;quot;', '\"'))
+    return re.sub(r'<[^>]*?>', '', value.replace('&lt;', '<').replace('&gt;', 
'>').replace('&quot;', '\"').replace('&amp;quot;', '\"'))
 
 
-tux = TuxAPI('192.168.1.5', 270)
-tux.server.autoConnect(CLIENT_LEVEL_RESTRICTED, 'RssMonde', 'NONE')
-tux.server.waitConnected(10.0)
-tux.dongle.waitConnected(10.0)
-tux.radio.waitConnected(10.0)
-if tux.access.waitAcquire(10.0, ACCESS_PRIORITY_NORMAL):
+def getNewsLEMONDE():
+   global iLimite
+   news = []
+   cpt = 0
+   
+   req = urllib2.Request('http://www.lemonde.fr/rss/une.xml')
+   handle = urllib2.urlopen(req)
+   xmldoc = minidom.parse(handle)
 
+   for item in xmldoc.getElementsByTagName('item'):
+     if cpt >= iLimite:
+       break
 
-  the_url =  'http://www.lemonde.fr/rss/une.xml'
-  iLimite = 10
+     title = 
unicode(item.getElementsByTagName('title')[0].toxml()).encode('latin1')
+     description = 
unicode(item.getElementsByTagName('description')[0].toxml()).encode('latin1')
+     
+     news.extend([strip_html_tags(title), strip_html_tags(description)])
+     cpt = cpt + 1
+   
+   return news
 
-  req = urllib2.Request(the_url)
-  handle = urllib2.urlopen(req)
 
-  xmldoc = minidom.parse(handle)
+def getNewsYAHOO():
+   global iLimite
+   news = []
+   cpt = 0
+   
+   req = urllib2.Request('http://d.yimg.com/dj/rss/reuters-top.xml')
+   handle = urllib2.urlopen(req)
+   xmldoc = minidom.parse(handle)
 
-  cpt = 0
-  old_title = ""
+   for item in xmldoc.getElementsByTagName('item'):
+     if cpt >= iLimite:
+       break
 
-  for item in xmldoc.getElementsByTagName('item'):
-    if cpt >= iLimite:
-      break
+     title = 
unicode(item.getElementsByTagName('title')[0].toxml()).encode('latin1')
+     title = title.replace('<title><![CDATA[', '').replace(']]></title>','')
+     description = 
unicode(item.getElementsByTagName('description')[0].toxml()).encode('latin1')
+     description = description.replace('<description><![CDATA[', 
'').replace(']]></description>', '')
+     
+     news.extend([title, description])
+     cpt = cpt + 1
+   
+   return news
 
-    title = 
unicode(item.getElementsByTagName('title')[0].toxml()).encode('latin1')
-    description = 
unicode(item.getElementsByTagName('description')[0].toxml()).encode('latin1')
 
-    # The newspaper "le monde"'rss feed  have a bug : sometimes there are
-    # two identical news that are following each others...
-    # let's ignore them if they are identical
-    if old_title != title:
-      print "\n\n#" + str(cpt) + " title: " + strip_html_tags(title) + \
-            "\n\ndescription: " + strip_html_tags(description)
-      tux_speak(strip_html_tags(title))
-      time.sleep(1)
-      tux_speak(strip_html_tags(description))
-      time.sleep(1)
-      old_title = title
-      cpt = cpt + 1
+def getNewsLCI():
+   global iLimite
+   news = []
+   cpt = 0
+   
+   req = urllib2.Request('http://www.tf1.fr/xml/rss/0,,9,00.xml')
+   handle = urllib2.urlopen(req)
+   xmldoc = minidom.parse(handle)
 
+   for item in xmldoc.getElementsByTagName('item'):
+     if cpt >= iLimite:
+       break
 
+     title = 
unicode(item.getElementsByTagName('title')[0].toxml()).encode('latin1')
+     title = title.replace('<title><![CDATA[', '').replace(']]></title>','')
+     description = "" #no need to say the description  because it is the same 
as the title
+     
+     news.extend([title.strip(), description])
+     cpt = cpt + 1
+   
+   return news
+
+def getNews01NET():
+   global iLimite
+   news = []
+   cpt = 0
+   
+   req = urllib2.Request('http://www.01net.com/rss/actus.xml')
+   handle = urllib2.urlopen(req)
+   xmldoc = minidom.parse(handle)
+
+   for item in xmldoc.getElementsByTagName('item'):
+     if cpt >= iLimite:
+       break
+
+     title = 
unicode(item.getElementsByTagName('title')[0].toxml()).encode('latin1')
+     title = title.replace('<title><![CDATA[', '').replace(']]></title>','')
+     description = 
unicode(item.getElementsByTagName('description')[0].toxml()).encode('latin1')
+     description = description.replace('<description><![CDATA[', 
'').replace(']]></description>', '')
+    
+     news.extend([title.strip(), strip_html_tags(description)])
+     cpt = cpt + 1
+   
+   return news
+
+
+def getNewsZDNET():
+   global iLimite
+   news = []
+   cpt = 0
+   
+   req = urllib2.Request('http://www.zdnet.fr/feeds/rss/actualites/?l=20')
+   handle = urllib2.urlopen(req)
+   xmldoc = minidom.parse(handle)
+
+   for item in xmldoc.getElementsByTagName('item'):
+     if cpt >= iLimite:
+       break
+
+     title = 
unicode(item.getElementsByTagName('title')[0].toxml()).encode('latin1')
+     title = title.replace('<title><![CDATA[', '').replace(']]></title>','')
+     description = 
unicode(item.getElementsByTagName('description')[0].toxml()).encode('latin1')
+     description = description.replace('<description><![CDATA[', 
'').replace(']]></description>', '')
+    
+     news.extend([strip_html_tags(title), strip_html_tags(description)])
+     cpt = cpt + 1
+   
+   return news
+
+
+#####################################################################################
+
+
+# Which feed to read ?
+infos = getNewsLEMONDE()
+#infos = getNewsYAHOO()
+#infos = getNewsLCI()
+#infos = getNews01NET()
+#infos = getNewsZDNET()
+
+
+# Initializing the tux
+tux = TuxAPI('192.168.1.5', 270)
+tux.server.autoConnect(CLIENT_LEVEL_RESTRICTED, 'RssNews', 'NONE')
+tux.server.waitConnected(10.0)
+tux.dongle.waitConnected(10.0)
+tux.radio.waitConnected(10.0)
+if tux.access.waitAcquire(10.0, ACCESS_PRIORITY_NORMAL):
+
+  # Read the news
+  for i in range(1, len(infos), 2):
+    if i >= iLimite:
+      exit()
+    print "\n\n#" + str(int(round(i/2))+1) + " " + infos[int(i)-1] + "\n" + 
infos[int(i)]
+    tux_speak('[' + infos[int(i)-1] + '] ' + infos[int(i)])
+
 tux.access.release()
 tux.server.disconnect()
 tux.destroy()


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to