| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | #!/usr/bin/python3 | 
					
						
							|  |  |  | # -*- coding: utf-8 -*- | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  | RSS module for Inky-Calendar Project | 
					
						
							|  |  |  | Copyright by aceisace | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | try: | 
					
						
							|  |  |  |   import feedparser | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |   print('feedparser is not installed! Please install with:') | 
					
						
							| 
									
										
										
										
											2020-05-13 18:36:54 +02:00
										 |  |  |   print('pip3 install feedparser') | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | filename = os.path.basename(__file__).split('.py')[0] | 
					
						
							|  |  |  | logger = logging.getLogger(filename) | 
					
						
							| 
									
										
										
										
											2020-05-26 19:10:20 +02:00
										 |  |  | logger.setLevel(level=logging.ERROR) | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 19:10:20 +02:00
										 |  |  | class RSS(inkycal_module): | 
					
						
							| 
									
										
										
										
											2020-05-12 19:35:08 +02:00
										 |  |  |   """RSS class
 | 
					
						
							| 
									
										
										
										
											2020-05-26 19:10:20 +02:00
										 |  |  |   parses rss/atom feeds from given urls | 
					
						
							| 
									
										
										
										
											2020-05-12 19:35:08 +02:00
										 |  |  |   """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  |   def __init__(self, section_size, section_config): | 
					
						
							|  |  |  |     """Initialize inkycal_rss module""" | 
					
						
							| 
									
										
										
										
											2020-05-26 19:10:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |     super().__init__(section_size, section_config) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:46:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |     # Module specific parameters | 
					
						
							|  |  |  |     required = ['rss_urls'] | 
					
						
							|  |  |  |     for param in required: | 
					
						
							|  |  |  |       if not param in section_config: | 
					
						
							|  |  |  |         raise Exception('config is missing {}'.format(param)) | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |     # module name | 
					
						
							| 
									
										
										
										
											2020-05-29 03:59:44 +02:00
										 |  |  |     self.name = self.__class__.__name__ | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # module specific parameters | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  |     self.shuffle_feeds = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |     # give an OK message | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  |     print('{0} loaded'.format(self.name)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |   def _validate(self): | 
					
						
							|  |  |  |     """Validate module-specific parameters""" | 
					
						
							|  |  |  |     if not isinstance(self.shuffle_feeds, bool): | 
					
						
							|  |  |  |       print('shuffle_feeds has to be a boolean: True/False') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def generate_image(self): | 
					
						
							|  |  |  |     """Generate image for this module""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Define new image size with respect to padding | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |     im_width = int(self.width - (self.width * 2 * self.margin_x)) | 
					
						
							|  |  |  |     im_height = int(self.height - (self.height * 2 * self.margin_y)) | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  |     im_size = im_width, im_height | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |     logger.info('image size: {} x {} px'.format(im_width, im_height)) | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create an image for black pixels and one for coloured pixels | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  |     im_black = Image.new('RGB', size = im_size, color = 'white') | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  |     im_colour = Image.new('RGB', size = im_size, color = 'white') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 19:35:08 +02:00
										 |  |  |     # Check if internet is available | 
					
						
							|  |  |  |     if internet_available() == True: | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |       logger.info('Connection test passed') | 
					
						
							| 
									
										
										
										
											2020-05-12 19:35:08 +02:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  |       raise Exception('Network could not be reached :/') | 
					
						
							| 
									
										
										
										
											2020-05-12 19:35:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  |     # Set some parameters for formatting rss 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)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Calculate padding from top so the lines look centralised | 
					
						
							|  |  |  |     spacing_top = int( im_height % line_height / 2 ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Calculate line_positions | 
					
						
							|  |  |  |     line_positions = [ | 
					
						
							|  |  |  |       (0, spacing_top + _ * line_height ) for _ in range(max_lines)] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  |     # Create list containing all rss-feeds from all rss-feed urls | 
					
						
							|  |  |  |     parsed_feeds = [] | 
					
						
							|  |  |  |     for feeds in self.config['rss_urls']: | 
					
						
							|  |  |  |       text = feedparser.parse(feeds) | 
					
						
							|  |  |  |       for posts in text.entries: | 
					
						
							|  |  |  |         parsed_feeds.append('•{0}: {1}'.format(posts.title, posts.summary)) | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  |     self._parsed_feeds = parsed_feeds | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  |     # Shuffle the list to prevent showing the same content | 
					
						
							|  |  |  |     if self.shuffle_feeds == True: | 
					
						
							|  |  |  |       shuffle(parsed_feeds) | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  |     # Trim down the list to the max number of lines | 
					
						
							|  |  |  |     del parsed_feeds[max_lines:] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Wrap long text from feeds (line-breaking) | 
					
						
							|  |  |  |     flatten = lambda z: [x for y in z for x in y] | 
					
						
							|  |  |  |     filtered_feeds, counter = [], 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  |     self._filtered_feeds = filtered_feeds | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # 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: | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  |       # Write rss-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') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  |     # Cleanup | 
					
						
							|  |  |  |     del filtered_feeds, parsed_feeds, wrapped, counter, text | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Save image of black and colour channel in image-folder | 
					
						
							| 
									
										
										
										
											2020-05-13 18:36:54 +02:00
										 |  |  |     im_black.save(images+self.name+'.png', 'PNG') | 
					
						
							|  |  |  |     im_colour.save(images+self.name+'_colour.png', 'PNG') | 
					
						
							| 
									
										
										
										
											2020-05-07 20:16:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |   print('running {0} in standalone/debug mode'.format(filename)) |