1 # for localized messages
4 from Components.config import config
5 from enigma import eTimer
7 from RSSFeed import BaseFeed, UniversalFeed
9 from twisted.web.client import getPage
10 from xml.etree.cElementTree import fromstring as cElementTree_fromstring
12 NOTIFICATIONID = 'SimpleRSSUpdateNotification'
17 """Keeps all Feed and takes care of (automatic) updates"""
19 def __init__(self, session, poll = True):
21 self.poll_timer = eTimer()
22 self.poll_timer.callback.append(self.poll)
24 self.poll_timer.start(0, 1)
26 # Save Session, Initialize Var to identify triggered Reload
27 self.session = session
28 self.reloading = False
30 self.newItemFeed = BaseFeed(
33 _("New Items since last Auto-Update"),
42 for x in config.plugins.simpleRSS.feed
48 def addCallback(self, callback):
49 if callback not in update_callbacks:
50 update_callbacks.append(callback)
52 def removeCallback(self, callback):
53 if callback in update_callbacks:
54 update_callbacks.remove(callback)
56 def doCallback(self, id = None):
57 for callback in update_callbacks:
63 def error(self, error = ""):
64 print "[SimpleRSS] failed to fetch feed:", error
66 # Assume its just a temporary failure and jump over to next feed
69 def _gotPage(self, data, id = None, callback = False, errorback = None):
70 # workaround: exceptions in gotPage-callback were ignored
72 self.gotPage(data, id)
75 except NotImplementedError, errmsg:
76 # Don't show this error when updating in background
78 from Screens.MessageBox import MessageBox
82 _("Sorry, this type of feed is unsupported:\n%s") % (str(errmsg)),
83 type = MessageBox.TYPE_INFO,
88 traceback.print_exc(file=sys.stdout)
89 # Errorback given, call it (asumme we don't need do restart timer!)
90 if errorback is not None:
93 # Assume its just a temporary failure and jump over to next feed
96 def gotPage(self, data, id = None):
97 feed = cElementTree_fromstring(data)
101 self.feeds[id].gotFeed(feed)
102 print "[SimpleRSS] single feed parsed..."
105 new_items = self.feeds[self.current_feed].gotFeed(feed)
107 print "[SimpleRSS] feed parsed..."
109 # Append new items to locally bound ones
110 if new_items is not None:
111 self.newItemFeed.history.extend(new_items)
113 # Start Timer so we can either fetch next feed or show new_items
116 def singlePoll(self, id, callback = False, errorback = None):
117 getPage(self.feeds[id].uri).addCallback(self._gotPage, id, callback, errorback).addErrback(errorback)
120 # Reloading, reschedule
122 print "[SimpleRSS] timer triggered while reloading, rescheduling"
123 self.poll_timer.start(10000, 1)
125 elif len(self.feeds) <= self.current_feed:
127 if len(self.newItemFeed.history):
128 print "[SimpleRSS] got new items, calling back"
132 update_notification_value = config.plugins.simpleRSS.update_notification.value
133 if update_notification_value == "preview":
134 from RSSScreens import RSSFeedView
136 from Tools.Notifications import AddNotificationWithID, RemovePopup
138 RemovePopup(NOTIFICATIONID)
140 AddNotificationWithID(
146 elif update_notification_value == "notification":
147 from Tools.Notifications import AddPopup
148 from Screens.MessageBox import MessageBox
151 _("Received %d new news item(s).") % (len(self.newItemFeed.history)),
152 MessageBox.TYPE_INFO,
158 print "[SimpleRSS] no new items"
160 self.current_feed = 0
161 self.poll_timer.startLongTimer(config.plugins.simpleRSS.interval.value*60)
164 # Assume we're cleaning history if current feed is 0
165 clearHistory = self.current_feed == 0
166 if config.plugins.simpleRSS.update_notification.value != "none":
167 from Tools.Notifications import current_notifications, notifications
168 for x in current_notifications:
169 if x[0] == NOTIFICATIONID:
170 print "[SimpleRSS] timer triggered while preview on screen, rescheduling"
171 self.poll_timer.start(10000, 1)
175 for x in notifications:
176 if x[4] and x[4] == NOTIFICATIONID:
177 print "[SimpleRSS] wont wipe history because it was never read"
182 del self.newItemFeed.history[:]
184 # Feed supposed to autoupdate
185 feed = self.feeds[self.current_feed]
188 getPage(feed.uri).addCallback(self._gotPage).addErrback(self.error)
191 print "[SimpleRSS] passing feed"
195 self.current_feed += 1
196 self.poll_timer.start(1000, 1)
199 self.poll_timer.callback.remove(self.poll)
200 self.poll_timer = None
202 def triggerReload(self):
203 self.reloading = True
206 oldfeeds = self.feeds
208 for x in config.plugins.simpleRSS.feed:
209 for feed in oldfeeds:
210 if x.uri.value == feed.uri:
211 # Update possibly different autoupdate value
212 feed.autoupdate = x.autoupdate.value
213 newfeeds.append(feed) # Append to new Feeds
214 oldfeeds.remove(feed) # Remove from old Feeds
225 self.feeds = newfeeds
227 self.reloading = False