| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  | #!python3 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2020-12-05 00:21:44 +01:00
										 |  |  | Feeds module for InkyCal Project | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | Copyright by aceisace | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2020-12-02 01:00:28 +01:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | from inkycal.modules.template import inkycal_module | 
					
						
							| 
									
										
										
										
											2020-05-12 19:35:08 +02:00
										 |  |  | from inkycal.custom import * | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | from random import shuffle | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  | import feedparser | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 02:56:04 +02:00
										 |  |  | logger = logging.getLogger(__name__) | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | class Feeds(inkycal_module): | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     """RSS class
 | 
					
						
							|  |  |  |     parses rss/atom feeds from given urls | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2020-05-12 19:35:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     name = "RSS / Atom - Display feeds from given RSS/ATOM feeds" | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     requires = { | 
					
						
							|  |  |  |         "feed_urls": { | 
					
						
							|  |  |  |             "label": "Please enter ATOM or RSS feed URL/s, separated by a comma", | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     optional = { | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         "shuffle_feeds": { | 
					
						
							|  |  |  |             "label": "Should the parsed RSS feeds be shuffled? (default=True)", | 
					
						
							|  |  |  |             "options": [True, False], | 
					
						
							|  |  |  |             "default": True | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         """Initialize inkycal_feeds module""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super().__init__(config) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         config = config['config'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Check if all required parameters are present | 
					
						
							|  |  |  |         for param in self.requires: | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  |             if param not in config: | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |                 raise Exception(f'config is missing {param}') | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # required parameters | 
					
						
							|  |  |  |         if config["feed_urls"] and isinstance(config['feed_urls'], str): | 
					
						
							|  |  |  |             self.feed_urls = config["feed_urls"].split(",") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.feed_urls = config["feed_urls"] | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # optional parameters | 
					
						
							|  |  |  |         self.shuffle_feeds = config["shuffle_feeds"] | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # give an OK message | 
					
						
							| 
									
										
										
										
											2022-10-03 02:56:04 +02:00
										 |  |  |         print(f'{__name__} loaded') | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     def _validate(self): | 
					
						
							|  |  |  |         """Validate module-specific parameters""" | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         if not isinstance(self.shuffle_feeds, bool): | 
					
						
							|  |  |  |             print('shuffle_feeds has to be a boolean: True/False') | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     def generate_image(self): | 
					
						
							|  |  |  |         """Generate image for this module""" | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Define new image size with respect to padding | 
					
						
							|  |  |  |         im_width = int(self.width - (2 * self.padding_left)) | 
					
						
							|  |  |  |         im_height = int(self.height - (2 * self.padding_top)) | 
					
						
							|  |  |  |         im_size = im_width, im_height | 
					
						
							|  |  |  |         logger.info(f'Image size: {im_size}') | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Create an image for black pixels and one for coloured pixels | 
					
						
							|  |  |  |         im_black = Image.new('RGB', size=im_size, color='white') | 
					
						
							|  |  |  |         im_colour = Image.new('RGB', size=im_size, color='white') | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Check if internet is available | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  |         if internet_available(): | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             logger.info('Connection test passed') | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  |             raise NetworkNotReachableError | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Set some parameters for formatting feeds | 
					
						
							|  |  |  |         line_spacing = 1 | 
					
						
							|  |  |  |         line_height = self.font.getsize('hg')[1] + line_spacing | 
					
						
							|  |  |  |         line_width = im_width | 
					
						
							|  |  |  |         max_lines = (im_height // (self.font.getsize('hg')[1] + line_spacing)) | 
					
						
							| 
									
										
										
										
											2020-05-12 19:35:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Calculate padding from top so the lines look centralised | 
					
						
							|  |  |  |         spacing_top = int(im_height % line_height / 2) | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Calculate line_positions | 
					
						
							|  |  |  |         line_positions = [ | 
					
						
							|  |  |  |             (0, spacing_top + _ * line_height) for _ in range(max_lines)] | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Create list containing all feeds from all urls | 
					
						
							|  |  |  |         parsed_feeds = [] | 
					
						
							|  |  |  |         for feeds in self.feed_urls: | 
					
						
							|  |  |  |             text = feedparser.parse(feeds) | 
					
						
							|  |  |  |             for posts in text.entries: | 
					
						
							| 
									
										
										
										
											2022-04-14 04:57:55 +02:00
										 |  |  |                 if "summary" in posts: | 
					
						
							|  |  |  |                     summary = posts["summary"] | 
					
						
							|  |  |  |                     parsed_feeds.append(f"•{posts.title}: {re.sub('<[^<]+?>', '', posts.summary)}") | 
					
						
							| 
									
										
										
										
											2022-10-03 01:03:24 +02:00
										 |  |  |                 # if "description" in posts: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-10 22:58:01 +01:00
										 |  |  |         if parsed_feeds: | 
					
						
							|  |  |  |             parsed_feeds = [i.split("\n") for i in parsed_feeds][0] | 
					
						
							|  |  |  |             parsed_feeds = [i for i in parsed_feeds if i] | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Shuffle the list to prevent showing the same content | 
					
						
							| 
									
										
										
										
											2022-04-10 06:35:08 +02:00
										 |  |  |         if self.shuffle_feeds: | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             shuffle(parsed_feeds) | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Trim down the list to the max number of lines | 
					
						
							|  |  |  |         del parsed_feeds[max_lines:] | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Wrap long text from feeds (line-breaking) | 
					
						
							|  |  |  |         flatten = lambda z: [x for y in z for x in y] | 
					
						
							|  |  |  |         filtered_feeds, counter = [], 0 | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         for posts in parsed_feeds: | 
					
						
							|  |  |  |             wrapped = text_wrap(posts, font=self.font, max_width=line_width) | 
					
						
							|  |  |  |             counter += len(wrapped) | 
					
						
							|  |  |  |             if counter < max_lines: | 
					
						
							|  |  |  |                 filtered_feeds.append(wrapped) | 
					
						
							|  |  |  |         filtered_feeds = flatten(filtered_feeds) | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         logger.debug(f'filtered feeds -> {filtered_feeds}') | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # Check if feeds could be parsed and can be displayed | 
					
						
							|  |  |  |         if len(filtered_feeds) == 0 and len(parsed_feeds) > 0: | 
					
						
							|  |  |  |             print('Feeds could be parsed, but the text is too long to be displayed:/') | 
					
						
							|  |  |  |         elif len(filtered_feeds) == 0 and len(parsed_feeds) == 0: | 
					
						
							|  |  |  |             print('No feeds could be parsed :/') | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # Write feeds on image | 
					
						
							|  |  |  |             for _ in range(len(filtered_feeds)): | 
					
						
							|  |  |  |                 write(im_black, line_positions[_], (line_width, line_height), | 
					
						
							|  |  |  |                       filtered_feeds[_], font=self.font, alignment='left') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # return images | 
					
						
							|  |  |  |         return im_black, im_colour | 
					
						
							| 
									
										
										
										
											2020-11-10 11:53:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2022-10-03 02:56:04 +02:00
										 |  |  |     print(f'running {__name__} in standalone/debug mode') |