Skip to content

Commit

Permalink
Add download_images
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdjiji committed Aug 22, 2022
1 parent 31324ef commit 34cf9af
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
# hexo-bangumis

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

## 介绍

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

## 特色功能

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

## 预览

![](img/preview.png)

## 安装

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

``` yaml
bangumis:
enable: true # 是否启用
enable: true # 是否启用
path: bangumis/index.html # 生成追番页面的路径
show: 1 # 想看,在看,看完
title: '追番列表' # 标题
show: 1 # 想看,在看,看完
title: '追番列表' # 标题
quote: '生命不息,追番不止' # 格言
color_meta: "#555" # 追番项元数据的颜色
color_summary: "#555" # 追番项简介的颜色
bgmtv_uid: mmdjiji # bgm.tv的uid
download_image: true # 下载图片并使用本地图片,否则使用bgm.tv提供的网络图源

color_meta: "#555" # 追番项元数据的颜色
color_summary: "#555" # 追番项简介的颜色
bgmtv_uid: mmdjiji # bgm.tv的uid
download_image: true # 下载图片并使用本地图片,否则使用bgm.tv提供的网络图源
lazyload: true # 是否开启懒加载
```
## 使用
Expand All @@ -47,7 +58,6 @@ $ hexo bangumis -d

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


## Lisense

[Apache Licence 2.0](LICENSE)
6 changes: 5 additions & 1 deletion dist/templates/bgm-template.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<div class="bangumi-item">
<div class="bangumi-picture">
<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;" />
<% if(download_image) { %>
<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;" />
<% } else { %>
<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;" />
<% } %>
</div>
<div class="bangumi-info">
<div class="bangumi-title">
Expand Down
Binary file added img/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ hexo.extend.generator.register('bangumis', function (locals) {
if (!this?.config?.bangumis?.enable) {
return;
}
if (!fs.existsSync(path.join(this.source_dir, '/images/loading.gif'))) {
fs.copyFile(path.join(__dirname, 'img/loading.gif'), path.join(this.source_dir, '/images/loading.gif'));
}
return require('./dist/bangumi-generator').call(this, locals);
});
hexo.extend.console.register('bangumis', 'Generate pages of bangumis for Hexo', options, function (args) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/bangumi-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = async function (locals) {
color_meta: config.bangumis.color_meta ? `style="color:${config.bangumis.color_meta}"` : '',
color: config.bangumis.color ? `style="color:${config.bangumis.color}"` : '',
lazyload: config.bangumis.lazyload ?? true,
download_image: config.bangumis.download_image ?? false,
wantWatch,
watched,
watching,
Expand Down
21 changes: 17 additions & 4 deletions src/lib/get-bgm-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const getBangumi = async (bgm, cachePath) => {
if (await fs.exists(savedPath)) {
try {
const read = await JSON.parse(await fs.readFile(savedPath));
if(read.id === bangumi_id) {
if (read.id === bangumi_id) {
return read;
}
throw new Error(`Id not match when trying to load id=${bangumi_id}`);
Expand Down Expand Up @@ -163,15 +163,25 @@ const getBangumi = async (bgm, cachePath) => {
log.info(`Get bangumi (${bangumi_id}) Failed, maybe invalid!`);
};

const getImage = async (image_url) => {

const getImage = (image_url, imagesPath) => {
if (image_url && !fs.existsSync(`${imagesPath}/${image_url}`)) {
fetch(`https://lain.bgm.tv/pic/cover/c/${image_url}`, {
method: 'GET',
headers: { 'Content-Type': 'application/octet-stream' }
}).then((res) => res.buffer())
.then((image) => {
fs.writeFile(`${imagesPath}/${image_url}`, image, 'binary', (err) => {
console.error(err);
});
});
}
};

module.exports.getBgmData = async (bgmtv_uid, download_image, source_dir) => {
// create folders if not exist
const bangumisPath = path.join(source_dir, '/_data/bangumis');
const cachePath = path.join(bangumisPath, '/cache');
const imagesPath = path.join(bangumisPath, '/images');
const imagesPath = path.join(source_dir, '/images');
const pathList = [bangumisPath, cachePath, imagesPath];
for (const i of pathList) {
if (!fs.existsSync(i)) {
Expand All @@ -198,6 +208,9 @@ module.exports.getBgmData = async (bgmtv_uid, download_image, source_dir) => {
const info = await getBangumi(item, cachePath);
if (info) {
result.push(info);
if (download_image) {
getImage(info.image, imagesPath);
}
log.info(`Get bangumi 《${info.name_cn || info.name}》 (${info.id}) Success!`);
}
}
Expand Down

0 comments on commit 34cf9af

Please sign in to comment.