-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotebookimporter.py
150 lines (133 loc) · 5.57 KB
/
notebookimporter.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of Notebook Importer.
#
# Notebook Importer is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Notebook Importer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Notebook Importer. If not, see <http://www.gnu.org/licenses/>.
import sys
import csv
import smtplib
from email.MIMEText import MIMEText
# Adapt the following 3 variables to your situation
GMAIL_LOGIN = "[email protected]"
GMAIL_PASSWORD = "password"
# Find your Evernote email at https://www.evernote.com/Settings.action
EVERNOTE_EMAIL = "[email protected]"
def parseData(filename):
with open(filename, 'rb') as f:
reader = csv.reader(f)
try:
header = None
content = []
for row in reader:
if not header:
header = row
else:
# Do not allow newlines in the title
row[0] = " - ".join(row[0].splitlines())
content.append(row)
return (header, content)
except csv.Error as e:
sys.exit("File %s, line %d: %s" % (filename, reader.line_num, e))
def printData(header, content, startOffset, numToDisplay):
for row in content[startOffset:startOffset+numToDisplay]:
colnum = 0
for col in row:
print "%-13s: %s" % (header[colnum], col)
colnum += 1
print
def formatEmail(row):
"""Determine subject and body of the email to be sent"""
subject = row[0]
if row[3]:
# Notebook name
subject += " @%s" % row[3]
if row[2]:
# Labels
#TODO: handle multiple tags
subject += " #%s" % row[2]
message = row[1]
return (subject, message)
def sendEmail(subject, message):
"""
Based on http://halotis.com/2009/07/11/sending-email-from-python-using-gmail/
"""
msg = MIMEText(message, "plain", "utf-8")
msg['Subject'] = subject
msg['From'] = GMAIL_LOGIN
msg['To'] = EVERNOTE_EMAIL
server = smtplib.SMTP('smtp.gmail.com', 587) #port 465 or 587
server.ehlo()
server.starttls()
server.ehlo()
server.login(GMAIL_LOGIN, GMAIL_PASSWORD)
server.sendmail(GMAIL_LOGIN, EVERNOTE_EMAIL, msg.as_string())
server.close()
def emailData(content, startOffset, numToSend):
"""
Skip all emails that have any text in the 4th column ("Do not upload")
Try to send as many notes as requested, but do not send more than the
maximum allowed by your account:
* Non-premium Evernote accounts: 50 emails per day
* Premium Evernote accounts: 200 emails per day
"""
# Determine how many emails can be sent
numCanBeSent = 0
for row in content[startOffset:]:
if not row[4]:
numCanBeSent += 1
if numCanBeSent == numToSend:
break
maxToSend = 50 # Limit the number of emails that can be sent
numWillBeSent = min(numCanBeSent, maxToSend)
if numWillBeSent < numCanBeSent:
print "WARNING: A total of %d notes are ready to be sent, but due to Evernote limitations only %d will be sent:" % (numCanBeSent, numWillBeSent)
else:
print "%d notes will be sent:" % numWillBeSent
# Send the emails
numEmailsSent = 0
for row in content[startOffset:]:
if not row[4]:
(subject, message) = formatEmail(row)
print "%3d%% - %s" % (float(100*numEmailsSent/numWillBeSent), subject)
sendEmail(subject, message)
numEmailsSent += 1
if numEmailsSent == numWillBeSent:
break
print "100%\n"
print "Note: in case you have more notes to send later (due to the limit of %d emails per day), don't forget to update your .csv file and indicate in the 5th column (\"Do not upload\") that these notes that have just been sent do not need to be sent again." % maxToSend
def defaults(content, startOffset, num):
if startOffset==None or num==None:
return (0, len(content))
else:
return (startOffset, num)
if __name__ == '__main__':
filename = sys.argv[1]
# Retrieve the data from the CSV file
(header, content) = parseData(filename)
if ['Title', 'Content', 'Labels', 'Notebook', 'Do not upload'] != header:
# The first line should contain the 5 headers, nothing different
sys.exit("File is not valid")
# How many notes to process (leave to None to process all)
# Note that if the number to process is smaller than the total number of
# notes, the list displayed by printData might be different than the list
# of notes that will be sent by email.
# This is due to the fact that the display function shows X number of notes
# (including the notes that should not be sent), while the email process
# skips those unwanted notes but will still send the same X number of notes.
startOffset = numToProcess = None
#startOffset = 185
#numToProcess = 3
(startOffset, numToProcess) = defaults(content, startOffset, numToProcess)
printData(header, content, startOffset, numToProcess)
emailData(content, startOffset, numToProcess)