Skip to content
Browse files

Convert to utf-8 instead of ASCII; added encoding command line option

  • Loading branch information...
1 parent c9401e4 commit 4869f9140ccbc6e5ae2cd882b77f7137c6acc0b4 @kerchen committed
Showing with 21 additions and 12 deletions.
  1. +21 −12 export2enex.py
View
33 export2enex.py
@@ -1,6 +1,6 @@
# A script for exporting all starred items from Google Reader to Evernote,
# using exported JSON data from Google's Takeout and Evernote's
-# note emailing feature.
+# bulk import feature (available in the desktop client).
#
# Copyright 2013 Paul Kerchen
#
@@ -24,10 +24,15 @@
import io
import getopt, sys
import os.path
+import codecs
def usage():
print "\nOptions:"
+ print "-e, --encoding: The name of the character encoding codec to use. The default"
+ print " is utf8. For other possible values, see the table of standard"
+ print " python character encodings at "
+ print " http://docs.python.org/2/library/codecs.html#standard-encodings"
print "-n, --notebook: The name of the Evernote notebook to put sent notes in."
print " If you do not specify a notebook, sent notes will be put in the default notebook."
print "-m, --maximum: The maximum number of articles that should be converted."
@@ -35,7 +40,7 @@ def usage():
print "-h, --help: Print this message and exit."
try:
- opts, args = getopt.getopt( sys.argv[1:], "n:m:h", ["notebook=","maximum=","help"])
+ opts, args = getopt.getopt( sys.argv[1:], "e:n:m:h", ["encoding=","notebook=","maximum=","help"])
except getopt.GetoptError as err:
print str(err)
usage()
@@ -47,9 +52,12 @@ def usage():
message_limit = -1
skip_count = 0
continue_from_prev = False
+char_encoding = "utf8"
for o, a in opts:
- if o in ("-n", "--notebook"):
+ if o in ("-e", "--encoding"):
+ char_encoding = a
+ elif o in ("-n", "--notebook"):
notebook = a
elif o in ("-m", "--maximum"):
message_limit = int(a)
@@ -57,41 +65,42 @@ def usage():
usage()
sys.exit()
-json_file = open("starred.json")
-json_dict = json.loads( unicode(json_file.read(), encoding="utf-8") )
+#json_file = open("starred.json", "rb")
+json_file = codecs.open( "starred.json", encoding=char_encoding )
+json_dict = json.loads( json_file.read() )
item_list = json_dict[ "items" ]
if message_limit < 0:
message_limit = len(item_list)
-print('<?xml version="1.0" encoding="UTF-8"?>')
+print('<?xml version="1.0" encoding="' + char_encoding + '"?>')
print('<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export2.dtd">')
print('<en-export export-date="20130320T150950Z" application="Evernote" version="Evernote Mac 5.0.6 (400960)">')
for s in item_list:
subject = ""
if 'title' in s.keys():
- subject = unicode(s["title"]).encode('ascii', 'replace')
+ subject = s["title"].encode(char_encoding, 'replace')
if notebook:
subject = subject + " @" + notebook
msg_body = ""
- msg_body = msg_body + '<note><title>'+subject+'</title><content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"> <en-note>'
+ msg_body = msg_body + '<note><title>'+subject+'</title><content><![CDATA[<?xml version="1.0" encoding="'+char_encoding+'" standalone="no"?> <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"> <en-note>'
msg_url = ""
if 'canonical' in s.keys():
d = s["canonical"][0]
- msg_url = unicode(d["href"]).encode('ascii', 'replace')
+ msg_url = d["href"].encode(char_encoding, 'replace')
if 'alternate' in s.keys():
d = s["alternate"][0]
- msg_url = unicode(d["href"]).encode('ascii', 'replace')
+ msg_url = d["href"].encode(char_encoding, 'replace')
if 'summary' in s.keys():
d = s["summary"]
- msg_body = msg_body + unicode(d["content"]).encode('ascii', 'replace')
+ msg_body = msg_body + d["content"].encode(char_encoding, 'replace')
if 'content' in s.keys():
d = s["content"]
- msg_body = msg_body + unicode(d["content"]).encode('ascii', 'replace')
+ msg_body = msg_body + d["content"].encode(char_encoding, 'replace')
msg_body = msg_body + "</en-note>]]>\r\n</content>\r\n"
msg_body = msg_body + "<note-attributes><source>web.clip</source><source-url>" + msg_url + "</source-url></note-attributes>"
msg_body = msg_body + "</note>\r\n"

0 comments on commit 4869f91

Please sign in to comment.
Something went wrong with that request. Please try again.