diff --git a/README.md b/README.md index d7c2041..f93f1b9 100644 --- a/README.md +++ b/README.md @@ -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 文件夹下执行: @@ -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 # 是否开启懒加载 ``` ## 使用 @@ -47,7 +58,6 @@ $ hexo bangumis -d 登录 [bgm.tv](https://bgm.tv) 后打开控制台(快捷键 `Ctrl` + `Shift` + `J`),输入 `CHOBITS_UID` 后按回车,得到的数字就是 `uid` 啦~ - ## Lisense [Apache Licence 2.0](LICENSE) diff --git a/dist/templates/bgm-template.ejs b/dist/templates/bgm-template.ejs index b8dee03..37adda4 100644 --- a/dist/templates/bgm-template.ejs +++ b/dist/templates/bgm-template.ejs @@ -1,6 +1,10 @@
- " <%- 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) { %> + " <%- lazyload ? ` data-src="/images/${item.image}"` : '' %> referrerPolicy="no-referrer" width="110" style="width:110px;margin:20px auto;" /> + <% } else { %> + " <%- lazyload ? ` data-src="https://lain.bgm.tv/pic/cover/c/${item.image}"` : '' %> referrerPolicy="no-referrer" width="110" style="width:110px;margin:20px auto;" /> + <% } %>
diff --git a/img/preview.png b/img/preview.png new file mode 100644 index 0000000..5325123 Binary files /dev/null and b/img/preview.png differ diff --git a/src/index.js b/src/index.js index 81cf025..ec7eb73 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { diff --git a/src/lib/bangumi-generator.js b/src/lib/bangumi-generator.js index 50dd98b..fbb7816 100644 --- a/src/lib/bangumi-generator.js +++ b/src/lib/bangumi-generator.js @@ -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, diff --git a/src/lib/get-bgm-data.js b/src/lib/get-bgm-data.js index c428ae3..b7c6025 100644 --- a/src/lib/get-bgm-data.js +++ b/src/lib/get-bgm-data.js @@ -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}`); @@ -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)) { @@ -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!`); } }