X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;ds=inline;f=gkeep_renderer.py;h=3846efb6664ee73c452b327e159cc51b2455e9d7;hb=8dc3a81f3bc8eb9ac4871b64c6d471493d293316;hp=c882526409d1f9a698385c53c642c931b7d717bb;hpb=4b1f3d8a8b278ca6d62f461ea80c8ea21080c301;p=kiosk.git
diff --git a/gkeep_renderer.py b/gkeep_renderer.py
index c882526..3846efb 100644
--- a/gkeep_renderer.py
+++ b/gkeep_renderer.py
@@ -1,111 +1,172 @@
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
-import constants
-import file_writer
-import gkeepapi
+import logging
import os
import re
+from typing import Dict
+
+import gkeepapi # type: ignore
+
+import kiosk_constants
+import file_writer
import renderer
-import secrets
+import kiosk_secrets as secrets
-class gkeep_renderer(renderer.debuggable_abstaining_renderer):
- def __init__(self, name_to_timeout_dict):
- super(gkeep_renderer, self).__init__(name_to_timeout_dict, True)
- self.keep = gkeepapi.Keep()
- success = self.keep.login(secrets.google_keep_username,
- secrets.google_keep_password)
- if success:
- self.debug_print("Connected with gkeep.")
- else:
- self.debug_print("Error connecting with gkeep.")
+
+logger = logging.getLogger(__file__)
+
+
+class gkeep_renderer(renderer.abstaining_renderer):
+ def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
+ super().__init__(name_to_timeout_dict)
self.colors_by_name = {
- 'white' : '#002222',
- 'green' : '#345920',
- 'darkblue' : '#1F3A5F',
- 'blue' : '#2D545E',
- 'orange' : '#604A19',
- 'red' : '#5C2B29',
- 'purple' : '#42275E',
- 'pink' : '#5B2245',
- 'yellow' : '#635D19',
- 'brown' : '#442F19',
- 'gray' : '#3c3f4c',
- 'teal' : '#16504B'
+ "white": "#002222",
+ "green": "#345920",
+ "darkblue": "#1F3A5F",
+ "blue": "#2D545E",
+ "orange": "#604A19",
+ "red": "#5C2B29",
+ "purple": "#42275E",
+ "pink": "#5B2245",
+ "yellow": "#635D19",
+ "brown": "#442F19",
+ "gray": "#3c3f4c",
+ "teal": "#16504B",
}
+ self.keep = gkeepapi.Keep()
+ self.token_file = "./.google_keep_token"
+ if os.path.exists(self.token_file):
+ logger.debug("Attempting to reuse persisted Google Keep login token...")
+ try:
+ with open(self.token_file, "r") as rf:
+ token = "".join(rf.readlines()).strip()
+ self.keep.resume(secrets.google_keep_username, token)
+ logger.debug("Successfully reused existing login token.")
+ except gkeepapi.exception.LoginException:
+ logger.warning("Invalid token, attempting to re-login.")
+
+ if not self.keep.login(
+ secrets.google_keep_username,
+ secrets.google_keep_password,
+ secrets.google_keep_mac,
+ ):
+ raise Exception("Error connecting with Google Keep?!")
+ logger.debug("Successfully logged in with Google Keep")
- def debug_prefix(self):
+ def debug_prefix(self) -> str:
return "gkeep"
- def periodic_render(self, key):
- strikethrough = re.compile(u'\u2611([^\n]*)\n', re.UNICODE)
- linkify = re.compile(r'.*(https?:\/\/\S+).*')
+ def periodic_render(self, key: str) -> bool:
+ strikethrough = re.compile("(\u2611[^\n]*)\n", re.UNICODE)
+ # linkify = re.compile(r".*(https?:\/\/\S+).*")
self.keep.sync()
- result_list = self.keep.find(labels=[self.keep.findLabel('kiosk')])
+ result_list = self.keep.find(labels=[self.keep.findLabel("kiosk")])
for note in result_list:
title = note.title
title = title.replace(" ", "-")
title = title.replace("/", "")
- filename = "%s_2_3600.html" % title
+ filename = f"{title}_2_3600.html"
contents = note.text + "\n"
- self.debug_print("Note title '%s'" % title)
- if contents != '' and not contents.isspace():
- contents = strikethrough.sub(r'` \1\n', contents)
- contents = contents.replace('`', u'\u2611')
- #self.debug_print("Note contents:\n%s" % contents)
- contents = linkify.sub(r'\1', contents)
+ logger.debug(f"Note title '{title}'")
+ if contents != "" and not contents.isspace():
+ contents = strikethrough.sub("", contents)
+ logger.debug(f"Note contents:\n{contents}")
+ contents = contents.replace(
+ "\u2610 ", '
%s
-\n")
- f.write("")
- count = 0
- for x in individual_lines:
- f.write(x + " \n") - count += 1 - if count == num_lines / 2: - f.write(" | \n")
- f.write("\n") - f.write("") - f.write(" |
{note.title}
+\n'
+ )
+ f.write("
| \n")
+ f.write(
+ '\n'
+ )
+ f.write("
|