Newspaper

Right-clicking will open a personalized newspaper with all articles
of your feeds
This commit is contained in:
Lonesome byte 2019-08-19 08:27:44 +02:00
parent da99ab4af5
commit b7493e8519

View file

@ -23,6 +23,7 @@ import os
import tempfile import tempfile
import logging import logging
import random import random
import re
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
@ -33,13 +34,13 @@ import bumblebee.engine
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
REFRESH_DELAY = 600 REFRESH_DELAY = 600
SCROLL_SPEED = 3 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): 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 if DEPENDENCIES_OK else self._show_error) 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', 'https://www.espn.com/espn/rss/news').split(" ")
self._refresh_countdown = 0 self._refresh_countdown = 0
self._feeds_to_update = [] self._feeds_to_update = []
@ -63,14 +64,31 @@ 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 _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('<img[^>]*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): 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'], 'link': entry['link'],
'new': all([i['title'] != entry['title'] for i in self._items]), 'new': all([i['title'] != entry['title'] for i in self._items]),
'source': url, 'source': url,
'summary': entry['summary'], 'summary': self._remove_tags(entry['summary']),
'feed': feed, '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} '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):
@ -161,7 +179,7 @@ class Module(bumblebee.engine.Module):
def state(self, _): def state(self, _):
return self._state return self._state
def _create_news_element(self, item): def _create_news_element(self, item, overlay_title):
try: try:
timestr = "" if item['published'] == 0 else str(time.ctime(item['published'])) timestr = "" if item['published'] == 0 else str(time.ctime(item['published']))
except Exception as e: except Exception as e:
@ -170,7 +188,7 @@ class Module(bumblebee.engine.Module):
element = "<div class='item' onclick=window.open('"+item['link']+"')>" element = "<div class='item' onclick=window.open('"+item['link']+"')>"
element += "<div class='titlecontainer'>" element += "<div class='titlecontainer'>"
element += " <img "+("" if item['image'] else "class='noimg' ")+"src='"+item['image']+"'>" element += " <img "+("" if item['image'] else "class='noimg' ")+"src='"+item['image']+"'>"
element += " <div class='title'>"+item['title']+"</div>" element += " <div class='title"+(" overlay" if overlay_title else "")+"'>"+item['title']+"</div>"
element += "</div>" element += "</div>"
element += "<div class='summary'>"+item['summary']+"</div>" element += "<div class='summary'>"+item['summary']+"</div>"
element += "<div class='info'><span class='author'>"+item['feed']+"</span><span class='published'>"+timestr+"</span></div>" element += "<div class='info'><span class='author'>"+item['feed']+"</span><span class='published'>"+timestr+"</span></div>"
@ -184,7 +202,7 @@ class Module(bumblebee.engine.Module):
section += "<td><div class='itemcontainer'>" section += "<td><div class='itemcontainer'>"
for j in range(0, self.LAYOUT_STYLES_ITEMS[style][i]): for j in range(0, self.LAYOUT_STYLES_ITEMS[style][i]):
if newspaper_items: 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] del newspaper_items[0]
section += "</div></td>" section += "</div></td>"
section += "</tr></table>" section += "</tr></table>"
@ -201,7 +219,7 @@ class Module(bumblebee.engine.Module):
HTML_TEMPLATE = """<!DOCTYPE html> HTML_TEMPLATE = """<!DOCTYPE html>
<html> <html>
<style> <style>
body {background: #fff; font-family: Helvetica neue;} body {background: #eee; font-family: Helvetica neue;}
td {background: #fff; height: 100%;} td {background: #fff; height: 100%;}
tr.style0 td {width: 33%;} tr.style0 td {width: 33%;}
tr.style1 td {width: 20%;} tr.style1 td {width: 20%;}
@ -211,15 +229,16 @@ tr.style2 td:first-child {width: 60%;}
tr.style3 td {width: 20%;} tr.style3 td {width: 20%;}
tr.style3 td:nth-child(2) {width: 60%;} tr.style3 td:nth-child(2) {width: 60%;}
img {width: 100%; display: block; } img {width: 100%; display: block; }
img.noimg {min-height:250px;} img.noimg {min-height:250px; background: #1299c8;}
#content {width: 1500px; margin: auto; background: #eee; padding: 1px;} #content {width: 1500px; margin: auto; background: #eee; padding: 1px;}
#newspapertitle {text-align: center; font-size: 60px; font-family: Arial Black; background: #1299c8; font-style: Italic; padding: 10px; margin: 20px; color: #fff; } #newspapertitle {text-align: center; font-size: 60px; font-family: Arial Black; background: #1299c8; font-style: Italic; padding: 10px; color: #fff; }
.section {display: flex;} .section {display: flex;}
.column {display: flex;} .column {display: flex;}
.itemcontainer {height: 100%; position: relative; display: inline-table;} .itemcontainer {width: 100%; height: 100%; position: relative; display: inline-table;}
.item {cursor: pointer; } .item {cursor: pointer; }
.titlecontainer {position: relative; background: #1299c8;} .titlecontainer {position: relative;}
.title {font-family: Arial; position: absolute; bottom: 10px; color: #fff; font-weight: bold; text-align: right; max-width: 75%; right: 10px; font-size: 23px; text-shadow: 1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0 #000;} .title.overlay {font-family: Arial; position: absolute; bottom: 10px; color: #fff; font-weight: bold; text-align: right; max-width: 75%; right: 10px; font-size: 23px; text-shadow: 1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0 #000;}
.title:not(.overlay) {font-weight: bold; padding: 0px 10px;}
.summary {color: #444; padding: 10px 10px 0px 10px; font-family: Times new roman; font-size: 18px; flex: 1;max-height: 105px; overflow: hidden;} .summary {color: #444; padding: 10px 10px 0px 10px; font-family: Times new roman; font-size: 18px; flex: 1;max-height: 105px; overflow: hidden;}
.info {color: #aaa; font-family: arial; font-size: 13px; padding: 10px;} .info {color: #aaa; font-family: arial; font-size: 13px; padding: 10px;}
.published {float: right;} .published {float: right;}