-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiigo_to_evernote.py
executable file
·148 lines (106 loc) · 3.92 KB
/
diigo_to_evernote.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
#!/usr/bin/env python
import os
import sys
import codecs
import ConfigParser
import cgi
import textwrap
from datetime import datetime
from collections import OrderedDict
from bs4 import BeautifulSoup
import evernote.edam.userstore.constants as UserStoreConstants
import evernote.edam.type.ttypes as Types
from evernote.api.client import EvernoteClient
NOTE_TEMPLATE = textwrap.dedent(u'''\
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
<h1>{title}</h1>
<p>
<b>URL:</b> <a href="{url}">{url}</a><br/>
<b>Tags:</b> {tags}<br/>
<b>Last visit:</b> {last_visit}<br/>
<b>Add date:</b> {add_date}<br/>
<b>Private:</b> {private}<br/>
</p>
</en-note>
''')
class EvernoteBookmarks(object):
def __init__(self, token):
self.token = token
self.client = EvernoteClient(
token=token,
sandbox=False)
self.user_store = self.client.get_user_store()
version_ok = self.user_store.checkVersion(
"Evernote EDAMTest (Python)",
UserStoreConstants.EDAM_VERSION_MAJOR,
UserStoreConstants.EDAM_VERSION_MINOR
)
if not version_ok:
raise ValueError("Evernote API version not up to date")
self.note_store = self.client.get_note_store()
def list_notebooks(self):
return self.note_store.listNotebooks()
def create_notebook(self, notebook_name):
notebook = Types.Notebook()
notebook.name = notebook_name
notebook = self.note_store.createNotebook(notebook)
return notebook.guid
def create_note(self, bookmark, notebook_guid):
date_created = bookmark['add_date']*1000
tag_string = ', '.join(bookmark['tags'])
if 'no_tag' in tag_string:
tag_string = ""
content = NOTE_TEMPLATE.format(
title=cgi.escape(bookmark['title']),
url=cgi.escape(bookmark['url']),
tags=cgi.escape(tag_string),
last_visit=cgi.escape(str(datetime.fromtimestamp(bookmark['last_visit']))),
add_date=cgi.escape(str(datetime.fromtimestamp(bookmark['add_date']))),
private=cgi.escape("Yes" if bookmark['private'] else "No"),
)
note = Types.Note()
note.title = bookmark['title'].encode('utf-8')
note.content = content.encode('utf-8')
note.tagNames = [bm.encode('utf-8') for bm in bookmark['tags']]
note.created = date_created
note.updated = date_created
note.notebookGuid = notebook_guid
self.note_store.createNote(note)
def get_bookmarks(html):
soup = BeautifulSoup(html)
anchors = soup.find_all('a')
bookmarks = []
for a in anchors:
bm_data = OrderedDict()
bm_data['title'] = a.get_text()
bm_data['url'] = a.get('href')
bm_data['tags'] = a.get('tags').split(',')
bm_data['last_visit'] = int(a.get('last_visit'))
bm_data['add_date'] = int(a.get('add_date'))
bm_data['private'] = (a.get('private') == '1')
bookmarks.append(bm_data)
return bookmarks
def main():
input_fname = sys.argv[1]
config = ConfigParser.ConfigParser()
config.read(('diigo_to_evernote.conf',
os.path.expanduser(os.path.join('~', '.diigo_to_evernote.conf'))))
# parse the input
html = codecs.open(input_fname, 'r', 'utf-8').read()
bookmarks = get_bookmarks(html)
enbm = EvernoteBookmarks(config.get('Evernote', 'DeveloperToken'))
notebooks = enbm.list_notebooks()
try:
notebook_guid = next(nb.guid for nb in notebooks if
nb.name == 'Bookmarks')
except StopIteration:
notebook_guid = enbm.create_notebook('Bookmarks')
for bm in bookmarks:
print "Adding bookmark:"
print bm
print ""
enbm.create_note(bm, notebook_guid)
if __name__ == '__main__':
main()