-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimplenote.py
142 lines (110 loc) · 4.53 KB
/
Simplenote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
import base64, logging, urllib
from datetime import datetime
from google.appengine.api import urlfetch
from django.utils import simplejson
class MyException(Exception):
def _get_message(self, message):
return self._message
def _set_message(self, message):
self._message = message
message = property(_get_message, _set_message)
class AuthError(MyException):
def __init__(self, email, message):
self.email = email
self.message = message
class ApiError(MyException):
def __init__(self, method, message):
self.method = method
self.message = message
def mkdatetime(time_str):
return datetime.strptime(time_str.split('.')[0], "%Y-%m-%d %H:%M:%S")
def get_token(email, password):
url = 'https://simple-note.appspot.com/api/login'
form_fields = {
'email': email,
'password': password
}
payload = base64.b64encode(urllib.urlencode(form_fields))
result = urlfetch.fetch(url=url,
method=urlfetch.POST,
payload=payload,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
if (result.status_code == 200):
return result.content.strip()
else:
logging.error("Error getting token: %d %s" % (result.status_code, result.content))
raise AuthError(email, "Could not authenticate or bad response from server.")
def index(token, email):
email = email.replace("+", "%2B")
url = "https://simple-note.appspot.com/api/index?auth=%s&email=%s" % (token, email)
result = urlfetch.fetch(url=url,
method=urlfetch.GET)
try:
notes = simplejson.loads(result.content)
except:
logging.error("Error loading JSON from index.\nStatus: %d\nToken: %s\nBody: %s" % (result.status_code, token, result.content))
raise ApiError('/index', "Exception loading token from body: %s" % result.content)
if (result.status_code == 200):
return notes
else:
raise ApiError('/index', "Exception retrieving notes from index.")
def search(query, token, email, max_results=10, offset_index=0):
email = email.replace("+", "%2B")
url = "https://simple-note.appspot.com/api/search?query=%s&results=%s&offset=%sauth=%s&email=%s" % (query, max_results, offset_index, token, email)
result = urlfetch.fetch(url=url,
method=urlfetch.GET)
notes = simplejson.loads(result.content)
if (result.status_code == 200):
return notes
else:
raise ApiError('/search', "Exception searching notes for '%s'" % query)
def get_note(key, token, email):
email = email.replace("+", "%2B")
url = "https://simple-note.appspot.com/api/note?key=%s&auth=%s&email=%s" % (key, token, email)
result = urlfetch.fetch(url=url,
method=urlfetch.GET)
if (result.status_code == 200):
note = {
'content': result.content,
'key': key,
'modified': mkdatetime(result.headers['note-modifydate']),
'created': mkdatetime(result.headers['note-createdate'])
}
return note
else:
logging.error("Bad key: '%s'" % key)
raise ApiError('/note', "Exception retrieving note with key '%s'" % key)
# also supports setting date modified; date format is not documented,
# is presumed to be GMT
def update_note(key, note_body, token, email):
email = email.replace("+", "%2B")
url = "https://simple-note.appspot.com/api/note?key=%s&auth=%s&email=%s" % (key, token, email)
payload = base64.b64encode(unicode(note_body))
result = urlfetch.fetch(url=url,
method=urlfetch.POST,
payload=payload)
if (result.status_code == 200):
return result.content.strip()
else:
raise ApiError('/note', "Exception updating note with key '%s'" % key)
def create_note(note_body, token, email):
email = email.replace("+", "%2B")
url = "https://simple-note.appspot.com/api/note?auth=%s&email=%s" % (token, email)
payload = base64.b64encode(unicode(note_body))
result = urlfetch.fetch(url=url,
method=urlfetch.POST,
payload=payload)
if (result.status_code == 200):
return result.content.strip()
else:
raise ApiError('/note', 'Exception creating note.')
def delete_note(key, token, email):
email = email.replace("+", "%2B")
url = "https://simple-note.appspot.com/api/delete?key=%s&auth=%s&email=%s" % (key, token, email)
result = urlfetch.fetch(url=url,
method=urlfetch.GET)
if (result.status_code == 200):
return True
else:
raise ApiError('/delete', "Exception deleting note with key '%s'" % key)