-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
259 lines (211 loc) · 8.38 KB
/
run.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
Author: Remy D <[email protected]>
Ralph Bean <[email protected]>
Mike Nolan <[email protected]>
License: AGPLv3+
"""
import ConfigParser
import feedparser
import json
import urllib2
import re
from flask import Flask
from flask.ext.mako import MakoTemplates, render_template
from flask import redirect, url_for, request
from evernote.api.client import EvernoteClient
from evernote.edam.notestore.ttypes import NoteFilter, NotesMetadataResultSpec
from evernote.edam.type.ttypes import NoteSortOrder
app = Flask(__name__)
app.template_folder = "templates"
mako = MakoTemplates(app)
config = ConfigParser.ConfigParser()
config.read('config.ini')
consumer_key = config.get('evernote', 'consumer_key', 0)
consumer_secret = config.get('evernote', 'consumer_secret', 0)
dev_token = config.get('evernote', 'dev_token', 0).strip("'")
@app.route('/')
def index():
client = EvernoteClient(token=dev_token)
userStore = client.get_user_store()
user = userStore.getUser()
username = user.username
return render_template('index.mak', name='mako', username=username)
@app.route('/getbooks')
def getbooks():
client = EvernoteClient(token=dev_token)
noteStore = client.get_note_store()
notebooks = noteStore.listNotebooks(dev_token)
for n in notebooks:
print n.name
return "cool"
@app.route('/getevernote')
def getevernote():
client = EvernoteClient(token=dev_token)
updated_filter = NoteFilter(order=NoteSortOrder.UPDATED)
offset = 0
max_notes = 10
result_spec = NotesMetadataResultSpec(includeTitle=True)
note_store = client.get_note_store()
result_list = note_store.findNotesMetadata(
dev_token,
updated_filter,
offset,
max_notes,
result_spec)
notes = {"timeline": {
"headline": "Evernote to Timeline.js",
"type": "default",
"text": "Automatic Tweets, Wikipedia, Youtube",
"startDate": "2014,8,26",
"date": [],
}}
for note in result_list.notes:
content = note_store.getNoteContent(dev_token, note.guid)
content_list = content.split(',', 3)
media = False
if (":wikipedia:" in content or ":twitter:" in content or
":youtube:" in content):
media = True
note_json = {
"startDate": content_list[1].replace('-', ','),
"endDate": content_list[2].replace('-', ','),
"headline": note.title,
"text": content_list[3]
}
if media:
m = re.search('.+:\w+:\s([\w:/\.]+).*', content)
asset = {
"media": m.group(1)
}
note_json['asset'] = asset
notes['timeline']['date'].append(note_json)
return json.dumps(notes)
@app.route('/postevernote')
def postevernote():
'''
An example script using the EvernoteClient to post new notes.
Source:
https://github.com/evernote/evernote-sdk-python/tree/master/sample/client
'''
#
# A simple Evernote API demo script that lists all notebooks in the user's
# account and creates a simple test note in the default notebook.
#
# Before running this sample, you must fill in your Evernote developer
# token.
#
# To run (Unix):
# export PYTHONPATH=../../lib; python EDAMTest.py
#
import hashlib
import binascii
import evernote.edam.userstore.constants as UserStoreConstants
import evernote.edam.type.ttypes as Types
from evernote.api.client import EvernoteClient
# Real applications authenticate with Evernote using OAuth, but for the
# purpose of exploring the API, you can get a developer token that allows
# you to access your own Evernote account. To get a developer token, visit
# https://sandbox.evernote.com/api/DeveloperToken.action
auth_token = dev_token
if auth_token == "your developer token":
print "Please fill in your developer token"
print "To get a developer token, visit " \
"https://sandbox.evernote.com/api/DeveloperToken.action"
exit(1)
# Initial development is performed on our sandbox server. To use the
# production service, change sandbox=False and replace your
# developer token above with a token from
# https://www.evernote.com/api/DeveloperToken.action
client = EvernoteClient(token=auth_token, sandbox=True)
user_store = client.get_user_store()
version_ok = user_store.checkVersion(
"Evernote EDAMTest (Python)",
UserStoreConstants.EDAM_VERSION_MAJOR,
UserStoreConstants.EDAM_VERSION_MINOR
)
print "Is my Evernote API version up to date? ", str(version_ok)
print ""
if not version_ok:
exit(1)
note_store = client.get_note_store()
# List all of the notebooks in the user's account
notebooks = note_store.listNotebooks()
print "Found ", len(notebooks), " notebooks:"
for notebook in notebooks:
print " * ", notebook.name
print
print "Creating a new note in the default notebook"
print
# To create a new note, simply create a new Note object and fill in
# attributes such as the note's title.
note = Types.Note()
note.title = "Test note from EDAMTest.py"
# To include an attachment such as an image in a note, first create a
# Resource for the attachment. At a minimum, the Resource contains the
# binary attachment data, an MD5 hash of the binary data, and the
# attachment MIME type. It can also include attributes such as filename
# and location.
image = open('static/img/enlogo.png', 'rb').read()
md5 = hashlib.md5()
md5.update(image)
hash = md5.digest()
data = Types.Data()
data.size = len(image)
data.bodyHash = hash
data.body = image
resource = Types.Resource()
resource.mime = 'image/png'
resource.data = data
# Now, add the new Resource to the note's list of resources
note.resources = [resource]
# To display the Resource as part of the note's content, include an
# <en-media> tag in the note's ENML content. The en-media tag identifies
# the corresponding Resource using the MD5 hash.
hash_hex = binascii.hexlify(hash)
# The content of an Evernote note is represented using Evernote Markup
# Language (ENML). The full ENML specification can be found in the Evernote
# API Overview at
# http://dev.evernote.com/documentation/cloud/chapters/ENML.php
note.content = '<?xml version="1.0" encoding="UTF-8"?>'
note.content += '<!DOCTYPE en-note SYSTEM ' \
'"http://xml.evernote.com/pub/enml2.dtd">'
note.content += '<en-note>Here is the Evernote logo:<br/>'
note.content += '<en-media type="image/png" hash="' + hash_hex + '"/>'
note.content += '</en-note>'
# Finally, send the new note to Evernote using the createNote method
# The new Note object that is returned will contain server-generated
# attributes such as the new note's unique GUID.
created_note = note_store.createNote(note)
status = "Successfully created a new note with GUID: " + created_note.guid
print status
return status
@app.route('/about')
def about():
return render_template('about.mak', name='mako')
@app.route('/feed')
def feed():
query = request.args.get('q').lower()
request_type = request.args.get('type').lower()
if request_type == 'news':
google_news_rss_url = "https://news.google.com/news/feeds?q=" + "{0}".format(query) + "&output=rss"
feed = feedparser.parse(google_news_rss_url).entries
return render_template('feed.mak', request_type=request_type,
name='mako', feed=feed)
elif request_type == 'bills':
bills_response = urllib2.urlopen(
"http://congress.api.sunlightfoundation.com/bills/search?query="
+ "{0}".format(query) + "&apikey=9b21768d77c648a39ba9b9e77cda089c")
feed = json.loads(bills_response.read().decode(
bills_response.info().getparam('charset') or 'utf-8'))['results']
return render_template('feed.mak', request_type=request_type,
name='mako', feed=feed)
@app.route('/timeline')
def timeline():
return render_template('timeline.mak', name='mako')
@app.route('/story')
def story():
return render_template('story.mak', name='mako')
# return redirect(url_for('static', filename='img/preso.svg'))
@app.route('/slides')
def slides():
return redirect(url_for('static', filename='img/preso.svg'))