-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathindex.js
More file actions
172 lines (172 loc) · 4.2 KB
/
Copy pathindex.js
File metadata and controls
172 lines (172 loc) · 4.2 KB
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
const fs = require('hexo-fs');
const path = require('path');
const hexoLog = require('hexo-log');
const log = (typeof hexoLog.default === 'function' ? hexoLog.default : hexoLog)({
debug: false,
silent: false
});
const {
getBiliData
} = require('./lib/get-bili-data');
const {
getBgmData
} = require('./lib/get-bgm-data');
const {
getBgmv0Data
} = require('./lib/get-bgmv0-data');
const {
getAnilistData
} = require('./lib/get-anilist-data');
const {
getSimklData
} = require('./lib/get-simkl-data');
const {
hoistBangumiAssets
} = require('./lib/asset-hoist');
if (typeof URL !== 'function') {
const {
URL
} = require('url');
global.URL = URL;
}
const COMMAND_OPTIONS = {
options: [{
name: '-u, --update',
desc: 'Update data'
}, {
name: '-d, --delete',
desc: 'Delete data'
}]
};
const DATA_TYPES = {
bangumi: {
jsonFile: 'bangumis.json',
configKey: 'bangumi',
alias: 'bgm'
},
cinema: {
jsonFile: 'cinemas.json',
configKey: 'cinema',
alias: 'cnm'
},
game: {
jsonFile: 'games.json',
configKey: 'game',
alias: 'gm'
}
};
Object.entries(DATA_TYPES).forEach(([type, config]) => {
hexo.extend.generator.register(`bangumis-${type}`, function (locals) {
if (!this?.config?.[config.configKey]?.enable) {
return;
}
return require('./lib/bangumi-generator').call(this, locals, type);
});
});
hexo.extend.filter.register('after_render:html', html => hoistBangumiAssets(html));
hexo.extend.filter.register('after_post_render', data => {
if (data.path.split('.').at(-1) === 'json') {
data.content = data._content;
}
return data;
}, 11);
const validateConfig = config => {
if (!config) {
log.info('Please add config to _config.yml');
return false;
}
if (!config.enable) {
return false;
}
if (!config.vmid) {
log.info('Please add vmid to _config.yml');
return false;
}
return true;
};
const handleDataDelete = (sourceDir, type) => {
const jsonPath = path.join(sourceDir, `/_data/${DATA_TYPES[type].jsonFile}`);
if (fs.existsSync(jsonPath)) {
fs.unlinkSync(jsonPath);
log.info(`${type} data has been deleted`);
}
};
const handleDataUpdate = async function (config, type, sourceDir, args) {
const baseConfig = {
vmid: config.vmid,
showProgress: config.progress ?? true,
sourceDir,
extraOrder: config.extraOrder,
pagination: config.pagination,
coverMirror: config.coverMirror ?? ''
};
switch (config.source) {
case 'bgm':
case 'bangumi':
return getBgmData({
...baseConfig,
type,
proxy: config.proxy,
infoApi: config.bgmInfoApi,
host: `${config.source}.tv`,
skipNsfw: config.skipNsfw ?? false
});
case 'bgmv0':
{
const typeMapping = {
bangumi: 2,
cinema: 6,
game: 4
};
return getBgmv0Data({
...baseConfig,
type: typeMapping[type],
proxy: config.proxy
});
}
case 'anilist':
if (type === 'bangumi') {
return getAnilistData({
...baseConfig,
type: 'ANIME'
});
}
log.info(`${config.source} not support for ${type}`);
return;
case 'simkl':
return getSimklData({
...baseConfig,
type
});
default:
return getBiliData({
...baseConfig,
type,
useWebp: config.webp,
SESSDATA: typeof args.u === 'string' ? args.u : null
});
}
};
Object.entries(DATA_TYPES).forEach(([type, config]) => {
const options = {
alias: config.alias,
...COMMAND_OPTIONS
};
hexo.extend.console.register(type, `Generate pages of ${type} for Hexo`, options, function (args) {
if (args.d) {
handleDataDelete(this.source_dir, type);
} else if (args.u) {
const typeConfig = this.config[config.configKey];
if (!validateConfig(typeConfig)) {
return;
}
if (type === 'game' && typeConfig.source !== 'bgmv0') {
log.info(`${typeConfig.source} not support for game`);
return;
}
handleDataUpdate(typeConfig, type, this.source_dir, args);
} else {
log.info(`Unknown command, please use "hexo ${type} -h" to see the available commands`);
}
});
});