From 46282f8c61a019a5b5531d1e066648494f2f3b4b Mon Sep 17 00:00:00 2001 From: Ace Date: Mon, 25 Nov 2019 21:32:55 +0100 Subject: [PATCH] Update inkycal_rss.py --- Inky-Calendar/modules/inkycal_rss.py | 106 ++++++++++++++++++--------- 1 file changed, 71 insertions(+), 35 deletions(-) diff --git a/Inky-Calendar/modules/inkycal_rss.py b/Inky-Calendar/modules/inkycal_rss.py index 9ad8392..2397a3b 100644 --- a/Inky-Calendar/modules/inkycal_rss.py +++ b/Inky-Calendar/modules/inkycal_rss.py @@ -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 from random import shuffle from settings 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""" -lines = bottom_section_height // line_height -"""Create and fill a dictionary of the positions of each line""" -line_pos = {} -for i in range(lines): - y = bottom_section_offset + i * line_height - line_pos['pos' + str(i+1)] = (x_padding, y) +max_lines = (bottom_section_height - (border_top*2)) // (font.getsize('hg')[1] + + space_between_lines) -if bottom_section == "RSS" and rss_feeds != []: - """Parse the RSS-feed titles & summaries and save them to a list""" - rss_feed = [] - for feeds in rss_feeds: - text = feedparser.parse(feeds) - for posts in text.entries: - rss_feed.append('{0}: {1}'.format(posts.title, posts.summary)) - del rss_feed[lines:] - shuffle(rss_feed) +"""Calculate the height padding so the lines look centralised""" +y_padding = int( (bottom_section_height % line_height) / 2 ) + +"""Create a list containing positions of each line""" +line_positions = [(border_left, bottom_section_offset + + border_top + y_padding + _*line_height ) for _ in range(max_lines)] + +def main(): + 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""" - flatten = lambda z: [x for y in z for x in y] - filtered, counter = [], 0 + """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] + filtered_feeds, counter = [], 0 - for posts in rss_feed: - wrapped = text_wrap(posts) - counter += len(filtered) + len(wrapped) - if counter < lines: - filtered.append(wrapped) - filtered = flatten(filtered) + for posts in parsed_feeds: + wrapped = text_wrap(posts, font = font, line_width = line_width) + counter += len(filtered_feeds) + len(wrapped) + if counter < max_lines: + filtered_feeds.append(wrapped) + 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 -## print(i, ' ' * (line-len(i)),'| height: ',default.getsize(i)[1]) + del filtered_feeds, parsed_feeds -"""Write the correctly formatted text on the display""" - for i in range(len(filtered)): - write_text(display_width, default.getsize('hg')[1], - ' '+filtered[i], line_pos['pos'+str(i+1)], alignment= 'left') - - image.crop((0,bottom_section_offset, display_width, display_height)).save( - 'rss.png') + rss_image = image.crop((0,bottom_section_offset, display_width, + display_height)) + rss_image.save(image_path+'rss.png') + print('Done') - del filtered, rss_feed + except Exception as e: + """If something went wrong, print a Error message on the Terminal""" + print('Failed!') + print('Error in RSS module!') + print('Reason: ',e) + pass + +if __name__ == '__main__': + main()