Skip to content

Commit

Permalink
Add image level choice
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdjiji committed Aug 25, 2022
1 parent 79523f1 commit f973023
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bangumis:
color_summary: "#555" # 追番项简介的颜色
bgmtv_uid: mmdjiji # bgm.tv的uid
download_image: true # 下载图片并使用本地图片,否则使用bgm.tv提供的网络图源
image_level: c # 图片高清等级 (l, c, m, s, g)
lazyload: true # 是否开启懒加载
margin: 20px # 封面图的偏移量微调
```
Expand Down
6 changes: 3 additions & 3 deletions dist/templates/bangumi.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
</div>
<div>
<div id="bangumi-item1">
<% wantWatch.forEach(function(item){ %>
<% wantWatch.forEach(function(item) { %>
<%- include('bgm-template.ejs', { item, loading, color_meta }) %>
<% }); %>
<%- include('pagination.ejs', {__: __}) %>
</div>
<div id="bangumi-item2">
<% watching.forEach(function(item){ %>
<% watching.forEach(function(item) { %>
<%- include('bgm-template.ejs', { item, loading, color_meta }) %>
<% }); %>
<%- include('pagination.ejs', {__: __}) %>
</div>
<div id="bangumi-item3">
<% watched.forEach(function(item){ %>
<% watched.forEach(function(item) { %>
<%- include('bgm-template.ejs', { item, loading, color_meta }) %>
<% }); %>
<%- include('pagination.ejs', {__: __}) %>
Expand Down
2 changes: 1 addition & 1 deletion dist/templates/bgm-template.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% if(download_image) { %>
<img src="<%= lazyload ? ("/images/loading.gif") : `/images/${item.image}` %>" data-src="<%= `/images/${item.image}` %>" referrerPolicy="no-referrer" width="110" style="width:110px;margin:<%= margin %> auto;" />
<% } else { %>
<img src="<%= lazyload ? ("/images/loading.gif") : `https://lain.bgm.tv/pic/cover/c/${item.image}` %>" data-src="<%= `/images/${item.image}` %>" referrerPolicy="no-referrer" width="110" style="width:110px;margin:<%= margin %> auto;" />
<img src="<%= lazyload ? ("/images/loading.gif") : `https://lain.bgm.tv/pic/cover/${image_level}/${item.image}` %>" data-src="<%= `/images/${item.image}` %>" referrerPolicy="no-referrer" width="110" style="width:110px;margin:<%= margin %> auto;" />
<% } %>
</div>
<div class="bangumi-info">
Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ hexo.extend.generator.register('bangumis', function (locals) {
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 All @@ -53,13 +57,14 @@ hexo.extend.console.register('bangumis', 'Generate pages of bangumis for Hexo',
var _this$config$bangumis2 = this.config.bangumis,
enable = _this$config$bangumis2.enable,
bgmtv_uid = _this$config$bangumis2.bgmtv_uid,
download_image = _this$config$bangumis2.download_image;
download_image = _this$config$bangumis2.download_image,
image_level = _this$config$bangumis2.image_level;

if (!enable) {
return;
}

getBgmData(bgmtv_uid, download_image, this.source_dir);
getBgmData(bgmtv_uid, download_image, image_level, this.source_dir);
} else {
log.info('Unknown command, please use "hexo bangumis -h" to see the available commands');
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ hexo.extend.console.register('bangumis', 'Generate pages of bangumis for Hexo',
log.info('Please add config to _config.yml');
return;
}
const { enable, bgmtv_uid, download_image } = this.config.bangumis;
const { enable, bgmtv_uid, download_image, image_level } = this.config.bangumis;
if (!enable) {
return;
}
getBgmData(bgmtv_uid, download_image, this.source_dir);
getBgmData(bgmtv_uid, download_image, image_level, this.source_dir);
} else {
log.info('Unknown command, please use "hexo bangumis -h" to see the available commands');
}
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 @@ -42,6 +42,7 @@ module.exports = async function (locals) {
lazyload: config.bangumis.lazyload ?? true,
margin: config.bangumis.margin ?? '20px',
download_image: config.bangumis.download_image ?? false,
image_level: config.bangumis.image_level ?? 'c',
wantWatch,
watched,
watching,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/get-bgm-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ const getBangumi = async (bgm, cachePath) => {
log.info(`Get bangumi (${bangumi_id}) Failed, maybe invalid!`);
};

const getImage = (image_url, imagesPath) => {
const getImage = (image_url, imagesPath, image_level) => {
if (image_url && !fs.existsSync(`${imagesPath}/${image_url}`)) {
fetch(`https://lain.bgm.tv/pic/cover/c/${image_url}`, {
fetch(`https://lain.bgm.tv/pic/cover/${image_level}/${image_url}`, {
method: 'GET',
headers: { 'Content-Type': 'application/octet-stream' }
}).then((res) => res.buffer())
Expand All @@ -177,7 +177,7 @@ const getImage = (image_url, imagesPath) => {
}
};

module.exports.getBgmData = async (bgmtv_uid, download_image, source_dir) => {
module.exports.getBgmData = async (bgmtv_uid, download_image, image_level, source_dir) => {
// create folders if not exist
const bangumisPath = path.join(source_dir, '/_data/bangumis');
const cachePath = path.join(bangumisPath, '/cache');
Expand Down Expand Up @@ -209,7 +209,7 @@ module.exports.getBgmData = async (bgmtv_uid, download_image, source_dir) => {
if (info) {
result.push(info);
if (download_image) {
getImage(info.image, imagesPath);
getImage(info.image, imagesPath, image_level);
}
log.info(`Get bangumi 《${info.name_cn || info.name}》 (${info.id}) Success!`);
}
Expand Down

0 comments on commit f973023

Please sign in to comment.