From da99ab4af5095f42ce7b4bbd3cebb878c067da3a Mon Sep 17 00:00:00 2001 From: Lonesome byte Date: Sat, 17 Aug 2019 17:53:40 +0200 Subject: [PATCH] First version --- bumblebee/modules/rss.py | 68 ++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/bumblebee/modules/rss.py b/bumblebee/modules/rss.py index 05eb5a1..bec9dc7 100644 --- a/bumblebee/modules/rss.py +++ b/bumblebee/modules/rss.py @@ -22,6 +22,7 @@ import time import os import tempfile import logging +import random import bumblebee.input import bumblebee.output @@ -32,7 +33,7 @@ import bumblebee.engine class Module(bumblebee.engine.Module): REFRESH_DELAY = 600 SCROLL_SPEED = 3 - + LAYOUT_STYLES_ITEMS = [[1,1,1],[2,2,1],[1,2,2],[2,1,2]] def __init__(self, engine, config): super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.ticker_update if DEPENDENCIES_OK else self._show_error) @@ -62,19 +63,19 @@ class Module(bumblebee.engine.Module): if self._current_item: webbrowser.open(self._current_item['link']) - def _create_item(self, entry, url): + def _create_item(self, entry, url, feed): return {'title': entry['title'].replace('\n', ' '), 'link': entry['link'], 'new': all([i['title'] != entry['title'] for i in self._items]), 'source': url, - 'summary': i['summary'], - 'feed': parser['feed']['title'], - 'image': next(iter([l['href'] for l in i['links'] if l['rel']=='enclosure']), ''), + 'summary': entry['summary'], + 'feed': feed, + 'image': next(iter([l['href'] for l in entry['links'] if l['rel']=='enclosure']), ''), 'published': time.mktime(entry.published_parsed) if hasattr(entry, 'published_parsed') else 0} def _update_items_from_feed(self, url): parser = feedparser.parse(url) - new_items = [self._create_item(entry, url) for entry in parser['entries']] + new_items = [self._create_item(entry, url, parser['feed']['title']) 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 @@ -162,43 +163,70 @@ class Module(bumblebee.engine.Module): def _create_news_element(self, item): try: - logging.error("aaaaaaaa") - timestr = "" if item['published'] == 0 else str(time.ctime(1565783383)) + timestr = "" if item['published'] == 0 else str(time.ctime(item['published'])) except Exception as e: logging.error(str(e)) raise e element = "
" element += "
" - element += " " + element += " " element += "
"+item['title']+"
" element += "
" - element += "
"+item['feed']+""+timestr+"
" element += "
"+item['summary']+"
" + element += "
"+item['feed']+""+timestr+"
" element += "
" return element + def _create_news_section(self, newspaper_items): + style = random.randint(0, 3) + section = "" + for i in range(0, 3): + section += "" + section += "
" + for j in range(0, self.LAYOUT_STYLES_ITEMS[style][i]): + if newspaper_items: + section += self._create_news_element(newspaper_items[0]) + del newspaper_items[0] + section += "
" + return section + def _create_newspaper(self, _): content = "" - for item in self._items: - content += self._create_news_element(item) + newspaper_items = self._items[:] + while newspaper_items: + content += self._create_news_section(newspaper_items) open(self._newspaper_filename, "w").write(HTML_TEMPLATE.replace("[[CONTENT]]", content)) webbrowser.open("file://"+self._newspaper_filename) HTML_TEMPLATE = """ -
Bumblebee Daily
+
Bumblebee Daily
[[CONTENT]]