Skip to content

Instantly share code, notes, and snippets.

@irwinwilliams
Created October 19, 2019 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irwinwilliams/c02fbba35c6c33a2971b7ff4e64044c6 to your computer and use it in GitHub Desktop.
Save irwinwilliams/c02fbba35c6c33a2971b7ff4e64044c6 to your computer and use it in GitHub Desktop.
import logging
import gkeepapi
import jsonpickle
import os
import azure.functions as func
class KeepNote(object):
label = ""
text = ""
category = ""
# The class "constructor" - It's actually an initializer
def __init__(self, label, text, category):
self.label = label
self.text = text
self.category = category
def __json__(self):
return {
'label': self.label,
'text': self.text,
'category': self.category,
'__python__': 'mymodule.submodule:KeepNote.from_json',
}
for_json = __json__ # supported by simplejson
@classmethod
def from_json(cls, json):
obj = cls()
obj.label = json['label']
obj.text = json['text']
obj.category = json['category']
return obj
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
keep = gkeepapi.Keep()
clientId = os.environ["gclientid"]
secret = os.environ["gkey"]
# return func.HttpResponse(f"{username}")
success = keep.login(clientId, secret)
body = ""
title = ""
category = ""
try:
req_body = req.get_json()
body = req_body["body"]
title = req_body["title"]
category = req_body["category"]
except ValueError:
pass
else:
body = req_body.get('body')
title = req_body.get('title')
category = req_body.get('category')
print(body)
print(title)
print(category)
#return func.HttpResponse(title)
labels = keep.labels()
currentLabel = None
for label in labels:
if label.name == category:
currentLabel = label
break
if currentLabel is None:
currentLabel = keep.createLabel(category)
keep.sync()
#return func.HttpResponse(currentLabel.id)
if len(category) > 0:
note = keep.createNote(title, body)
note.color = gkeepapi.node.ColorValue.Pink
note.labels.add(currentLabel)
keep.sync()
return func.HttpResponse(note.id)
gnotes = keep.all()
#for label in labels:
#print label.id
#help(label)
keepNotes = []
for note in gnotes:
try:
woi = "--->"
data = note.title
for label in note.labels.all():
try:
keepNote = KeepNote(note.title, note.text, label.name)
keepNotes.append(keepNote)
except:
pass
#print data
#woi = woi + "," + label.name
#print woi
#print note.text
#print "\r\n"
except:
pass
#json = simplejson.dumps(keepNotes, for_json=True)
json = jsonpickle.encode(keepNotes)
logging.info(json)
# name = req.params.get('name')
# if not name:
# try:
# req_body = req.get_json()
# except ValueError:
# pass
# else:
# name = req_body.get('name')
# if name:
# return func.HttpResponse(f"{json.tostring()}")
#http://www.convertcsv.com/json-to-csv.htm
return func.HttpResponse(json)
# else:
# return func.HttpResponse(
# "Please pass a name on the query string or in the request body",
# status_code=400
# )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment