X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=gkeep_renderer.py;h=d05a24b2defd66f302cafa65b0c82096dd724620;hb=7eae23537dcc61565a24d5c957d4325b7337b63a;hp=f8313bd64f239a49b6ca741b0af7b88090a46e56;hpb=37b72e72c59140c4a6f7f541c716e4b0bc254b08;p=kiosk.git diff --git a/gkeep_renderer.py b/gkeep_renderer.py index f8313bd..d05a24b 100644 --- a/gkeep_renderer.py +++ b/gkeep_renderer.py @@ -1,61 +1,67 @@ -# -*- coding: utf-8 -*- +#!/usr/bin/env python3 -import constants -import file_writer -import gkeepapi +import gkeepapi # type: ignore import os import re +from typing import List, Dict + +from google_auth_oauthlib.flow import InstalledAppFlow + +import 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): + def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None: super(gkeep_renderer, self).__init__(name_to_timeout_dict, True) + 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", + } self.keep = gkeepapi.Keep() - success = self.keep.login(secrets.google_keep_username, - secrets.google_keep_password) + 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.") - 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' - } - def debug_prefix(self): + def debug_prefix(self) -> str: return "gkeep" - def periodic_render(self, key): - strikethrough = re.compile('(\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('', contents) - contents = contents.replace(u'\u2610 ', - u'
  •  ') - - #self.debug_print("Note contents:\n%s" % contents) + self.debug_print(f"Note title '{title}'") + if contents != "" and not contents.isspace(): + contents = strikethrough.sub("", contents) + self.debug_print(f"Note contents:\n{contents}") + contents = contents.replace( + "\u2610 ", '
  •  ' + ) contents = linkify.sub(r'\1', contents) individual_lines = contents.split("\n") @@ -66,13 +72,14 @@ class gkeep_renderer(renderer.debuggable_abstaining_renderer): length = len(x) if length > max_length: max_length = length - spaces = len(x) - len(x.lstrip(' ')) - spaces /= 2 - spaces = int(spaces) - x = x.lstrip(' ') - for y in range(0, spaces): + leading_spaces = len(x) - len(x.lstrip(" ")) + leading_spaces //= 2 + leading_spaces = int(leading_spaces) + x = x.lstrip(" ") + # self.debug_print(" * (%d) '%s'" % (leading_spaces, x)) + for y in range(0, leading_spaces): x = "" contents = contents + x + "\n" @@ -81,39 +88,50 @@ class gkeep_renderer(renderer.debuggable_abstaining_renderer): if color in list(self.colors_by_name.keys()): color = self.colors_by_name[color] else: - self.debug_print("Unknown color '%s'" % color) - f = file_writer.file_writer(filename) - f.write(""" + self.debug_print(f"Unknown color '{color}'") + print(f"TITLE: {color} {note.title}") + with file_writer.file_writer(filename) as f: + f.write(""" -
    -

    %s

    -
    """ % (color, note.title)) - if num_lines >= 12 and max_length < 120: - self.debug_print("%d lines (max=%d chars): two columns" % - (num_lines, max_length)) - f.write("") - f.write("\n") - f.write("
    \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("
      ") - f.write("
    \n"); - else: - self.debug_print("%d lines (max=%d chars): one column" % - (num_lines, max_length)) - f.write("" % contents) - f.write("") - f.close() +
    +""" % color + ) + f.write(f""" +

    {note.title}

    +
    +""" + ) + if num_lines >= 12 and max_length < 120: + self.debug_print( + f"{num_lines} lines (max={max_length} chars): two columns" + ) + f.write('') + f.write( + '\n") + f.write( + '
    \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("
      ") + f.write("
    \n") + else: + self.debug_print( + f"{num_lines} lines (max={max_length} chars): one column" + ) + f.write(f"") + f.write("") else: - self.debug_print("Note is empty, deleting %s." % filename) + self.debug_print(f"Note is empty, deleting {filename}.") _ = os.path.join(constants.pages_dir, filename) try: os.remove(_) @@ -121,6 +139,7 @@ class gkeep_renderer(renderer.debuggable_abstaining_renderer): pass return True + # Test -x = gkeep_renderer({"Test", 1234}) -x.periodic_render("Test") +#x = gkeep_renderer({"Test", 1234}) +#x.periodic_render("Test")