87 lines
2.4 KiB
Python
87 lines
2.4 KiB
Python
import json
|
|
import hashlib
|
|
|
|
|
|
base91chars = (
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
|
'0123456789!#$%&()*+,-./:;<=>?@[]^_`{|}~'
|
|
)
|
|
|
|
def anki_guid(text: str) -> str:
|
|
# SHA1-Hash des Front-Felds berechnen
|
|
h = hashlib.sha1(text.encode('utf-8')).digest()
|
|
x = int.from_bytes(h[:8], 'little') # Anki nimmt die ersten 8 Bytes, little-endian
|
|
|
|
# Kodierung in Base91-Zeichenkette (10 Zeichen)
|
|
chars = []
|
|
for _ in range(10):
|
|
chars.append(base91chars[x % len(base91chars)])
|
|
x //= len(base91chars)
|
|
|
|
return ''.join(chars)
|
|
|
|
with open('Schwedisch_Goetheverlag/deck.json', 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
|
|
|
|
original_notes = data['notes']
|
|
new_notes = []
|
|
existing_signatures = set()
|
|
existing__notemodels = []
|
|
for i in data['note_models']:
|
|
existing__notemodels.append(i['crowdanki_uuid'])
|
|
|
|
for note in original_notes:
|
|
fields = note['fields']
|
|
note_model_uuid = note['note_model_uuid']
|
|
if len(fields) < 3:
|
|
continue
|
|
|
|
original_front = fields[0] # "ich"
|
|
original_back = fields[1] # "jag"
|
|
original_back_audio = fields[2] # "[sound:Jag-4718d0.mp3]"
|
|
original_front_uuid = note_model_uuid
|
|
new_uuid = None
|
|
new_note = None
|
|
|
|
if original_front_uuid == existing__notemodels[0]:
|
|
# Original:
|
|
# Deutsch (Front)
|
|
# Sverige (Back)
|
|
# Audio (Backaudio)
|
|
# Neu:
|
|
# Sverige (Front)
|
|
# Audio (Frontaudio)
|
|
# Deutsch (Back)
|
|
|
|
new_note = {
|
|
"__type__": "Note",
|
|
"fields": [
|
|
original_back,
|
|
original_back_audio,
|
|
original_front
|
|
],
|
|
"guid": f"{anki_guid(original_back)}",
|
|
"note_model_uuid": existing__notemodels[1],
|
|
"tags": []
|
|
}
|
|
new_notes.append(new_note)
|
|
|
|
# new_note = {
|
|
# "__type__": "Note",
|
|
# "fields": [
|
|
# original_front,
|
|
# original_front_audio,
|
|
# original_back
|
|
# ],
|
|
# "guid": f"{anki_guid(original_front)}",
|
|
# "note_model_uuid": note_model_uuid[1]
|
|
# }
|
|
# new_notes.append(new_note)
|
|
# print(new_note
|
|
|
|
|
|
data['notes'].extend(new_notes)
|
|
|
|
with open('Schwedisch_Goetheverlag/crowdanki_export_dupliziert.json', 'w', encoding='utf-8') as f:
|
|
json.dump(data, f, ensure_ascii=False, indent=2) |