-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
executable file
·179 lines (138 loc) · 5.36 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
import csv, logging, Simplenote, StringIO, wsgiref.handlers, yaml
from betterhandler import *
from django.utils import simplejson
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class Auth(BetterHandler):
def post(self):
email = self.request.get('email')
password = self.request.get('password')
try:
token = Simplenote.get_token(email, password)
except Simplenote.AuthError:
for_template = {
'autherror': True,
}
return self.response.out.write(template.render(self.template_path('index.html'),
for_template))
else:
index = Simplenote.index(token, email)
note_ids = []
logging.info("index of all notes: '%s'" % index)
for note in index:
if note['deleted'] == False:
note_ids.append(note['key'])
logging.info("note_ids minus deleted notes: '%s'" % note_ids)
for_template = {
'note_count': len(note_ids),
'token': token,
'email': email,
'note_ids': ','.join(note_ids),
}
return self.response.out.write(template.render(self.template_path('auth.html'),
for_template))
class Export(BetterHandler):
def txt(self, notes):
for_template = {
'notes': notes
}
self.response.headers["Content-Type"] = "application/octet-stream"
self.response.headers.add_header("Content-Disposition",
'attachment; filename=simplenote-export.txt')
self.response.out.write(template.render(self.template_path('txt'),
for_template))
def csv(self, notes):
output = StringIO.StringIO()
writer = csv.writer(output)
for note in notes:
row = [note['created'].strftime("%b %d %Y %H:%M:%S"),
note['modified'].strftime("%b %d %Y %H:%M:%S"),
note['content']]
writer.writerow(row)
csvout = output.getvalue()
output.close()
self.response.headers["Content-Type"] = "application/octet-stream"
self.response.headers.add_header("Content-Disposition",
'attachment; filename=simplenote-export.csv')
self.response.out.write(csvout)
def json(self, notes):
for note in notes:
note['created'] = note['created'].strftime("%b %d %Y %H:%M:%S")
note['modified'] = note['modified'].strftime("%b %d %Y %H:%M:%S")
output = simplejson.dumps(notes)
self.response.headers["Content-Type"] = "application/octet-stream"
self.response.headers.add_header("Content-Disposition",
'attachment; filename=simplenote-export.json')
self.response.out.write(output)
def xml(self, notes):
for_template = {
'notes': notes
}
self.response.headers["Content-Type"] = "application/octet-stream"
self.response.headers.add_header("Content-Disposition",
'attachment; filename=simplenote-export.xml')
self.response.out.write(template.render(self.template_path('xml'),
for_template))
def enex(self, notes):
for_template = {
'notes': notes
}
self.response.headers["Content-Type"] = "application/octet-stream"
self.response.headers.add_header("Content-Disposition",
'attachment; filename=simplenote-export.enex')
self.response.out.write(template.render(self.template_path('enex'),
for_template))
def yaml(self, notes):
yamlnotes = []
for note in notes:
d = {}
d[str(note['key'])] = {
'created': note['created'],
'modified': note['modified'],
'content': note['content']
}
yamlnotes.append(d)
output = yaml.dump(yamlnotes, default_flow_style=False)
self.response.headers["Content-Type"] = "application/octet-stream"
self.response.headers.add_header("Content-Disposition",
'attachment; filename=simplenote-export.yaml')
self.response.out.write(output)
def post(self):
token = self.request.get('token')
email = self.request.get('email')
format = self.request.get('format')
note_ids = self.request.get('note_ids').split(',')
notes = []
for key in note_ids:
note = Simplenote.get_note(key, token, email)
notes.append(note)
for_template = {
'notes': notes
}
logging.info("Found notes: '%s'" % notes)
if format == 'txt':
self.txt(notes)
elif format == 'csv':
self.csv(notes)
elif format == 'json':
self.json(notes)
elif format == 'xml':
self.xml(notes)
elif format == 'yaml':
self.yaml(notes)
elif format == 'enex':
self.enex(notes)
class FrontPage(BetterHandler):
def get(self):
return self.response.out.write(template.render(self.template_path('index.html'),
{}))
def main():
logging.getLogger().setLevel(logging.INFO)
application = webapp.WSGIApplication([('/', FrontPage),
('/auth', Auth),
('/export', Export)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()