# -*- coding: utf-8 -*-
import constants
import file_writer
import gkeepapi
import os
import re
import renderer
import 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.")
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):
return "gkeep"
def periodic_render(self, key):
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')])
for note in result_list:
title = note.title
title = title.replace(" ", "-")
title = title.replace("/", "")
filename = "%s_2_3600.html" % title
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('`', '\u2611')
#self.debug_print("Note contents:\n%s" % contents)
contents = linkify.sub(r'\1', contents)
individual_lines = contents.split("\n")
num_lines = len(individual_lines)
max_length = 0
for x in individual_lines:
length = len(x)
if length > max_length:
max_length = length
contents = contents.replace("\n", "
\n")
color = note.color.name.lower()
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("""
%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(" |