From ba7e7d258d1aace231c591136edeabd9e3d5cb1b Mon Sep 17 00:00:00 2001 From: Lonesome byte Date: Fri, 16 Aug 2019 10:11:22 +0200 Subject: [PATCH] Reworked code 2 --- bumblebee/modules/rss.py | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/bumblebee/modules/rss.py b/bumblebee/modules/rss.py index 4c7627a..6045ed2 100644 --- a/bumblebee/modules/rss.py +++ b/bumblebee/modules/rss.py @@ -4,7 +4,7 @@ Fetches rss news items and shows these as a news ticker. Left-clicking will open the full story in a browser. -New stories are highlighted. +New stories are highlighted. Parameters: * rss.feeds : Space-separated list of RSS URLs @@ -19,7 +19,6 @@ except ImportError: import webbrowser import time -import os import bumblebee.input import bumblebee.output @@ -33,7 +32,7 @@ class Module(bumblebee.engine.Module): def __init__(self, engine, config): super(Module, self).__init__(engine, config, - bumblebee.output.Widget(full_text=self.ticker_update) + bumblebee.output.Widget(full_text=self.ticker_update if DEPENDENCIES_OK else self._show_error) ) # Use BBC newsfeed as demo: self._feeds = self.parameter('feeds', 'http://feeds.bbci.co.uk/news/rss.xml').split(" ") @@ -56,17 +55,16 @@ class Module(bumblebee.engine.Module): if self._current_item: webbrowser.open(self._current_item['link']) + def _create_item(self, entry, url): + return {'title': entry['title'].replace('\n', ' '), + 'link': entry['link'], + 'new': all([i['title'] != entry['title'] for i in self._items]), + 'source': url, + 'published': time.mktime(entry.published_parsed) if hasattr(entry, 'published_parsed') else 0} + def _update_items_from_feed(self, url): parser = feedparser.parse(url) - old_titles = [i['title'] for i in self._items if i['source'] == url] - - new_items = [{'title': i['title'].replace('\n', ' '), - 'link': i['link'], - 'new': i['title'] not in old_titles, - 'source': url, - 'published': time.mktime(i.published_parsed) if hasattr(i, 'published_parsed') else 0} - for i in parser['entries']] - + new_items = [self._create_item(entry, url) for entry in parser['entries']] # Remove the previous items self._items = [i for i in self._items if i['source'] != url] # Add the new items @@ -75,11 +73,7 @@ class Module(bumblebee.engine.Module): self._items.sort(key=lambda i: i['published'], reverse=True) def _check_for_refresh(self): - if not DEPENDENCIES_OK: - self._items = [{'title': 'Please install feedparser first', 'new':True, 'published': 0, - 'link': 'https://pypi.org/project/feedparser/'}] - self._current_item = self._items[0] - elif self._feeds_to_update: + if self._feeds_to_update: # Update one feed at a time to not overload this update cycle url = self._feeds_to_update.pop() self._update_items_from_feed(url) @@ -122,6 +116,9 @@ class Module(bumblebee.engine.Module): # Increase scroll position self._ticker_offset += self.SCROLL_SPEED + def _show_error(self, _): + return "Please install feedparser first" + def ticker_update(self, _): self._check_for_refresh() @@ -143,7 +140,6 @@ class Module(bumblebee.engine.Module): return response self._state = [] - self._check_scroll_done() return response