-
Notifications
You must be signed in to change notification settings - Fork 24
/
appledict2sqlite3.py
executable file
·63 lines (56 loc) · 1.59 KB
/
appledict2sqlite3.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
#!/usr/bin/env python
#coding: utf-8
"""
Convert XML file created from an Apple Dictionary Base.body file
into an SQLite3 database.
"""
import os
import sys
import sqlite3
from xml.etree import ElementTree
# fetch name of the output file or use dictionary.db as default
output = None
if not sys.argv[1:]:
output = "dictionary.db"
else:
output = sys.argv[1]
# rename file if it exists already
if os.path.exists(output):
import random
new = output + str(int(10000 * random.random()))
os.rename(output, new)
sys.stderr.write("renamed {} to {}\n".format(output, new))
# create connection and the file
conn = sqlite3.connect(output)
# create the table
c = conn.cursor()
c.execute("""
CREATE TABLE definitions (id text, title text, entry text)
""")
# read input line by line and insert the entires
linenum = 0
for line in sys.stdin:
linenum += 1
try:
root = ElementTree.fromstring(line)
# obtain the title
id = root.get("id")
for key in root.keys():
if key.endswith("title"):
title = root.get(key)
break
c.execute("INSERT INTO definitions VALUES (?, ?, ?)",
(id, title, memoryview(bytes(line,encoding="utf-8"))))
except ElementTree.ParseError:
# malformed XML
sys.stderr.write("invalid XML on line {}\n".format(linenum))
import traceback
traceback.print_exc()
break
except KeyboardInterrupt:
# Shutdown
sys.stderr.write("shutdown requested\n")
break
# save changes and close the connection
conn.commit()
conn.close()