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