6 from typing import Dict
8 import gkeepapi # type: ignore
10 import kiosk_constants
13 import kiosk_secrets as secrets
16 logger = logging.getLogger(__file__)
19 class gkeep_renderer(renderer.abstaining_renderer):
20 def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
21 super().__init__(name_to_timeout_dict)
22 self.colors_by_name = {
25 "darkblue": "#1F3A5F",
36 self.keep = gkeepapi.Keep()
37 success = self.keep.login(
38 secrets.google_keep_username, secrets.google_keep_password
41 logger.debug("Connected with gkeep.")
43 logger.debug("Error connecting with gkeep.")
45 def debug_prefix(self) -> str:
48 def periodic_render(self, key: str) -> bool:
49 strikethrough = re.compile("(\u2611[^\n]*)\n", re.UNICODE)
50 linkify = re.compile(r".*(https?:\/\/\S+).*")
53 result_list = self.keep.find(labels=[self.keep.findLabel("kiosk")])
54 for note in result_list:
56 title = title.replace(" ", "-")
57 title = title.replace("/", "")
59 filename = f"{title}_2_3600.html"
60 contents = note.text + "\n"
61 logger.debug(f"Note title '{title}'")
62 if contents != "" and not contents.isspace():
63 contents = strikethrough.sub("", contents)
64 logger.debug(f"Note contents:\n{contents}")
65 contents = contents.replace(
66 "\u2610 ", '<LI><INPUT TYPE="checkbox"> '
68 #contents = linkify.sub(r'<a href="\1">\1</a>', contents)
70 individual_lines = contents.split("\n")
71 num_lines = len(individual_lines)
73 for x in individual_lines:
75 leading_spaces = len(x) - len(x.lstrip(" "))
77 leading_spaces = int(leading_spaces)
79 # logger.debug(" * (%d) '%s'" % (leading_spaces, x))
80 for y in range(0, leading_spaces):
82 for y in range(0, leading_spaces):
84 contents = contents + x + "\n"
86 individual_lines = contents.split("\n")
87 color = note.color.name.lower()
88 if color in list(self.colors_by_name.keys()):
89 color = self.colors_by_name[color]
91 logger.debug(f"Unknown color '{color}'")
92 print(f"TITLE: {color} {note.title}")
93 with file_writer.file_writer(filename) as f:
95 <STYLE type="text/css">
96 a:link { color:#88bfbf; }
97 ul { list-style-type:none; }
99 <DIV STYLE="border-radius:25px; border-style:solid; padding:20px; background-color:%s; color:#eeeeee; font-size:x-large;">
103 <p style="color:#ffffff; font-size:larger"><B>{note.title}</B></p>
104 <HR style="border-top:3px solid white;">
109 f"{num_lines} lines: two column mode"
111 f.write('<TABLE BORDER=0 WIDTH=100%><TR valign="top">')
113 '<TD WIDTH=50% style="color:#eeeeee; font-size:large">\n'
115 f.write("<FONT><UL STYLE='list-style-type:none'>")
117 for x in individual_lines:
120 if count == num_lines / 2:
121 f.write("</UL></FONT></TD>\n")
123 '<TD WIDTH=50% style="color:#eeeeee; font-size:large">\n'
125 f.write("<FONT><UL STYLE='list-style-type:none'>")
126 f.write("</UL></FONT></TD></TR></TABLE></DIV>\n")
129 f"{num_lines} lines: one column mode"
131 f.write(f"<FONT><UL>{contents}</UL></FONT>")
134 logger.debug(f"Note is empty, deleting {filename}.")
135 _ = os.path.join(kiosk_constants.pages_dir, filename)
144 #logger.setLevel(logging.DEBUG)
145 #ch = logging.StreamHandler()
146 #ch.setLevel(logging.DEBUG)
147 #formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
148 #ch.setFormatter(formatter)
149 #logger.addHandler(ch)
150 #x = gkeep_renderer({"Test", 1234})
151 #x.periodic_render("Test")