Reworked code 2

This commit is contained in:
Lonesome byte 2019-08-16 10:11:22 +02:00
parent d1fc0f2575
commit ba7e7d258d

View file

@ -19,7 +19,6 @@ except ImportError:
import webbrowser import webbrowser
import time import time
import os
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
@ -33,7 +32,7 @@ class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
super(Module, self).__init__(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: # Use BBC newsfeed as demo:
self._feeds = self.parameter('feeds', 'http://feeds.bbci.co.uk/news/rss.xml').split(" ") 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: if self._current_item:
webbrowser.open(self._current_item['link']) 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): def _update_items_from_feed(self, url):
parser = feedparser.parse(url) parser = feedparser.parse(url)
old_titles = [i['title'] for i in self._items if i['source'] == url] new_items = [self._create_item(entry, url) for entry in parser['entries']]
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']]
# Remove the previous items # Remove the previous items
self._items = [i for i in self._items if i['source'] != url] self._items = [i for i in self._items if i['source'] != url]
# Add the new items # Add the new items
@ -75,11 +73,7 @@ class Module(bumblebee.engine.Module):
self._items.sort(key=lambda i: i['published'], reverse=True) self._items.sort(key=lambda i: i['published'], reverse=True)
def _check_for_refresh(self): def _check_for_refresh(self):
if not DEPENDENCIES_OK: if self._feeds_to_update:
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:
# Update one feed at a time to not overload this update cycle # Update one feed at a time to not overload this update cycle
url = self._feeds_to_update.pop() url = self._feeds_to_update.pop()
self._update_items_from_feed(url) self._update_items_from_feed(url)
@ -122,6 +116,9 @@ class Module(bumblebee.engine.Module):
# Increase scroll position # Increase scroll position
self._ticker_offset += self.SCROLL_SPEED self._ticker_offset += self.SCROLL_SPEED
def _show_error(self, _):
return "Please install feedparser first"
def ticker_update(self, _): def ticker_update(self, _):
self._check_for_refresh() self._check_for_refresh()
@ -143,7 +140,6 @@ class Module(bumblebee.engine.Module):
return response return response
self._state = [] self._state = []
self._check_scroll_done() self._check_scroll_done()
return response return response