ich muss gestehen ich bin absoluter Neuling mit Python und ehrlich gesagt stand es auf der Meiner "todo" Liste auch recht weit unten, jedoch habe ich in jüngster Vergangenheit gelernt, dass es sich hervorragend eignet, um plattformunanhängig Werte aus vorhandenen Quellen zu holen, zu formatieren und anders wie weiter zu verarbeiten. Genau so was muss ich leider häufiger machen, daher ist es nun in meiner todo Liste nach oben gerutscht.
Da ich aber aus Erfahrung ein besserer "Editor" als "Author" bin, suche ich mir für solche Vorhaben gerne "fertige" Dinge, dich ich dann an meine Ansprüche anpasse. Aus der Vergangenheit weiss ich, dass ich persönlich dadurch am besten lerne und am schnellsten vorankommen.
Genau vor einem solchen Problem stehe ich nun. Jamie Todd Rubin hat ein geniales Script erstellt, mit dem man mehr oder weniger automatisch seine Notizen und Markierungen vom Kindle in Evernote einfügen kann. (Siehe hier. Das Projekt ist auf GitHub hier zu finden).
Leider stosse ich bei diesem Script auf einige Probleme:
Das Script hat die Worte "Highlight" und "Note" fest im Code. Das ist natürlich "blöd", denn das funktioniert nur, wenn man seinen Kindle auf Englisch eingestellt hat. Im Deutschen heisst es z.B. "Markierung" und "Notiz".
Das wollte ich als erstes lösen, indem ich in der Konfig Datei (siehe github) zwei weitere Variablen aufgenommen habe (LANG_HIGHLIGHT und LANG_NOTE), die sollen dann im eigentliche python script eingebaut werden. Prinzipiell funktioniert das auch, jedoch bin ich dadurch auf ein anderes Problem gestossen, wo es mich nun verlässt ......
In der englischen Version sieht eine clipping.txt so aus:
Code: Alles auswählen
-Maze (McDermott, J.M.)
- Your Highlight on Location 51-51 | Added on Thursday, March 6, 2014 2:51:44 AM
The blood in her veins will keep her safe just as it kept me safe when I was pregnant
==========
Code: Alles auswählen
BLACKOUT - Morgen ist es zu spät: Roman (German Edition) (Elsberg, Marc)
- Your Highlight on page 417 | location 5319-5319 | Added on Sunday, 23 March 2014 13:29:27
Dahinter lief ein junger Mann ganz in Weiß
==========
Code: Alles auswählen
# Parse the clippings.txt file
lines = [line.strip() for line in codecs.open(config['CLIPPINGS_FILE'], 'r', 'utf-8-sig')]
for line in lines:
line_count = line_count + 1
if (line_count == 1 or is_title == 1):
# ASSERT: this is a title line
title = line.encode('ascii', 'ignore')
prev_title = 1
is_title = 0
note_type_result = note_type = l = l_result = location = ""
continue
else:
# ASSERT: not the first line
if (prev_title == 1):
# ASSERT: this is the date line
#print(line)
result = re.search( r'(.*)Added on (.*)', line, re.M|re.I)
if (result is not None):
note_type_result = result.group(1)
#print(note_type_result)
if (note_type_result.find("Highlight")>0):
note_type = "Highlight"
else:
note_type = "Note"
l = note_type_result
l_result = re.search(r'(\d+)', l, re.M|re.I)
location = l_result.group(1)
note_date = parser.parse(result.group(2))
if (note_date >= last_date):
# ASSERT: We haven't collected this note yet, so do it now.
str_date = note_date.strftime("%Y-%m-%d %H:%M:%S")
if (title_notes.has_key(title)):
title_notes[title] += 'On ' + str_date + ', a ' + note_type + ' starting at location ' + location + '\n'
else:
title_notes[title] = 'On ' + str_date + ', a ' + note_type + ' starting at location ' + location + '\n'
prev_title = 0
collect = 1
continue
elif (line == RECORD_DELIM):
# ASSERT: end of record
if (note_type == "Highlight" and highlight == 1):
title_notes[title] += '</i></div></blockquote><i><br/></i>\n'
if (note_type == "Note" and notenote == 1):
title_notes[title] += '</div></blockquote><br/>\n';
collect = 0
is_title = 1
highlight = 0
notenote = 0
continue
else:
# ASSERT: collecting lines for the current title/date
if (collect == 1):
if (note_type == "Highlight" and highlight == 0):
title_notes[title] += '<div><br/></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><i style="background-color:rgb(255, 250, 165);-evernote-highlight:true;">' + line + '\n'
highlight = 1
elif (note_type == "Note" and notenote == 0):
title_notes[title] += '<div><br/></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>' + line + '\n'
notenote = 1
else:
title_notes[title] += line + '\n'
Vermute ich, dass es hier ein Problem mit der Umwandlung des "deutschen" Datums zu datetime gibt.mbp-lan:~ adieball$ python kindle2en.py
Traceback (most recent call last):
File "kindle2en.py", line 232, in <module>
main(sys.argv[1:])
File "kindle2en.py", line 149, in main
if (note_date >= last_date):
UnboundLocalError: local variable 'note_date' referenced before assignment
mbp-lan:~ adieball$
Liege ich hier richtig? Kann mir jemand eventuell hier unterstützend unter die Arme greifen?
Ich würde das Script gerne so wie es ist zum laufen bekommen um dann zu verstehen, was ich ändern muss (durch z.B. eine andere .cfg Datei) um es flexibler zu machen.
Vielen Dank im Voraus.