1 from __future__ import print_function
3 # for localized messages
6 from Components.config import config
7 from enigma import eTimer
9 from Tools.Notifications import AddPopup
10 from Screens.MessageBox import MessageBox
12 from RSSFeed import BaseFeed, UniversalFeed
14 from twisted.web.client import getPage
15 from xml.etree.cElementTree import fromstring as cElementTree_fromstring
17 from GoogleReader import GoogleReader
19 NOTIFICATIONID = 'SimpleRSSUpdateNotification'
24 """Keeps all Feed and takes care of (automatic) updates"""
26 def __init__(self, poll = True):
28 self.poll_timer = eTimer()
29 self.poll_timer.callback.append(self.poll)
32 # this indicates we're reloading the list of feeds
33 self.reloading = False
35 self.newItemFeed = BaseFeed(
38 _("New Items since last Auto-Update"),
47 for x in config.plugins.simpleRSS.feed
50 if not config.plugins.simpleRSS.enable_google_reader.value:
52 self.poll_timer.start(0, 1)
54 self.googleReader = GoogleReader(config.plugins.simpleRSS.google_username.value, config.plugins.simpleRSS.google_password.value)
55 self.googleReader.login().addCallback(self.googleLoggedIn).addErrback(self.googleLoginFailed)
60 def googleLoggedIn(self, sid = None):
61 self.googleReader.getSubscriptionList().addCallback(self.googleSubscriptionList).addErrback(self.googleSubscriptionFailed)
63 def googleLoginFailed(self, res = None):
65 _("Failed to login to Google Reader."),
66 MessageBox.TYPE_ERROR,
70 self.reloading = False
72 self.poll_timer.start(0, 1)
74 def googleSubscriptionList(self, subscriptions = None):
75 self.feeds.extend(subscriptions)
77 self.reloading = False
80 self.poll_timer.start(0, 1)
82 def googleSubscriptionFailed(self, res = None):
84 _("Failed to get subscriptions from Google Reader."),
85 MessageBox.TYPE_ERROR,
89 self.reloading = False
91 self.poll_timer.start(0, 1)
93 def addCallback(self, callback):
94 if callback not in update_callbacks:
95 update_callbacks.append(callback)
97 def removeCallback(self, callback):
98 if callback in update_callbacks:
99 update_callbacks.remove(callback)
101 def doCallback(self, id = None):
102 for callback in update_callbacks:
108 def error(self, error = ""):
109 print("[SimpleRSS] failed to fetch feed:", error)
111 # Assume its just a temporary failure and jump over to next feed
114 def _gotPage(self, data, id = None, callback = False, errorback = None):
115 # workaround: exceptions in gotPage-callback were ignored
117 self.gotPage(data, id)
120 except NotImplementedError as errmsg:
121 # Don't show this error when updating in background
124 _("Sorry, this type of feed is unsupported:\n%s") % (str(errmsg)),
125 MessageBox.TYPE_INFO,
129 # We don't want to stop updating just because one feed is broken
132 import traceback, sys
133 traceback.print_exc(file=sys.stdout)
134 # Errorback given, call it (asumme we don't need do restart timer!)
135 if errorback is not None:
138 # Assume its just a temporary failure and jump over to next feed
141 def gotPage(self, data, id = None):
142 feed = cElementTree_fromstring(data)
146 self.feeds[id].gotFeed(feed)
147 print("[SimpleRSS] single feed parsed...")
150 new_items = self.feeds[self.current_feed].gotFeed(feed)
152 print("[SimpleRSS] feed parsed...")
154 # Append new items to locally bound ones
155 if new_items is not None:
156 self.newItemFeed.history.extend(new_items)
158 # Start Timer so we can either fetch next feed or show new_items
161 def singlePoll(self, id, callback = False, errorback = None):
162 getPage(self.feeds[id].uri).addCallback(self._gotPage, id, callback, errorback).addErrback(errorback)
165 # Reloading, reschedule
167 print("[SimpleRSS] timer triggered while reloading, rescheduling")
168 self.poll_timer.start(10000, 1)
170 elif len(self.feeds) <= self.current_feed:
172 if self.newItemFeed.history:
173 print("[SimpleRSS] got new items, calling back")
177 update_notification_value = config.plugins.simpleRSS.update_notification.value
178 if update_notification_value == "preview":
179 from RSSScreens import RSSFeedView
181 from Tools.Notifications import AddNotificationWithID, RemovePopup
183 RemovePopup(NOTIFICATIONID)
185 AddNotificationWithID(
191 elif update_notification_value == "notification":
193 _("Received %d new news item(s).") % (len(self.newItemFeed.history)),
194 MessageBox.TYPE_INFO,
198 elif update_notification_value == "ticker":
199 from RSSTickerView import tickerView
201 print("[SimpleRSS] missing ticker instance, something with my code is wrong :-/")
203 tickerView.display(self.newItemFeed)
206 print("[SimpleRSS] no new items")
208 self.current_feed = 0
209 self.poll_timer.startLongTimer(config.plugins.simpleRSS.interval.value*60)
212 # Assume we're cleaning history if current feed is 0
213 clearHistory = self.current_feed == 0
214 if config.plugins.simpleRSS.update_notification.value != "none":
215 from Tools import Notifications
216 if hasattr(Notifications, 'notificationQueue'):
217 notifications = Notifications.notificationQueue.queue
218 current_notifications = Notifications.notificationQueue.current
219 handler = lambda note: (note.fnc, note.screen, note.args, note.kwargs, note.id)
220 handler_current = lambda note_tup: (note[0].id,)
222 notifications = Notifications.notifications
223 current_notifications = Notifications.current_notifications
224 handler_current = handler = lambda note: note
226 for x in current_notifications:
227 if handler_current(x)[0] == NOTIFICATIONID:
228 print("[SimpleRSS] timer triggered while preview on screen, rescheduling")
229 self.poll_timer.start(10000, 1)
233 for x in notifications:
234 if handler(x)[4] == NOTIFICATIONID:
235 print("[SimpleRSS] wont wipe history because it was never read")
240 del self.newItemFeed.history[:]
242 # Feed supposed to autoupdate
243 feed = self.feeds[self.current_feed]
246 getPage(feed.uri).addCallback(self._gotPage).addErrback(self.error)
249 print("[SimpleRSS] passing feed")
253 self.current_feed += 1
254 self.poll_timer.start(1000, 1)
257 self.poll_timer.callback.remove(self.poll)
258 self.poll_timer = None
261 def triggerReload(self):
262 self.reloading = True
265 oldfeeds = self.feeds
267 for x in config.plugins.simpleRSS.feed:
268 for feed in oldfeeds:
269 if x.uri.value == feed.uri:
270 # Update possibly different autoupdate value
271 feed.autoupdate = x.autoupdate.value
272 newfeeds.append(feed) # Append to new Feeds
273 oldfeeds.remove(feed) # Remove from old Feeds
284 self.feeds = newfeeds
286 if config.plugins.simpleRSS.enable_google_reader.value:
287 self.googleReader = GoogleReader(config.plugins.simpleRSS.google_username.value, config.plugins.simpleRSS.google_password.value)
288 self.googleReader.login().addCallback(self.googleLoggedIn).addErrback(self.googleLoginFailed)
290 self.reloading = False