Skip to content

Commit 34cf9af

Browse files
committed
Add download_images
1 parent 31324ef commit 34cf9af

File tree

6 files changed

+46
-15
lines changed

6 files changed

+46
-15
lines changed

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
# hexo-bangumis
22

3-
![](https://nodei.co/npm/hexo-bangumis.png?downloads=true&downloadRank=true&stars=true)
3+
[![](https://nodei.co/npm/hexo-bangumis.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/hexo-bangumis)
44

55
## 介绍
66

77
**为 Hexo 添加 [Bangumi](https://bangumi.tv/) 追番页面,参考自 [HCLonely/hexo-bilibili-bangumi](https://github.com/HCLonely/hexo-bilibili-bangumi)**.
88

9+
## 特色功能
10+
11+
* 使用 [bgm.tv](https://bgm.tv) 的官方 [API](https://github.com/bangumi/api) 来进行爬取,番剧更多,可用性更高
12+
* 支持数据缓存及番剧封面图本地化,防止因上游服务挂掉导致连锁效应,爬取好番剧后可纯离线场景下部署
13+
* 极端条件下可通过编辑文件来自定义番剧列表,可自定义添加 [bgm.tv](https://bgm.tv) 没有的番剧
14+
* 总的来说,只要你爬取过这个番剧,那么就算将来 [bgm.tv](https://bgm.tv) 上把这个番剧删掉了,你也可以继续使用这个番剧
15+
16+
## 预览
17+
18+
![](img/preview.png)
19+
920
## 安装
1021

1122
在 Hexo 文件夹下执行:
@@ -19,16 +30,16 @@ $ npm install hexo-bangumis --save
1930

2031
``` yaml
2132
bangumis:
22-
enable: true # 是否启用
33+
enable: true # 是否启用
2334
path: bangumis/index.html # 生成追番页面的路径
24-
show: 1 # 想看,在看,看完
25-
title: '追番列表' # 标题
35+
show: 1 # 想看,在看,看完
36+
title: '追番列表' # 标题
2637
quote: '生命不息,追番不止' # 格言
27-
color_meta: "#555" # 追番项元数据的颜色
28-
color_summary: "#555" # 追番项简介的颜色
29-
bgmtv_uid: mmdjiji # bgm.tv的uid
30-
download_image: true # 下载图片并使用本地图片,否则使用bgm.tv提供的网络图源
31-
38+
color_meta: "#555" # 追番项元数据的颜色
39+
color_summary: "#555" # 追番项简介的颜色
40+
bgmtv_uid: mmdjiji # bgm.tv的uid
41+
download_image: true # 下载图片并使用本地图片,否则使用bgm.tv提供的网络图源
42+
lazyload: true # 是否开启懒加载
3243
```
3344
3445
## 使用
@@ -47,7 +58,6 @@ $ hexo bangumis -d
4758

4859
登录 [bgm.tv](https://bgm.tv) 后打开控制台(快捷键 `Ctrl` + `Shift` + `J`),输入 `CHOBITS_UID` 后按回车,得到的数字就是 `uid`~
4960

50-
5161
## Lisense
5262

5363
[Apache Licence 2.0](LICENSE)

dist/templates/bgm-template.ejs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<div class="bangumi-item">
22
<div class="bangumi-picture">
3-
<img src="<%= lazyload ? (loading || "https://cdn.jsdelivr.net/npm/[email protected]/lib/img/loading.gif") : `https://lain.bgm.tv/pic/cover/c/${item.image}` %>" <%- lazyload ? ` data-src="https://lain.bgm.tv/pic/cover/c/${item.image}"` : '' %> referrerPolicy="no-referrer" width="110" style="width:110px;margin:20px auto;" />
3+
<% if(download_image) { %>
4+
<img src="<%= lazyload ? ("/images/loading.gif") : `/images/${item.image}` %>" <%- lazyload ? ` data-src="/images/${item.image}"` : '' %> referrerPolicy="no-referrer" width="110" style="width:110px;margin:20px auto;" />
5+
<% } else { %>
6+
<img src="<%= lazyload ? ("/images/loading.gif") : `https://lain.bgm.tv/pic/cover/c/${item.image}` %>" <%- lazyload ? ` data-src="https://lain.bgm.tv/pic/cover/c/${item.image}"` : '' %> referrerPolicy="no-referrer" width="110" style="width:110px;margin:20px auto;" />
7+
<% } %>
48
</div>
59
<div class="bangumi-info">
610
<div class="bangumi-title">

img/preview.png

230 KB
Loading

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ hexo.extend.generator.register('bangumis', function (locals) {
2222
if (!this?.config?.bangumis?.enable) {
2323
return;
2424
}
25+
if (!fs.existsSync(path.join(this.source_dir, '/images/loading.gif'))) {
26+
fs.copyFile(path.join(__dirname, 'img/loading.gif'), path.join(this.source_dir, '/images/loading.gif'));
27+
}
2528
return require('./dist/bangumi-generator').call(this, locals);
2629
});
2730
hexo.extend.console.register('bangumis', 'Generate pages of bangumis for Hexo', options, function (args) {

src/lib/bangumi-generator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = async function (locals) {
4040
color_meta: config.bangumis.color_meta ? `style="color:${config.bangumis.color_meta}"` : '',
4141
color: config.bangumis.color ? `style="color:${config.bangumis.color}"` : '',
4242
lazyload: config.bangumis.lazyload ?? true,
43+
download_image: config.bangumis.download_image ?? false,
4344
wantWatch,
4445
watched,
4546
watching,

src/lib/get-bgm-data.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const getBangumi = async (bgm, cachePath) => {
6565
if (await fs.exists(savedPath)) {
6666
try {
6767
const read = await JSON.parse(await fs.readFile(savedPath));
68-
if(read.id === bangumi_id) {
68+
if (read.id === bangumi_id) {
6969
return read;
7070
}
7171
throw new Error(`Id not match when trying to load id=${bangumi_id}`);
@@ -163,15 +163,25 @@ const getBangumi = async (bgm, cachePath) => {
163163
log.info(`Get bangumi (${bangumi_id}) Failed, maybe invalid!`);
164164
};
165165

166-
const getImage = async (image_url) => {
167-
166+
const getImage = (image_url, imagesPath) => {
167+
if (image_url && !fs.existsSync(`${imagesPath}/${image_url}`)) {
168+
fetch(`https://lain.bgm.tv/pic/cover/c/${image_url}`, {
169+
method: 'GET',
170+
headers: { 'Content-Type': 'application/octet-stream' }
171+
}).then((res) => res.buffer())
172+
.then((image) => {
173+
fs.writeFile(`${imagesPath}/${image_url}`, image, 'binary', (err) => {
174+
console.error(err);
175+
});
176+
});
177+
}
168178
};
169179

170180
module.exports.getBgmData = async (bgmtv_uid, download_image, source_dir) => {
171181
// create folders if not exist
172182
const bangumisPath = path.join(source_dir, '/_data/bangumis');
173183
const cachePath = path.join(bangumisPath, '/cache');
174-
const imagesPath = path.join(bangumisPath, '/images');
184+
const imagesPath = path.join(source_dir, '/images');
175185
const pathList = [bangumisPath, cachePath, imagesPath];
176186
for (const i of pathList) {
177187
if (!fs.existsSync(i)) {
@@ -198,6 +208,9 @@ module.exports.getBgmData = async (bgmtv_uid, download_image, source_dir) => {
198208
const info = await getBangumi(item, cachePath);
199209
if (info) {
200210
result.push(info);
211+
if (download_image) {
212+
getImage(info.image, imagesPath);
213+
}
201214
log.info(`Get bangumi 《${info.name_cn || info.name}》 (${info.id}) Success!`);
202215
}
203216
}

0 commit comments

Comments
 (0)