de1116d1ef04dd2e47a6eff1b96450c21ce8897d
[kiosk.git] / gkeep_renderer.py
1 # -*- coding: utf-8 -*-
2
3 import constants
4 import file_writer
5 import gkeepapi
6 import os
7 import re
8 import renderer
9 import secrets
10
11 class gkeep_renderer(renderer.debuggable_abstaining_renderer):
12     def __init__(self, name_to_timeout_dict):
13         super(gkeep_renderer, self).__init__(name_to_timeout_dict, True)
14         self.keep = gkeepapi.Keep()
15         success = self.keep.login(secrets.google_keep_username,
16                                   secrets.google_keep_password)
17         if success:
18             self.debug_print("Connected with gkeep.")
19         else:
20             self.debug_print("Error connecting with gkeep.")
21         self.colors_by_name = {
22             'white' : '#002222',
23             'green' : '#345920',
24             'darkblue' : '#1F3A5F',
25             'blue' : '#2D545E',
26             'orange' : '#604A19',
27             'red' : '#5C2B29',
28             'purple' : '#42275E',
29             'pink' : '#5B2245',
30             'yellow' : '#635D19',
31             'brown' : '#442F19',
32             'gray' : '#3c3f4c',
33             'teal' : '#16504B'
34         }
35
36     def debug_prefix(self):
37         return "gkeep"
38
39     def periodic_render(self, key):
40         strikethrough = re.compile('(\u2611[^\n]*)\n', re.UNICODE)
41         linkify = re.compile(r'.*(https?:\/\/\S+).*')
42
43         self.keep.sync()
44         result_list = self.keep.find(labels=[self.keep.findLabel('kiosk')])
45         for note in result_list:
46             title = note.title
47             title = title.replace(" ", "-")
48             title = title.replace("/", "")
49
50             filename = "%s_2_3600.html" % title
51             contents = note.text + "\n"
52             self.debug_print("Note title '%s'" % title)
53             if contents != '' and not contents.isspace():
54                 contents = strikethrough.sub('', contents)
55                 self.debug_print("Note contents:\n%s" % contents)
56                 contents = contents.replace(u'\u2610 ',
57                                             u'<LI><INPUT TYPE="checkbox">&nbsp;')
58                 contents = linkify.sub(r'<a href="\1">\1</a>', contents)
59
60                 individual_lines = contents.split("\n")
61                 num_lines = len(individual_lines)
62                 max_length = 0
63                 contents = ""
64                 for x in individual_lines:
65                     length = len(x)
66                     if length > max_length:
67                         max_length = length
68                     leading_spaces = len(x) - len(x.lstrip(' '))
69                     leading_spaces /= 2
70                     leading_spaces = int(leading_spaces)
71                     x = x.lstrip(' ')
72                     # self.debug_print(" * (%d) '%s'" % (leading_spaces, x))
73                     for y in range(0, leading_spaces):
74                         x = "<UL>" + x
75                     for y in range(0, leading_spaces):
76                         x = x + "</UL>"
77                     contents = contents + x + "\n"
78
79                 individual_lines = contents.split("\n")
80                 color = note.color.name.lower()
81                 if color in list(self.colors_by_name.keys()):
82                     color = self.colors_by_name[color]
83                 else:
84                     self.debug_print("Unknown color '%s'" % color)
85                 f = file_writer.file_writer(filename)
86                 f.write("""
87 <STYLE type="text/css">
88   a:link { color:#88bfbf; }
89   ul { list-style-type:none; }
90 </STYLE>
91 <DIV STYLE="border-radius: 25px; border-style: solid; padding: 20px; background-color: %s; color: #eeeeee; font-size: x-large;">
92 <p style="color: #ffffff; font-size:larger"><B>%s</B></p>
93 <HR style="border-top: 3px solid white;">""" % (color, note.title))
94                 if num_lines >= 12 and max_length < 120:
95                     self.debug_print("%d lines (max=%d chars): two columns" %
96                                      (num_lines, max_length))
97                     f.write("<TABLE BORDER=0 WIDTH=100%%><TR valign=\"top\">")
98                     f.write("<TD WIDTH=50%% style=\"color:#eeeeee; font-size:large\">\n")
99                     f.write("<FONT><UL STYLE='list-style-type:none'>")
100                     count = 0
101                     for x in individual_lines:
102                         f.write(x + "\n")
103                         count += 1
104                         if count == num_lines / 2:
105                             f.write("</UL></FONT></TD>\n")
106                             f.write("<TD WIDTH=50%% style=\"color:#eeeeee; font-size:large\">\n")
107                             f.write("<FONT><UL STYLE='list-style-type:none'>")
108                     f.write("</UL></FONT></TD></TR></TABLE></DIV>\n");
109                 else:
110                     self.debug_print("%d lines (max=%d chars): one column" %
111                                      (num_lines, max_length))
112                     f.write("<FONT><UL>%s</UL></FONT>" % contents)
113                 f.write("</DIV>")
114                 f.close()
115             else:
116                 self.debug_print("Note is empty, deleting %s." % filename)
117                 _ = os.path.join(constants.pages_dir, filename)
118                 try:
119                     os.remove(_)
120                 except:
121                     pass
122         return True
123
124 # Test
125 #x = gkeep_renderer({"Test", 1234})
126 #x.periodic_render("Test")