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.
This commit is contained in:
Lonesome byte 2019-08-19 19:15:24 +02:00
parent 99f3bbefcc
commit 4b73ff61fa

View file

@ -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)
@ -62,6 +65,24 @@ 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'])
@ -75,7 +96,7 @@ class Module(bumblebee.engine.Module):
except Exception:
pass
if not image:
match = re.search('<img[^>]*src\s*=["\']*([^\s^>^"^\']*)["\']*', entry['summary'])
match = re.search(r'<img[^>]*src\s*=["\']*([^\s^>^"^\']*)["\']*', entry['summary'])
if match:
image = match.group(1)
return image if image else ''
@ -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 = "<div class='item' onclick=window.open('"+item['link']+"')>"
element += "<div class='titlecontainer'>"
@ -207,7 +233,7 @@ class Module(bumblebee.engine.Module):
section = "<table><tr class='style"+str(style)+"'>"
for i in range(0, 3):
section += "<td><div class='itemcontainer'>"
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)
del newspaper_items[0]
@ -225,6 +251,19 @@ class Module(bumblebee.engine.Module):
HTML_TEMPLATE = """<!DOCTYPE html>
<html>
<head>
<script>
window.onload = function() {
var images = document.getElementsByTagName('img');
for(var i = 0; i < images.length; i++) {
if (images[i].naturalWidth<50 || images[i].naturalHeight<50) {
images[i].src = ''
images[i].className+=' noimg'
}
}
}
</script>
</head>
<style>
body {background: #eee; font-family: Helvetica neue;}
td {background: #fff; height: 100%;}