-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
140 lines (111 loc) · 3.29 KB
/
main.js
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
import fs from "fs/promises";
import Db from "better-sqlite3";
import slugify from "slugify";
const ASSETS_DATABASE_DIRECTORY = `${process.env.HOME}/Library/Containers/com.apple.iBooksX/Data/Documents/BKLibrary`;
const ANNOTATIONS_DATABASE_DIRECTORY = `${process.env.HOME}/Library/Containers/com.apple.iBooksX/Data/Documents/AEAnnotation`;
const getDatabaseFile = async (directory) => {
let databaseFile;
let files;
try {
files = await fs.readdir(directory);
} catch (e) {
process.exit(1);
}
const dbFilename = files
.filter((filename) => filename.endsWith(".sqlite"))
.slice(-1);
databaseFile = `${directory}/${dbFilename}`;
return databaseFile;
};
(async () => {
// ASSETS
const assetsDatabaseFile = await getDatabaseFile(ASSETS_DATABASE_DIRECTORY);
if (!assetsDatabaseFile) {
process.exit(1);
}
const dbAssets = new Db(assetsDatabaseFile);
const assets = dbAssets
.prepare(
`SELECT
ZASSETID as id, ZTITLE AS title, ZAUTHOR AS author, ZLANGUAGE as language, ZPATH as path
FROM ZBKLIBRARYASSET
WHERE ZTITLE IS NOT NULL`
)
.all();
const collections = dbAssets
.prepare(
`SELECT
ZCOLLECTIONID as id, ZTITLE AS title, Z_PK as pk
FROM ZBKCOLLECTION
WHERE Z_PK > 8`
)
.all();
const members = dbAssets
.prepare(
`SELECT
ZCOLLECTION as id, ZASSETID as assetId
FROM ZBKCOLLECTIONMEMBER
WHERE ZCOLLECTION > 8`
)
.all();
// COLLECTIONS
for (const collection of collections) {
const assetsInCollection = {
id: collection.id,
pk: collection.pk,
title: collection.title,
members: [],
};
const collectionMembers = members.filter((m) => {
return m.id == collection.pk;
});
for (const member of collectionMembers) {
const asset = assets.filter((a) => {
return a.id == member.assetId;
});
if (asset) {
assetsInCollection.members.push(asset);
}
}
await fs.writeFile(
`./export/${slugify(collection.title)}.json`,
JSON.stringify(assetsInCollection)
);
}
await fs.writeFile("./export/assets.json", JSON.stringify(assets));
// ANNOTATIONS
const annotationsDatabaseFile = await getDatabaseFile(
ANNOTATIONS_DATABASE_DIRECTORY
);
if (!annotationsDatabaseFile) {
process.exit(1);
}
const dbAnnotations = new Db(annotationsDatabaseFile);
const annotations = dbAnnotations
.prepare(
`SELECT
ZANNOTATIONASSETID as assetId, ZANNOTATIONSELECTEDTEXT as selectedText, ZFUTUREPROOFING5 as chapter, ZANNOTATIONCREATIONDATE as creationDate, ZANNOTATIONMODIFICATIONDATE as modificationDate
FROM ZAEANNOTATION
WHERE ZANNOTATIONDELETED = 0 AND ZANNOTATIONSELECTEDTEXT NOT NULL`
)
.all();
const annotationsBook = [];
for (const annotation of annotations) {
const asset = assets.filter((a) => {
return a.id == annotation.assetId;
});
annotationsBook.push({
assetId: annotation.assetId,
selectedText: annotation.selectedText,
chapter: annotation.chapter,
creationDate: annotation.creationDate,
modificationDate: annotation.modificationDate,
asset: asset,
});
}
await fs.writeFile(
"./export/annotations.json",
JSON.stringify(annotationsBook)
);
process.exit();
})();