From 5d1422adab777b89f91541b8209455521d3124c5 Mon Sep 17 00:00:00 2001 From: Lonesome byte Date: Thu, 15 Aug 2019 18:00:32 +0200 Subject: [PATCH 1/6] First beta version --- bumblebee/modules/rss.py | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/bumblebee/modules/rss.py b/bumblebee/modules/rss.py index 6045ed2..05eb5a1 100644 --- a/bumblebee/modules/rss.py +++ b/bumblebee/modules/rss.py @@ -19,6 +19,9 @@ except ImportError: import webbrowser import time +import os +import tempfile +import logging import bumblebee.input import bumblebee.output @@ -49,7 +52,11 @@ class Module(bumblebee.engine.Module): self._post_delay = 0 self._state = [] + + self._newspaper_filename = tempfile.mktemp('.html') + engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd=self._open) + engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self._create_newspaper) def _open(self, _): if self._current_item: @@ -60,6 +67,9 @@ class Module(bumblebee.engine.Module): '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']), ''), 'published': time.mktime(entry.published_parsed) if hasattr(entry, 'published_parsed') else 0} def _update_items_from_feed(self, url): @@ -150,4 +160,47 @@ class Module(bumblebee.engine.Module): def state(self, _): return self._state + def _create_news_element(self, item): + try: + logging.error("aaaaaaaa") + timestr = "" if item['published'] == 0 else str(time.ctime(1565783383)) + except Exception as e: + logging.error(str(e)) + raise e + element = "
" + element += "
" + element += " " + element += "
"+item['title']+"
" + element += "
" + element += "
"+item['feed']+""+timestr+"
" + element += "
"+item['summary']+"
" + element += "
" + return element + + def _create_newspaper(self, _): + content = "" + for item in self._items: + content += self._create_news_element(item) + open(self._newspaper_filename, "w").write(HTML_TEMPLATE.replace("[[CONTENT]]", content)) + webbrowser.open("file://"+self._newspaper_filename) + +HTML_TEMPLATE = """ + + + +
Bumblebee Daily
+
+ [[CONTENT]] +
+ +""" # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From da99ab4af5095f42ce7b4bbd3cebb878c067da3a Mon Sep 17 00:00:00 2001 From: Lonesome byte Date: Sat, 17 Aug 2019 17:53:40 +0200 Subject: [PATCH 2/6] 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]]
From b7493e851929121b3079b5ab970fc9f0cdaf1e55 Mon Sep 17 00:00:00 2001 From: Lonesome byte Date: Mon, 19 Aug 2019 08:27:44 +0200 Subject: [PATCH 3/6] Newspaper Right-clicking will open a personalized newspaper with all articles of your feeds --- bumblebee/modules/rss.py | 79 +++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/bumblebee/modules/rss.py b/bumblebee/modules/rss.py index bec9dc7..0808e23 100644 --- a/bumblebee/modules/rss.py +++ b/bumblebee/modules/rss.py @@ -23,6 +23,7 @@ import os import tempfile import logging import random +import re import bumblebee.input import bumblebee.output @@ -33,13 +34,13 @@ 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]] + LAYOUT_STYLES_ITEMS = [[1,1,1],[3,3,2],[2,3,3],[3,2,3]] 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) ) # Use BBC newsfeed as demo: - self._feeds = self.parameter('feeds', 'http://feeds.bbci.co.uk/news/rss.xml').split(" ") + self._feeds = self.parameter('feeds', 'https://www.espn.com/espn/rss/news').split(" ") self._refresh_countdown = 0 self._feeds_to_update = [] @@ -63,14 +64,31 @@ class Module(bumblebee.engine.Module): if self._current_item: webbrowser.open(self._current_item['link']) + def _check_for_image(self, entry): + image = next(iter([l['href'] for l in entry['links'] if l['rel']=='enclosure']), None) + if not image and 'media_content' in entry: + try: + media = sorted(entry['media_content'], key=lambda i: i['height'] if 'height' in i else 0, reverse=True) + image = next(iter([i['url'] for i in media if i['medium']=='image']), None) + except Exception: + pass + if not image: + match = re.search(']*src\s*=["\']*([^\s^>^"^\']*)["\']*', entry['summary']) + if match: + image=match.group(1) + return image if image else '' + + def _remove_tags(self, txt): + return re.sub('<[^>]*>', '', txt) + def _create_item(self, entry, url, feed): - return {'title': entry['title'].replace('\n', ' '), + return {'title': self._remove_tags(entry['title'].replace('\n', ' ')), 'link': entry['link'], 'new': all([i['title'] != entry['title'] for i in self._items]), 'source': url, - 'summary': entry['summary'], + 'summary': self._remove_tags(entry['summary']), 'feed': feed, - 'image': next(iter([l['href'] for l in entry['links'] if l['rel']=='enclosure']), ''), + 'image': self._check_for_image(entry), 'published': time.mktime(entry.published_parsed) if hasattr(entry, 'published_parsed') else 0} def _update_items_from_feed(self, url): @@ -161,7 +179,7 @@ class Module(bumblebee.engine.Module): def state(self, _): return self._state - def _create_news_element(self, item): + def _create_news_element(self, item, overlay_title): try: timestr = "" if item['published'] == 0 else str(time.ctime(item['published'])) except Exception as e: @@ -170,7 +188,7 @@ class Module(bumblebee.engine.Module): element = "
" element += "
" element += " " - element += "
"+item['title']+"
" + element += "
"+item['title']+"
" element += "
" element += "
"+item['summary']+"
" element += "
"+item['feed']+""+timestr+"
" @@ -184,7 +202,7 @@ class Module(bumblebee.engine.Module): section += "
" for j in range(0, self.LAYOUT_STYLES_ITEMS[style][i]): if newspaper_items: - section += self._create_news_element(newspaper_items[0]) + section += self._create_news_element(newspaper_items[0], self.LAYOUT_STYLES_ITEMS[style][i]!=3) del newspaper_items[0] section += "
" section += "" @@ -201,28 +219,29 @@ class Module(bumblebee.engine.Module): HTML_TEMPLATE = """
From 99f3bbefccdae69da5bfbe4d6ef830899c93a2a3 Mon Sep 17 00:00:00 2001 From: Lonesome byte Date: Mon, 19 Aug 2019 16:06:34 +0200 Subject: [PATCH 4/6] Time-based scrolling and refreshing When you click on other modules, the ticker may scroll faster than once a second. This has been resolved. Refreshing the feeds was update-tick based. This has changed to actual time based. --- bumblebee/modules/rss.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/bumblebee/modules/rss.py b/bumblebee/modules/rss.py index 0808e23..6664893 100644 --- a/bumblebee/modules/rss.py +++ b/bumblebee/modules/rss.py @@ -41,7 +41,6 @@ class Module(bumblebee.engine.Module): ) # Use BBC newsfeed as demo: self._feeds = self.parameter('feeds', 'https://www.espn.com/espn/rss/news').split(" ") - self._refresh_countdown = 0 self._feeds_to_update = [] self._max_title_length = int(self.parameter("length", 60)) @@ -57,6 +56,9 @@ class Module(bumblebee.engine.Module): self._newspaper_filename = tempfile.mktemp('.html') + self._last_refresh = 0 + self._last_update = 0 + engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd=self._open) engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self._create_newspaper) @@ -109,13 +111,11 @@ class Module(bumblebee.engine.Module): if not self._current_item: self._next_item() - elif self._refresh_countdown == 0: + elif time.time()-self._last_refresh >= self.REFRESH_DELAY: # Populate the list with feeds to update self._feeds_to_update = self._feeds[:] - # Restart the update countdown timer - self._refresh_countdown = self.REFRESH_DELAY - else: - self._refresh_countdown -= 1 + # Update the refresh time + self._last_refresh = time.time() def _next_item(self): self._ticker_offset = 0 @@ -149,6 +149,13 @@ class Module(bumblebee.engine.Module): return "Please install feedparser first" def ticker_update(self, _): + # Only update the ticker once a second + now = time.time() + if now-self._last_update < 1: + return self._response + + self._last_update = now + self._check_for_refresh() # If no items were retrieved, return an empty string @@ -156,9 +163,9 @@ class Module(bumblebee.engine.Module): return " "*self._max_title_length # Prepare a substring of the item title - response = self._current_item['title'][self._ticker_offset:self._ticker_offset+self._max_title_length] + self._response = self._current_item['title'][self._ticker_offset:self._ticker_offset+self._max_title_length] # Add spaces if too short - response = response.ljust(self._max_title_length) + self._response = self._response.ljust(self._max_title_length) # Do not immediately scroll if self._pre_delay > 0: @@ -166,12 +173,12 @@ class Module(bumblebee.engine.Module): if self._current_item['new']: self._state = ['warning'] self._pre_delay -= 1 - return response + return self._response self._state = [] self._check_scroll_done() - return response + return self._response def update(self, widgets): pass From 4b73ff61fab484ba55bb3689523b7d5190333834 Mon Sep 17 00:00:00 2001 From: Lonesome byte Date: Mon, 19 Aug 2019 19:15:24 +0200 Subject: [PATCH 5/6] Persistent history History is now written to disk and loaded upon startup. This way old articles aren't listed as new after restart/reboot of Linux or i3. --- bumblebee/modules/rss.py | 63 ++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/bumblebee/modules/rss.py b/bumblebee/modules/rss.py index 6664893..a4d6d89 100644 --- a/bumblebee/modules/rss.py +++ b/bumblebee/modules/rss.py @@ -24,6 +24,7 @@ import tempfile import logging import random import re +import json import bumblebee.input import bumblebee.output @@ -35,6 +36,8 @@ class Module(bumblebee.engine.Module): REFRESH_DELAY = 600 SCROLL_SPEED = 3 LAYOUT_STYLES_ITEMS = [[1,1,1],[3,3,2],[2,3,3],[3,2,3]] + HISTORY_FILENAME = ".config/i3/rss.hist" + 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) @@ -53,7 +56,7 @@ class Module(bumblebee.engine.Module): self._post_delay = 0 self._state = [] - + self._newspaper_filename = tempfile.mktemp('.html') self._last_refresh = 0 @@ -62,22 +65,40 @@ class Module(bumblebee.engine.Module): engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd=self._open) engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self._create_newspaper) + self._history = {} + self._load_history() + + def _load_history(self): + if os.path.isfile(self.HISTORY_FILENAME): + self._history = json.loads(open(self.HISTORY_FILENAME, "r").read()) + + def _save_history(self): + sources = set([i['source'] for i in self._items]) + self._history = dict([[s, [i['title'] for i in self._items if i['source'] == s]] for s in sources]) + if not os.path.exists(os.path.dirname(self.HISTORY_FILENAME)): + os.makedirs(os.path.dirname(self.HISTORY_FILENAME)) + open(self.HISTORY_FILENAME, "w").write(json.dumps(self._history)) + + def _check_history(self, items): + for i in items: + i['new'] &= not (i['source'] in self._history and i['title'] in self._history[i['source']]) + def _open(self, _): if self._current_item: webbrowser.open(self._current_item['link']) def _check_for_image(self, entry): - image = next(iter([l['href'] for l in entry['links'] if l['rel']=='enclosure']), None) + image = next(iter([l['href'] for l in entry['links'] if l['rel'] == 'enclosure']), None) if not image and 'media_content' in entry: try: media = sorted(entry['media_content'], key=lambda i: i['height'] if 'height' in i else 0, reverse=True) - image = next(iter([i['url'] for i in media if i['medium']=='image']), None) + image = next(iter([i['url'] for i in media if i['medium'] == 'image']), None) except Exception: pass if not image: - match = re.search(']*src\s*=["\']*([^\s^>^"^\']*)["\']*', entry['summary']) + match = re.search(r']*src\s*=["\']*([^\s^>^"^\']*)["\']*', entry['summary']) if match: - image=match.group(1) + image = match.group(1) return image if image else '' def _remove_tags(self, txt): @@ -86,7 +107,7 @@ class Module(bumblebee.engine.Module): def _create_item(self, entry, url, feed): return {'title': self._remove_tags(entry['title'].replace('\n', ' ')), 'link': entry['link'], - 'new': all([i['title'] != entry['title'] for i in self._items]), + 'new': True, 'source': url, 'summary': self._remove_tags(entry['summary']), 'feed': feed, @@ -96,6 +117,8 @@ class Module(bumblebee.engine.Module): def _update_items_from_feed(self, url): parser = feedparser.parse(url) new_items = [self._create_item(entry, url, parser['feed']['title']) for entry in parser['entries']] + # Check history + self._check_history(new_items) # Remove the previous items self._items = [i for i in self._items if i['source'] != url] # Add the new items @@ -109,6 +132,9 @@ class Module(bumblebee.engine.Module): url = self._feeds_to_update.pop() self._update_items_from_feed(url) + if not self._feeds_to_update: + self._save_history() + if not self._current_item: self._next_item() elif time.time()-self._last_refresh >= self.REFRESH_DELAY: @@ -189,8 +215,8 @@ class Module(bumblebee.engine.Module): def _create_news_element(self, item, overlay_title): try: timestr = "" if item['published'] == 0 else str(time.ctime(item['published'])) - except Exception as e: - logging.error(str(e)) + except Exception as exc: + logging.error(str(exc)) raise e element = "
" element += "
" @@ -207,14 +233,14 @@ class Module(bumblebee.engine.Module): section = "" for i in range(0, 3): section += "" section += "
" - for j in range(0, self.LAYOUT_STYLES_ITEMS[style][i]): + for _ in range(0, self.LAYOUT_STYLES_ITEMS[style][i]): if newspaper_items: - section += self._create_news_element(newspaper_items[0], self.LAYOUT_STYLES_ITEMS[style][i]!=3) + section += self._create_news_element(newspaper_items[0], self.LAYOUT_STYLES_ITEMS[style][i] != 3) del newspaper_items[0] section += "
" return section - + def _create_newspaper(self, _): content = "" newspaper_items = self._items[:] @@ -222,9 +248,22 @@ class Module(bumblebee.engine.Module): 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 = """ + + +