/
PinboardEvernoteSync.py
executable file
·328 lines (294 loc) · 12 KB
/
PinboardEvernoteSync.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""PinboardEvernoteSync.py
Pinboard is great. Evernote too.
Make love, not war, use both and keep them in sync.
"""
__version__ = "1.0"
__license__ = "WTFPL"
__copyright__ = "Copyright 2013-2015, Olivier Thereaux"
__author__ = "Olivier Thereaux <http://olivier.thereaux.net/>"
import os
import socket
import subprocess
import re
import sys
import pinboard
import time
from datetime import datetime, date
import hashlib
import binascii
import cgi
import urllib
import urllib2
import json
import hashlib
import binascii
import evernote.edam.userstore.constants as UserStoreConstants
import evernote.edam.limits.constants as EvernoteLimits
from evernote.edam.notestore import NoteStore
import evernote.edam.type.ttypes as Types
import evernote.edam.error.ttypes as Errors
from evernote.api.client import EvernoteClient
# initialise clients with tokens
pinboard_username = None
pinboard_pass = None
evernote_token = None
readability_token = None
from conf import *
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def canhaslynx():
# test whether lynx is installed
# strongly inspired by http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
lynx_exe = None
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, "lynx")
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
lynx_exe = exe_file
return lynx_exe
def canhasphantom():
# test whether phantomJS is installed
phantom_exe = None
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, "phantomjs")
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
phantom_exe = exe_file
return phantom_exe
def clean_href(href):
clean_href = href
try: #python 3.3 and above
from shlex import quote
clean_href = quote(clean_href)
except:
_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search
if _find_unsafe(clean_href):
clean_href = "'" + clean_href.replace("'", "'\"'\"'") + "'"
return clean_href
def savescreenshotfromphantom(phantom_exe, href):
href = clean_href(href)
phantom_args = [phantom_exe, "phantom/rasterize.js", href, "screenshot.png"]
try:
subprocess.check_output(phantom_args, stderr=open('exceptions.txt', 'w'))
except:
pass
return None
def getsummaryfromreadability(href, readability_token):
readability_query = 'https://readability.com/api/content/v1/parser?url='+href+'&token='+readability_token
readable_text = urllib.urlopen(readability_query).read()
try:
return '<p>'+cgi.escape(json.loads(readable_text)['excerpt']).encode("utf-8")+'</p>'
except:
return ''
def getsummaryfromlynx(lynx_exe, href):
href = clean_href(href)
lynx_args = [lynx_exe,'-dump','-display_charset=utf-8','-assume_charset=utf-8','-nomargins','-hiddenlinks=ignore','-nonumbers',href]
try:
lynx_dump = cgi.escape(subprocess.check_output(lynx_args, stderr=open('/dev/null', 'w')))
return '<pre>'+lynx_dump+'</pre>'
except:
return ''
def save2evernote(pinbookmark, note_store, bookmark_guid, lynx_exe, phantom_exe, readability_token):
note = Types.Note()
# ourNote.notebookGuid = parentNotebook.guid
cleantitle = cgi.escape(pinbookmark["description"])
# According to https://dev.evernote.com/doc/reference/Types.html#Struct_Note
# The subject of the note. Can't begin or end with a space. Doing some zealous replacement which seems to work
cleantitle = cleantitle.replace(u"\xa0", u" ").encode("utf-8")
cleantitle = re.sub(r"\s+", " ", cleantitle)
cleantitle = re.sub(r"\t+", " ", cleantitle)
cleantitle = re.sub(r"\s*$", "", cleantitle)
cleantitle = re.sub(r"^\s*", "", cleantitle)
# as per https://dev.evernote.com/doc/reference/Limits.html
re.sub(r"[^\p{Cc}\p{Z}]([^\p{Cc}\p{Zl}\p{Zp}]{0,253}[^\p{Cc}\p{Z}])?$", "", cleantitle)
if len(cleantitle) >= EvernoteLimits.EDAM_NOTE_TITLE_LEN_MAX:
cleantitle = cleantitle[:250]+"..."
print "Note title: %s" % cleantitle
note.title = cleantitle
attributes = Types.NoteAttributes(sourceURL = pinbookmark["href"])
note.attributes = attributes
note.notebookGuid = bookmark_guid
page_dump = ''
if lynx_exe:
# print "Can Has Lynx"
page_dump = getsummaryfromlynx(lynx_exe, pinbookmark["href"])
if page_dump == "": #lynx failed, or no present. Try readability
# print "Lynx no worky"
if readability_token != None:
# print "Using Readability instead"
page_dump = getsummaryfromreadability(pinbookmark["href"], readability_token)
resource_embed = ""
if phantom_exe:
savescreenshotfromphantom(phantom_exe, pinbookmark['href'])
try:
image = open('screenshot.png', 'rb').read()
except:
image = None
if image:
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
note.resources = [resource]
hash_hex = binascii.hexlify(hash)
resource_embed = '<en-media type="image/png" hash="' + hash_hex + '"/>'
if resource_embed != "":
os.remove('screenshot.png')
resource_embed += """
<hr/>
"""
note.content = '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
%s
<hr />
%s
%s
</en-note>''' % (cgi.escape(pinbookmark["extended"]).encode("utf-8"), resource_embed, page_dump)
note.created = 1000*int(time.mktime(pinbookmark[u'time_parsed']))
note.updated = 1000*int(time.mktime(pinbookmark[u'time_parsed']))
return note_store.createNote(note)
def main():
# Checking if we have Lynx / phantomJS for optional features
#Is Lynx installed?
print "Can we use lynx to extract text?"
lynx_exe = canhaslynx()
if lynx_exe:
print "yes"
else:
print "no"
#Is phantomJS installed?
print "Can we use phantomJS to extract screenshots?"
phantom_exe = canhasphantom()
if phantom_exe:
print "yes"
else:
print "no"
print "connecting to Evernote…"
client = EvernoteClient(token=evernote_token, sandbox=False)
user_store = client.get_user_store()
try:
note_store = client.get_note_store()
except Errors.EDAMSystemException, e:
if e.errorCode == 19: # rate limit reached
print "Rate limit reached"
print "Retry your request in %d seconds" % e.rateLimitDuration
time.sleep(e.rateLimitDuration+1)
note_store = client.get_note_store()
# look for notebook "Bookmarks". If there is none, create it
notebooks = note_store.listNotebooks()
bookmark_notebook_guid = None
for notebook in notebooks:
if notebook.name == "Bookmarks":
bookmark_notebook_guid = notebook.guid
if bookmark_notebook_guid == None:
print "Creating Evernote Notebook…"
new_notebook = Types.Notebook()
new_notebook.name = "Bookmarks"
bookmark_notebook_guid = note_store.createNotebook(new_notebook).guid
# retrieve all ids and URIs in the Evernote notebook
print "Retrieving all Evernote bookmarks…"
filter = NoteStore.NoteFilter(notebookGuid = bookmark_notebook_guid)
spec = NoteStore.NotesMetadataResultSpec()
spec.includeTitle = True
spec.includeAttributes = True
spec.includeCreated = True
note_offset = 0
keep_looking = True
all_evernote_bookmarks = list()
while keep_looking:
found_notes = note_store.findNotesMetadata(filter, note_offset, 250, spec)
# oh FFS Evernote, can you make your API even less user-friendly?
all_evernote_bookmarks += found_notes.notes
if found_notes.totalNotes == len(all_evernote_bookmarks):
keep_looking = False
else:
note_offset = len(all_evernote_bookmarks)
time.sleep(1)
print "Success. Retrieved %d Bookmarks." % len(all_evernote_bookmarks)
all_evernote_bookmarks_map = dict()
all_evernote_uris = list()
for note in all_evernote_bookmarks:
if note.attributes.sourceURL != None:
all_evernote_bookmarks_map[note.attributes.sourceURL] = note
all_evernote_uris.append(note.attributes.sourceURL)
all_evernote_uris.reverse()
# Now we do the same dance with Pinboard
p = pinboard.open(token=pinboard_token)
print "connecting to Pinboard…"
p = pinboard.open(pinboard_username, pinboard_pass)
print "Retrieving all Pinboard bookmarks…"
all_pinboard_posts = p.posts()
print "Success. Retrieved %d Bookmarks." % len(all_pinboard_posts)
all_pinboard_uris = list()
all_pinboard_posts_map = dict()
for post in all_pinboard_posts:
if post["href"] != None:
all_pinboard_uris.append(post["href"])
all_pinboard_posts_map[post["href"]] = post
all_pinboard_uris.reverse()
# now we compare and prep the sync
missing_from_evernote = list()
missing_from_pinboard = list()
for evernote_uri in all_evernote_uris:
if evernote_uri not in all_pinboard_uris:
missing_from_pinboard.append(evernote_uri)
for pinboard_uri in all_pinboard_uris:
if pinboard_uri not in all_evernote_uris:
missing_from_evernote.append(pinboard_uri)
if len(missing_from_pinboard):
print "Processing %d bookmarks missing from Pinboard:" % len(missing_from_pinboard)
post_counter = 1
post_counter_total = len(missing_from_pinboard)
for evernote_uri in missing_from_pinboard:
note = all_evernote_bookmarks_map[evernote_uri]
print post_counter, "/", post_counter_total, ": ", evernote_uri
# remember? p is our pinboard API object
p.add(url=evernote_uri, description=note.title, date = datetime.fromtimestamp(note.created/1000))
time.sleep(1)
post_counter += 1
print
if len(missing_from_evernote):
print "Processing %d bookmarks missing from Evernote…" % len(missing_from_evernote)
post_counter = 1
post_counter_total = len(missing_from_evernote)
for pinboard_uri in missing_from_evernote:
post = all_pinboard_posts_map[pinboard_uri]
print post_counter, "/", post_counter_total, ": ", post['href']
note_filter = NoteStore.NoteFilter(words='sourceURL:"'+post["href"].encode("utf-8")+'"')
try:
existing_notes = note_store.findNotes(note_filter, 0, 1)
except socket.error:
print "Evernote server timeout, waiting to re-try"
time.sleep(5)
existing_notes = note_store.findNotes(note_filter, 0, 1)
except Errors.EDAMSystemException, e:
if e.errorCode == 19: # limit rate reached
print "Rate limit reached"
print "Retry your request in %d seconds" % e.rateLimitDuration
time.sleep(e.rateLimitDuration+1)
existing_notes = note_store.findNotes(note_filter, 0, 1)
# if this bombs again, let it crash. for now.
if len(existing_notes.notes) > 0:
print "Skipping post: already in Evernote"
pass
else:
try:
created_note = save2evernote(post, note_store, bookmark_notebook_guid, lynx_exe=lynx_exe, phantom_exe=phantom_exe, readability_token=readability_token)
print "Successfully created a new note with GUID: ", created_note.guid
except Exception,e:
print "Could not create a note. Error was: ", e
pass
post_counter += 1
print
if __name__ == "__main__":
sys.exit(main())