| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  | #!python3 | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | """
 | 
					
						
							|  |  |  | Inkycal-server module for Inkycal Project | 
					
						
							|  |  |  | by Aterju (https://inkycal.robertsirre.nl/) | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | Copyright by aceisace | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import requests | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | from inkycal.modules.template import inkycal_module | 
					
						
							|  |  |  | from inkycal.custom import * | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | from inkycal.modules.inky_image import Inkyimage as Images | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 02:56:04 +02:00
										 |  |  | logger = logging.getLogger(__name__) | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | class Inkyserver(inkycal_module): | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     """Displays an image from URL or local path
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     name = "Inykcal Server - fetches an image from Inkycal-server - (https://inkycal.robertsirre.nl/)" | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     requires = { | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         "path": { | 
					
						
							|  |  |  |             "label": "Which URL should be used to get the image?" | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         "palette": { | 
					
						
							|  |  |  |             "label": "Which palette should be used to convert the images?", | 
					
						
							|  |  |  |             "options": ['bw', 'bwr', 'bwy'] | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     optional = { | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         "path_body": { | 
					
						
							|  |  |  |             "label": "Send this data to the server via POST. Use a comma to " | 
					
						
							|  |  |  |                      "separate multiple items", | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         "dither": { | 
					
						
							|  |  |  |             "label": "Dither images before sending to E-Paper? Default is False.", | 
					
						
							|  |  |  |             "options": [False, True], | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         """Initialize module""" | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         super().__init__(config) | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         config = config['config'] | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # required parameters | 
					
						
							|  |  |  |         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-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # optional parameters | 
					
						
							|  |  |  |         self.path = config['path'] | 
					
						
							|  |  |  |         self.palette = config['palette'] | 
					
						
							|  |  |  |         self.dither = config['dither'] | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # convert path_body to list, if not already | 
					
						
							|  |  |  |         if config['path_body'] and isinstance(config['path_body'], str): | 
					
						
							|  |  |  |             self.path_body = config['path_body'].split(',') | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.path_body = config['path_body'] | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +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-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     def generate_image(self): | 
					
						
							|  |  |  |         """Generate image for this module""" | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01: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 | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         logger.info(f'Image size: {im_size}') | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # replace width and height of url | 
					
						
							|  |  |  |         print(self.path) | 
					
						
							|  |  |  |         self.path = self.path.format(width=im_width, height=im_height) | 
					
						
							|  |  |  |         print(f"modified path: {self.path}") | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # initialize custom image class | 
					
						
							|  |  |  |         im = Images() | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # when no path_body is provided, use plain GET | 
					
						
							|  |  |  |         if not self.path_body: | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             # use the image at the first index | 
					
						
							|  |  |  |             im.load(self.path) | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # else use POST request | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # Get the response image | 
					
						
							|  |  |  |             response = Image.open(requests.post( | 
					
						
							|  |  |  |                 self.path, json=self.path_body, stream=True).raw) | 
					
						
							| 
									
										
										
										
											2020-11-12 21:22:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |             # initialize custom image class with response | 
					
						
							|  |  |  |             im = Images(response) | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # resize the image to respect padding | 
					
						
							|  |  |  |         im.resize(width=im_width, height=im_height) | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # convert image to given palette | 
					
						
							|  |  |  |         im_black, im_colour = im.to_palette(self.palette, dither=self.dither) | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # with the images now send, clear the current image | 
					
						
							|  |  |  |         im.clear() | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # return images | 
					
						
							|  |  |  |         return im_black, im_colour | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2022-10-03 02:56:04 +02:00
										 |  |  |     print(f'running {__name__} in standalone/debug mode') | 
					
						
							| 
									
										
										
										
											2020-12-05 00:26:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ##  'https://inkycal.robertsirre.nl/panel/calendar/{model}?width={width}&height={height}' | 
					
						
							|  |  |  | ##path = path.replace('{model}', model).replace('{width}',str(display_width)).replace('{height}',str(display_height)) | 
					
						
							|  |  |  | ## | 
					
						
							|  |  |  | ## | 
					
						
							|  |  |  | ##inkycal_image_path_body = [ | 
					
						
							|  |  |  | ##   'https://calendar.google.com/calendar/ical/en.usa%23holiday%40group.v.calendar.google.com/public/basic.ics', | 
					
						
							|  |  |  | ##   'https |