forked from ykozxy/bangumi-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_util.ts
444 lines (397 loc) · 17.2 KB
/
data_util.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import axios from "axios";
import fs from "fs";
import stringSimilarity from "string-similarity";
import {ChinaAnimeData} from "../types/china_anime_data";
import {GlobalAnimeData} from "../types/global_anime_data";
import {EtagType, Relation} from "../types/cache";
import {getEtagCache, setEtagCache} from "./cache_util";
import {bangumiClient} from "./bangumi_client";
import {autoLog, autoLogException, LogLevel} from "./log_util";
import {config} from "./config_util";
let china_anime_data: ChinaAnimeData.Item[];
let global_anime_data: GlobalAnimeData.Item[];
const bgm_id_map: Map<string, ChinaAnimeData.Item> = new Map();
const mal_id_map: Map<string, GlobalAnimeData.Item[]> = new Map();
let known_relations: Relation[] = [];
/**
* @description Load anime data from local cache, and update cache if new data is available.
* This should be called before accessing china_anime_data and global_anime_data.
*/
export async function buildDatabase(): Promise<void> {
if (!fs.existsSync(config.cache_path)) {
fs.mkdirSync(config.cache_path);
}
// Smart update data
await autoUpdateDatabase();
// Update exported variables
china_anime_data = ChinaAnimeData.loadData(JSON.parse(fs.readFileSync(config.cache_path + '/china_anime.json').toString()));
global_anime_data = GlobalAnimeData.loadData(JSON.parse(fs.readFileSync(config.cache_path + '/global_anime.json').toString()));
// Build id map
bgm_id_map.clear();
mal_id_map.clear();
china_anime_data.forEach(item => {
const id = getBgmId(item);
if (!id) {
// console.warn(`[${loadData.name}] No bgm.tv record for ${item.title}`);
return;
}
bgm_id_map.set(id, item);
});
global_anime_data.forEach(item => {
const id = item.sites["MyAnimeList"];
if (!id) {
// console.warn(`[${loadData.name}] No bgm.tv record for ${item.title}`);
return;
}
if (!mal_id_map.has(id)) {
mal_id_map.set(id, []);
}
(<GlobalAnimeData.Item[]>mal_id_map.get(id)).push(item);
});
// Load cached relations
if (fs.existsSync(config.cache_path + '/known_relations.json')) {
known_relations = JSON.parse(fs.readFileSync(config.cache_path + '/known_relations.json').toString());
}
}
/**
* @description Free memory. Clear china_anime_data and global_anime_data.
*/
export function releaseDatabase(): void {
china_anime_data = [];
global_anime_data = [];
}
/**
* @description Get china anime object by bgm.tv id.
* @param bgm_id bgm.tv id of the anime.
* @param check_fields Whether to ensure all fields are present when fetching from bgm.tv.
* @returns The anime data, or null if not found.
*/
export async function getChinaAnimeItem(bgm_id: string, check_fields: boolean = true): Promise<ChinaAnimeData.Item | null> {
const cn_anime = bgm_id_map.get(bgm_id);
if (cn_anime) return cn_anime;
autoLog(`Bgm id ${bgm_id} not found in database, fetching and building from bgm.tv...`, "getChinaAnimeItem", LogLevel.Debug);
const bgm_subject = await bangumiClient.getSubjectById(bgm_id);
if (!bgm_subject) return null;
// Ensure bgm_subject has all required fields
if (check_fields && !bgm_subject.date) return null;
let type: ChinaAnimeData.Type;
switch (bgm_subject.platform) {
case "TV":
type = ChinaAnimeData.Type.Tv;
break;
case "OVA":
type = ChinaAnimeData.Type.Ova;
break;
case "WEB":
type = ChinaAnimeData.Type.Web;
break;
case "剧场版":
type = ChinaAnimeData.Type.Movie;
break;
default:
return null;
}
return {
// Set date to now if date is null
begin: bgm_subject.date ? new Date(bgm_subject.date) : new Date(),
sites: [{site: "bangumi", id: bgm_id}],
title: bgm_subject.name,
titleTranslate: bgm_subject.name_cn ? {"zh-Hans": [bgm_subject.name_cn]} : {},
type: type,
};
}
/**
* @description Get global anime object by myanimelist id.
* @param mal_id myanimelist id of the anime.
* @returns The anime data, or null if not found.
*/
export async function getGlobalAnimeItemByMal(mal_id: string): Promise<GlobalAnimeData.Item | null> {
let res = mal_id_map.get(mal_id);
if (res) return res[0];
return null;
}
/**
* @description Get global anime object by anilist id. Because this function is not frequently called, performance is not optimized.
* @param anilist_id anilist id of the anime.
*/
export function getGlobalAnimeItemByAnilist(anilist_id: string): GlobalAnimeData.Item | null {
for (let item of global_anime_data) {
if (item.sites["AniList"] == anilist_id) {
return item;
}
}
return null;
}
/**
* @description Try to match a China anime object to global object.
* @param cn China anime object.
* @param titleSimilarityThreshold Lower bound of title similarity when fuzzy matching.
* @returns The matched global anime object, or null if not found.
*/
export async function matchChinaToGlobal(cn: ChinaAnimeData.Item, titleSimilarityThreshold: number = 0.75): Promise<GlobalAnimeData.Item | null> {
// First, check known relations
const bgm_id = getBgmId(cn);
if (!bgm_id) {
autoLog(`Failed to find BGM id for "${cn.title}.`, "matchEntry", LogLevel.Warn);
// incrementProgressBar();
return null;
}
let mal_id = known_relations.find(r => r.bgm_id === bgm_id)?.mal_id;
if (mal_id) {
autoLog(`Found known relation for "${cn.title}".`, "matchEntry", LogLevel.Debug);
// incrementProgressBar();
return await getGlobalAnimeItemByMal(mal_id);
}
// Construct all titles in cn database
const cnTitles = [cn.title];
for (const [, names] of Object.entries(cn.titleTranslate)) if (names) cnTitles.push(...names);
// Fuzzy match titles in global database
const fuzzyMatch: { anime: GlobalAnimeData.Item, score: number }[] = [];
let bestMatch: { anime?: GlobalAnimeData.Item, score: number } = {score: 0};
global_anime_data.forEach(gl => {
const glTitles = [gl.title, ...gl.synonyms];
for (const title of cnTitles) {
const score = stringSimilarity.findBestMatch(title, glTitles).bestMatch.rating;
if (score >= titleSimilarityThreshold)
fuzzyMatch.push({anime: gl, score});
if (score > bestMatch.score)
bestMatch = {anime: gl, score};
}
});
fuzzyMatch.sort((a, b) => b.score - a.score);
// If all fuzzy match results have score below threshold, use the best match
// In this case, strict mode is enabled to (hopefully) avoid false positive
let strictMode = false;
if (fuzzyMatch.length == 0 && bestMatch.anime) {
fuzzyMatch.push({anime: bestMatch.anime, score: bestMatch.score});
strictMode = true;
}
// Check aired date and format
for (const {anime, score} of fuzzyMatch) {
if (!await compareChinaWithGlobal(cn, anime, strictMode)) continue;
// Store match to relations
const mal_id = anime.sites["MyAnimeList"];
if (!mal_id) {
// progressBarLog(`[${matchChinaToGlobal.name}] Failed to find MAL id for "${cn.title}"`);
continue;
}
known_relations.push({
bgm_id,
mal_id,
title: cn.title,
});
// Save known relations
fs.writeFileSync(config.cache_path + '/known_relations.json', JSON.stringify(known_relations, null, 4));
autoLog(`score=${score.toPrecision(3)}, "${cn.title}" matched to "${anime.title}"`, "matchEntry", LogLevel.Info);
// incrementProgressBar();
return anime;
}
// incrementProgressBar();
return null;
}
/**
* @description Try to match a Global anime object to cn object.
* @param gl Global anime object.
* @param titleSimilarityThreshold Lower bound of title similarity when fuzzy matching.
* @returns The matched global anime object, or null if not found.
*/
export async function matchGlobalToChina(gl: GlobalAnimeData.Item, titleSimilarityThreshold: number = 0.75): Promise<ChinaAnimeData.Item | null> {
// First, check known relations
const mal_id = gl.sites["MyAnimeList"];
if (!mal_id) {
autoLog(`Failed to find MAL id for "${gl.title}.`, "matchEntry", LogLevel.Warn);
// incrementProgressBar();
return null;
}
let bgm_id = known_relations.find(r => r.mal_id === mal_id)?.bgm_id;
if (bgm_id) {
autoLog(`Found known relation for "${gl.title}".`, "matchEntry", LogLevel.Debug);
// incrementProgressBar();
return await getChinaAnimeItem(bgm_id);
}
// Construct all titles in global database
const glTitles = [gl.title, ...gl.synonyms];
// Fuzzy match titles in cn database
const fuzzyMatch: { cn_anime: ChinaAnimeData.Item, score: number }[] = [];
let bestMatch: { anime?: ChinaAnimeData.Item, score: number } = {score: 0};
china_anime_data.forEach(cn => {
const cnTitles = [cn.title];
for (const [, names] of Object.entries(cn.titleTranslate)) if (names) cnTitles.push(...names);
for (const title of cnTitles) {
const score = stringSimilarity.findBestMatch(title, glTitles).bestMatch.rating;
if (score >= titleSimilarityThreshold)
fuzzyMatch.push({cn_anime: cn, score});
if (score > bestMatch.score)
bestMatch = {anime: cn, score};
}
});
fuzzyMatch.sort((a, b) => b.score - a.score);
// If all fuzzy match results have score below threshold, use the best match
// In this case, strict mode is enabled to (hopefully) avoid false positive
let strictMode = false;
if (fuzzyMatch.length == 0 && bestMatch.anime) {
fuzzyMatch.push({cn_anime: bestMatch.anime, score: bestMatch.score});
strictMode = true;
}
// Check aired date and format
for (const {cn_anime, score} of fuzzyMatch) {
if (!await compareChinaWithGlobal(cn_anime, gl, strictMode)) continue;
// Store match to relations
const bgm_id = getBgmId(cn_anime);
if (!bgm_id) {
continue;
}
known_relations.push({
mal_id,
bgm_id,
title: gl.title,
});
// Save known relations
fs.writeFileSync(config.cache_path + '/known_relations.json', JSON.stringify(known_relations, null, 4));
autoLog(`score=${score.toPrecision(3)}, "${gl.title}" matched to "${cn_anime.title}"`, "matchEntry", LogLevel.Info);
// incrementProgressBar();
return cn_anime;
}
// incrementProgressBar();
return null;
}
/**
* @description Check if the China and global anime are matched.
* @param china The china anime item.
* @param global The global anime item.
* @param strictMode When true, more strict check is performed.
* @param matchMonth When true, air date will be checked to month.
* @param matchFormat When true, same format will be ensured.
*/
export async function compareChinaWithGlobal(china: ChinaAnimeData.Item, global: GlobalAnimeData.Item, strictMode: boolean, matchMonth: boolean = true, matchFormat: boolean = true): Promise<boolean> {
// If years mismatch, skip
if (china.begin.getFullYear() != global.animeSeason.year)
return false;
// Check season
const month = china.begin.getMonth() + 1;
if (matchMonth && global.animeSeason.season !== GlobalAnimeData.Season.Undefined) {
if (strictMode) {
if (global.animeSeason.season !== GlobalAnimeData.Season.Winter && month < 4) return false;
if (global.animeSeason.season !== GlobalAnimeData.Season.Spring && month < 7) return false;
if (global.animeSeason.season !== GlobalAnimeData.Season.Summer && month < 10) return false;
if (global.animeSeason.season !== GlobalAnimeData.Season.Fall && month < 13) return false;
} else {
switch (month) {
case 1:
case 2:
if (global.animeSeason.season !== GlobalAnimeData.Season.Winter) return false;
break;
case 3:
if (global.animeSeason.season !== GlobalAnimeData.Season.Winter && global.animeSeason.season !== GlobalAnimeData.Season.Spring) return false;
break;
case 4:
case 5:
if (global.animeSeason.season !== GlobalAnimeData.Season.Spring) return false;
break;
case 6:
if (global.animeSeason.season !== GlobalAnimeData.Season.Spring && global.animeSeason.season !== GlobalAnimeData.Season.Summer) return false;
break;
case 7:
case 8:
if (global.animeSeason.season !== GlobalAnimeData.Season.Summer) return false;
break;
case 9:
if (global.animeSeason.season !== GlobalAnimeData.Season.Summer && global.animeSeason.season !== GlobalAnimeData.Season.Fall) return false;
break;
case 10:
case 11:
if (global.animeSeason.season !== GlobalAnimeData.Season.Fall) return false;
break;
case 12:
if (global.animeSeason.season !== GlobalAnimeData.Season.Fall && global.animeSeason.season !== GlobalAnimeData.Season.Winter) return false;
break;
default:
return false;
}
}
}
// Check format
let mismatch = false;
if (matchFormat && global.type !== GlobalAnimeData.Type.Unknown) {
switch (china.type) {
case ChinaAnimeData.Type.Tv:
if (global.type !== GlobalAnimeData.Type.Tv) mismatch = true;
break;
case ChinaAnimeData.Type.Web:
if (global.type !== GlobalAnimeData.Type.Ona) mismatch = true;
break;
case ChinaAnimeData.Type.Ova:
if (global.type !== GlobalAnimeData.Type.Ova && global.type !== GlobalAnimeData.Type.Special)
mismatch = true;
break;
case ChinaAnimeData.Type.Movie:
if (global.type !== GlobalAnimeData.Type.Movie) mismatch = true;
break;
}
}
// Check episode count as a backup
return !(mismatch && (!strictMode && (await bangumiClient.getSubjectById(<string>getBgmId(china)))?.total_episodes !== global.episodes));
}
/**
* @description Get bgm id of a china anime object.
* @param cn An cn anime object.
*/
export function getBgmId(cn: ChinaAnimeData.Item): string | null {
// TODO: construct map from cnItem to bgm_id for anime not in database
return cn.sites.find(s => s.site === "bangumi")?.id || null;
}
/**
* @description Get china anime object from a global anime object.
* @param malId
*/
export function getAnilistId(malId: string): string | null {
let gls = mal_id_map.get(malId);
if (!gls) return null;
for (let gl of gls) {
if (gl.sites["AniList"]) return gl.sites["AniList"];
}
return null;
}
/**
* @description Check the version of local anime database cache and update if necessary.
*/
async function autoUpdateDatabase(): Promise<void> {
// Fetch etag from url
let china_etag: string;
let global_etag: string
try {
china_etag = (await axios.head(config.china_anime_database_url)).headers['etag'];
global_etag = (await axios.head(config.global_anime_database_url)).headers['etag'];
} catch (e) {
autoLog("Failed to fetch etag from url.", "updateDatabase", LogLevel.Error);
autoLogException(e as Error);
return;
}
// Update if etag changed
if (china_etag !== getEtagCache(EtagType.China) || !fs.existsSync(config.cache_path + '/china_anime.json')) {
try {
autoLog("Updating china_anime database from " + config.china_anime_database_url, "updateDatabase", LogLevel.Info);
const china_data = await axios.get(config.china_anime_database_url).then(res => res.data);
fs.writeFileSync(config.cache_path + '/china_anime.json', JSON.stringify(china_data));
} catch (e) {
autoLog("Unable to fetch china_anime database: ", "updateDatabase", LogLevel.Error);
autoLogException(e as Error);
}
setEtagCache(EtagType.China, china_etag);
}
if (global_etag !== getEtagCache(EtagType.Global) || !fs.existsSync(config.cache_path + '/global_anime.json')) {
try {
autoLog("Updating global_anime database from " + config.global_anime_database_url, "updateDatabase", LogLevel.Info);
const global_data = await axios.get(config.global_anime_database_url).then(res => res.data);
fs.writeFileSync(config.cache_path + '/global_anime.json', JSON.stringify(global_data));
} catch (e) {
autoLog("Unable to fetch global_anime database: ", "updateDatabase", LogLevel.Error);
autoLogException(e as Error);
}
setEtagCache(EtagType.Global, global_etag);
}
}
// Update cache every 12 hours
// const schedule = require("node-schedule");
// schedule.scheduleJob('0 */12 * * *', loadData);
export {china_anime_data, global_anime_data};