Update inkycal_rss.py
This commit is contained in:
parent
fe1a688def
commit
46282f8c61
@ -1,49 +1,85 @@
|
|||||||
"""Add rss-feeds at the bottom section of the Calendar"""
|
#!/usr/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
RSS module for Inky-Calendar software.
|
||||||
|
Copyright by aceisace
|
||||||
|
"""
|
||||||
|
from __future__ import print_function
|
||||||
import feedparser
|
import feedparser
|
||||||
from random import shuffle
|
from random import shuffle
|
||||||
from settings import *
|
from settings import *
|
||||||
from configuration import *
|
from configuration import *
|
||||||
|
|
||||||
|
fontsize = 14
|
||||||
|
|
||||||
|
"""Add a border to increase readability"""
|
||||||
|
border_top = int(bottom_section_height * 0.05)
|
||||||
|
border_left = int(bottom_section_width * 0.02)
|
||||||
|
|
||||||
|
"""Choose font optimised for the weather section"""
|
||||||
|
font = ImageFont.truetype(NotoSans+'Light.ttf', fontsize)
|
||||||
|
space_between_lines = 1
|
||||||
|
line_height = font.getsize('hg')[1] + space_between_lines
|
||||||
|
line_width = bottom_section_width - (border_left*2)
|
||||||
|
|
||||||
"""Find out how many lines can fit at max in the bottom section"""
|
"""Find out how many lines can fit at max in the bottom section"""
|
||||||
lines = bottom_section_height // line_height
|
max_lines = (bottom_section_height - (border_top*2)) // (font.getsize('hg')[1]
|
||||||
"""Create and fill a dictionary of the positions of each line"""
|
+ space_between_lines)
|
||||||
line_pos = {}
|
|
||||||
for i in range(lines):
|
|
||||||
y = bottom_section_offset + i * line_height
|
|
||||||
line_pos['pos' + str(i+1)] = (x_padding, y)
|
|
||||||
|
|
||||||
if bottom_section == "RSS" and rss_feeds != []:
|
"""Calculate the height padding so the lines look centralised"""
|
||||||
"""Parse the RSS-feed titles & summaries and save them to a list"""
|
y_padding = int( (bottom_section_height % line_height) / 2 )
|
||||||
rss_feed = []
|
|
||||||
for feeds in rss_feeds:
|
"""Create a list containing positions of each line"""
|
||||||
text = feedparser.parse(feeds)
|
line_positions = [(border_left, bottom_section_offset +
|
||||||
for posts in text.entries:
|
border_top + y_padding + _*line_height ) for _ in range(max_lines)]
|
||||||
rss_feed.append('{0}: {1}'.format(posts.title, posts.summary))
|
|
||||||
del rss_feed[lines:]
|
def main():
|
||||||
shuffle(rss_feed)
|
if bottom_section == "RSS" and rss_feeds != [] and internet_available() == True:
|
||||||
|
try:
|
||||||
|
print('RSS module: Connectivity check passed. Generating image...',
|
||||||
|
end = '')
|
||||||
|
|
||||||
|
"""Parse the RSS-feed titles & summaries and save them to a list"""
|
||||||
|
parsed_feeds = []
|
||||||
|
for feeds in rss_feeds:
|
||||||
|
text = feedparser.parse(feeds)
|
||||||
|
for posts in text.entries:
|
||||||
|
parsed_feeds.append('•{0}: {1}'.format(posts.title, posts.summary))
|
||||||
|
|
||||||
|
"""Shuffle the list, then crop it to the max number of lines"""
|
||||||
|
shuffle(parsed_feeds)
|
||||||
|
del parsed_feeds[max_lines:]
|
||||||
|
|
||||||
|
|
||||||
"""Check the lenght of each feed. Wrap the text if it doesn't fit on one line"""
|
"""Check the lenght of each feed. Wrap the text if it doesn't fit on one line"""
|
||||||
flatten = lambda z: [x for y in z for x in y]
|
flatten = lambda z: [x for y in z for x in y]
|
||||||
filtered, counter = [], 0
|
filtered_feeds, counter = [], 0
|
||||||
|
|
||||||
for posts in rss_feed:
|
for posts in parsed_feeds:
|
||||||
wrapped = text_wrap(posts)
|
wrapped = text_wrap(posts, font = font, line_width = line_width)
|
||||||
counter += len(filtered) + len(wrapped)
|
counter += len(filtered_feeds) + len(wrapped)
|
||||||
if counter < lines:
|
if counter < max_lines:
|
||||||
filtered.append(wrapped)
|
filtered_feeds.append(wrapped)
|
||||||
filtered = flatten(filtered)
|
filtered_feeds = flatten(filtered_feeds)
|
||||||
|
|
||||||
|
"""Write the correctly formatted text on the display"""
|
||||||
|
for _ in range(len(filtered_feeds)):
|
||||||
|
write_text(line_width, line_height, filtered_feeds[_],
|
||||||
|
line_positions[_], font = font, alignment= 'left')
|
||||||
|
|
||||||
## for i in lines: # Show line lenght and content of each line
|
del filtered_feeds, parsed_feeds
|
||||||
## print(i, ' ' * (line-len(i)),'| height: ',default.getsize(i)[1])
|
|
||||||
|
|
||||||
"""Write the correctly formatted text on the display"""
|
rss_image = image.crop((0,bottom_section_offset, display_width,
|
||||||
for i in range(len(filtered)):
|
display_height))
|
||||||
write_text(display_width, default.getsize('hg')[1],
|
rss_image.save(image_path+'rss.png')
|
||||||
' '+filtered[i], line_pos['pos'+str(i+1)], alignment= 'left')
|
print('Done')
|
||||||
|
|
||||||
image.crop((0,bottom_section_offset, display_width, display_height)).save(
|
except Exception as e:
|
||||||
'rss.png')
|
"""If something went wrong, print a Error message on the Terminal"""
|
||||||
|
print('Failed!')
|
||||||
|
print('Error in RSS module!')
|
||||||
|
print('Reason: ',e)
|
||||||
|
pass
|
||||||
|
|
||||||
del filtered, rss_feed
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user