Skip to content

Commit

Permalink
获取番剧数据时添加进度条
Browse files Browse the repository at this point in the history
  • Loading branch information
HCLonely committed Aug 4, 2020
1 parent c7fae19 commit 8e79ae5
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 313 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## 介绍

**为Hexo添加哔哩哔哩番剧页面,参考了[hexo-douban](https://github.com/mythsman/hexo-douban)的部分代码**.
**为 Hexo 添加哔哩哔哩番剧页面,参考了[hexo-douban](https://github.com/mythsman/hexo-douban)的部分代码**.

[Demo](https://demo.hclonely.com/bangumis/)

Expand Down Expand Up @@ -38,19 +38,20 @@ bangumi:
- **path**: 番剧页面路径,默认`bangumis/index.html`
- **vmid**: 哔哩哔哩番剧页面的 `vmid(uid)`,[如何获取?](#获取uid)
- **title**: 该页面的标题
- **quote**: 写在页面开头的一段话,支持html语法
- **quote**: 写在页面开头的一段话,支持 html 语法,可留空。
- **show**: 初始显示页面:`0: 想看`, `1: 在看`, `2: 看过`,默认为`1`
- **loading**: 图片加载完成前的loading图片
- **metaColor**: meta部分(简介上方)字体颜色
- **loading**: 图片加载完成前的 loading 图片
- **metaColor**: meta 部分(简介上方)字体颜色
- **color**: 简介字体颜色
- **webp**: 番剧封面使用`webp`格式(此格式在`safari`浏览器下不显示,但是图片大小可以缩小100倍左右), 默认`true`
- **webp**: 番剧封面使用`webp`格式(此格式在`safari`浏览器下不显示,但是图片大小可以缩小 100 倍左右), 默认`true`
- **progress**: 获取番剧数据时是否显示进度条,默认`true`

## 使用

1. 在`hexo generate`或`hexo deploy`之前使用`hexo bangumi -u`命令更新番剧数据!
2. 删除数据命令:`hexo bangumi -d`

## 获取uid
## 获取 uid

登录哔哩哔哩后前往[https://space.bilibili.com/](https://space.bilibili.com/)页面,网址最后的一串数字就是 `uid`

Expand Down
96 changes: 53 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ var fs = require('hexo-fs');

var path = require('path');

var axios = require("axios");
var axios = require('axios');

var log = require('hexo-log')({
debug: false,
silent: false
});

var ProgressBar = require('progress');

if (typeof URL !== 'function') var _require = require('url'),
URL = _require.URL;
var options = {
Expand All @@ -48,15 +50,15 @@ hexo.extend.generator.register('bangumis', function (locals) {
});
hexo.extend.console.register('bangumi', 'Generate pages of bilibili bangumis for Hexo', options, function (args) {
if (args.d) {
if (fs.existsSync(path.join(__dirname, "/data/"))) {
fs.rmdirSync(path.join(__dirname, "/data/"));
if (fs.existsSync(path.join(__dirname, '/data/'))) {
fs.rmdirSync(path.join(__dirname, '/data/'));
log.info('Bangumis data has been deleted');
}
} else if (args.u) {
var _this$config2;

if (!(this === null || this === void 0 ? void 0 : (_this$config2 = this.config) === null || _this$config2 === void 0 ? void 0 : _this$config2.bangumi)) {
log.info("Please add config to _config.yml");
log.info('Please add config to _config.yml');
return;
}

Expand All @@ -65,13 +67,13 @@ hexo.extend.console.register('bangumi', 'Generate pages of bilibili bangumis for
}

if (!this.config.bangumi.vmid) {
log.info("Please add vmid to _config.yml");
log.info('Please add vmid to _config.yml');
return;
}

saveBangumiData(this.config.bangumi.vmid, this.config.bangumi.webp);
saveBangumiData(this.config.bangumi.vmid, this.config.bangumi.webp, this.config.bangumi.progress);
} else {
log.info("Unknown command, please use \"hexo bangumi -h\" to see the available commands");
log.info('Unknown command, please use "hexo bangumi -h" to see the available commands');
}
});

Expand All @@ -94,7 +96,7 @@ function _getBangumiPage() {
case 2:
response = _context.sent;

if (!((response === null || response === void 0 ? void 0 : (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.code) === 0 && (response === null || response === void 0 ? void 0 : (_response$data2 = response.data) === null || _response$data2 === void 0 ? void 0 : _response$data2.message) === "0" && (response === null || response === void 0 ? void 0 : (_response$data3 = response.data) === null || _response$data3 === void 0 ? void 0 : _response$data3.data) && typeof (response === null || response === void 0 ? void 0 : (_response$data4 = response.data) === null || _response$data4 === void 0 ? void 0 : (_response$data4$data = _response$data4.data) === null || _response$data4$data === void 0 ? void 0 : _response$data4$data.total) !== "undefined")) {
if (!((response === null || response === void 0 ? void 0 : (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.code) === 0 && (response === null || response === void 0 ? void 0 : (_response$data2 = response.data) === null || _response$data2 === void 0 ? void 0 : _response$data2.message) === '0' && (response === null || response === void 0 ? void 0 : (_response$data3 = response.data) === null || _response$data3 === void 0 ? void 0 : _response$data3.data) && typeof (response === null || response === void 0 ? void 0 : (_response$data4 = response.data) === null || _response$data4 === void 0 ? void 0 : (_response$data4$data = _response$data4.data) === null || _response$data4$data === void 0 ? void 0 : _response$data4$data.total) !== 'undefined')) {
_context.next = 7;
break;
}
Expand All @@ -105,7 +107,7 @@ function _getBangumiPage() {
});

case 7:
if (!(response && response.data && response.data.message !== "0")) {
if (!(response && response.data && response.data.message !== '0')) {
_context.next = 11;
break;
}
Expand Down Expand Up @@ -195,7 +197,7 @@ function _getBangumi() {
view: count(bangumi === null || bangumi === void 0 ? void 0 : (_bangumi$stat2 = bangumi.stat) === null || _bangumi$stat2 === void 0 ? void 0 : _bangumi$stat2.view),
danmaku: count(bangumi === null || bangumi === void 0 ? void 0 : (_bangumi$stat3 = bangumi.stat) === null || _bangumi$stat3 === void 0 ? void 0 : _bangumi$stat3.danmaku),
coin: count(bangumi.stat.coin),
score: (bangumi === null || bangumi === void 0 ? void 0 : bangumi.rating) ? bangumi === null || bangumi === void 0 ? void 0 : (_bangumi$rating = bangumi.rating) === null || _bangumi$rating === void 0 ? void 0 : _bangumi$rating.score : "暂无评分",
score: (bangumi === null || bangumi === void 0 ? void 0 : bangumi.rating) ? bangumi === null || bangumi === void 0 ? void 0 : (_bangumi$rating = bangumi.rating) === null || _bangumi$rating === void 0 ? void 0 : _bangumi$rating.score : '暂无评分',
des: bangumi === null || bangumi === void 0 ? void 0 : bangumi.evaluate
});
}
Expand All @@ -222,16 +224,16 @@ function count(e) {
}

function total(e) {
return e ? e === -1 ? "\u672A\u5B8C\u7ED3" : "\u5168".concat(e, "\u8BDD") : '-';
return e ? e === -1 ? '未完结' : "\u5168".concat(e, "\u8BDD") : '-';
}

function biliBangumi(_x7, _x8, _x9) {
function biliBangumi(_x7, _x8, _x9, _x10) {
return _biliBangumi.apply(this, arguments);
}

function _biliBangumi() {
_biliBangumi = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(vmid, status, webp) {
var page, list, i, data;
_biliBangumi = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(vmid, status, webp, progress) {
var page, list, bar, i, data;
return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
Expand All @@ -243,39 +245,45 @@ function _biliBangumi() {
page = _context3.sent;

if (!(page === null || page === void 0 ? void 0 : page.success)) {
_context3.next = 17;
_context3.next = 20;
break;
}

list = [];
bar = null;
if (progress) bar = new ProgressBar("\u6B63\u5728\u83B7\u53D6 ".concat(status === 1 ? '[想看]' : status === 2 ? '[在看]' : '[已看]', " \u756A\u5267 [:bar] :percent :elapseds"), {
total: page.data - 1,
complete: '█'
});
i = 1;

case 6:
case 8:
if (!(i < page.data)) {
_context3.next = 14;
_context3.next = 17;
break;
}

_context3.next = 9;
if (progress) bar.tick();
_context3.next = 12;
return getBangumi(vmid, status, webp, i);

case 9:
case 12:
data = _context3.sent;
list.push.apply(list, (0, _toConsumableArray2["default"])(data));

case 11:
case 14:
i++;
_context3.next = 6;
_context3.next = 8;
break;

case 14:
case 17:
return _context3.abrupt("return", list);

case 17:
console.log("Get bangumi data error:", page === null || page === void 0 ? void 0 : page.data);
case 20:
console.log('Get bangumi data error:', page === null || page === void 0 ? void 0 : page.data);
return _context3.abrupt("return", []);

case 19:
case 22:
case "end":
return _context3.stop();
}
Expand All @@ -285,13 +293,14 @@ function _biliBangumi() {
return _biliBangumi.apply(this, arguments);
}

function saveBangumiData(_x10) {
function saveBangumiData(_x11) {
return _saveBangumiData.apply(this, arguments);
}

function _saveBangumiData() {
_saveBangumiData = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(vmid) {
var webp,
progress,
startTime,
wantWatch,
watching,
Expand All @@ -304,45 +313,46 @@ function _saveBangumiData() {
switch (_context4.prev = _context4.next) {
case 0:
webp = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : true;
log.info("Getting bilibili bangumis, please wait...");
progress = _args4.length > 2 ? _args4[2] : undefined;
log.info('Getting bilibili bangumis, please wait...');
startTime = new Date().getTime();
_context4.next = 5;
return biliBangumi(vmid, 1, webp);
_context4.next = 6;
return biliBangumi(vmid, 1, webp, progress);

case 5:
case 6:
wantWatch = _context4.sent;
_context4.next = 8;
return biliBangumi(vmid, 2, webp);
_context4.next = 9;
return biliBangumi(vmid, 2, webp, progress);

case 8:
case 9:
watching = _context4.sent;
_context4.next = 11;
return biliBangumi(vmid, 3, webp);
_context4.next = 12;
return biliBangumi(vmid, 3, webp, progress);

case 11:
case 12:
watched = _context4.sent;
endTime = new Date().getTime();
log.info(wantWatch.length + watching.length + watched.length + ' bangumis have been loaded in ' + (endTime - startTime) + " ms");
log.info(wantWatch.length + watching.length + watched.length + ' bangumis have been loaded in ' + (endTime - startTime) + ' ms');
bangumis = {
wantWatch: wantWatch,
watching: watching,
watched: watched
};

if (!fs.existsSync(path.join(__dirname, "/data/"))) {
fs.mkdirsSync(path.join(__dirname, "/data/"));
if (!fs.existsSync(path.join(__dirname, '/data/'))) {
fs.mkdirsSync(path.join(__dirname, '/data/'));
}

fs.writeFile(path.join(__dirname, "/data/bangumis.json"), JSON.stringify(bangumis), function (err) {
fs.writeFile(path.join(__dirname, '/data/bangumis.json'), JSON.stringify(bangumis), function (err) {
if (err) {
log.info("Failed to write data to bangumis.json");
log.info('Failed to write data to bangumis.json');
console.error(err);
} else {
log.info("Bilibili bangumis data has been saved");
log.info('Bilibili bangumis data has been saved');
}
});

case 17:
case 18:
case "end":
return _context4.stop();
}
Expand Down
28 changes: 15 additions & 13 deletions lib/bangumi-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ module.exports = /*#__PURE__*/function () {
root = root.slice(0, root.length - 1);
}

wantWatch = [], watching = [], watched = [];
wantWatch = [];
watching = [];
watched = [];

if (!fs.existsSync(path.resolve(__dirname, '../data/bangumis.json'))) {
log.info("Can't find bilibili bangumi data, please use 'hexo bangumi -u' command to get data");
log.info('Can\'t find bilibili bangumi data, please use \'hexo bangumi -u\' command to get data');
} else {
_JSON$parse = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../data/bangumis.json')));
wantWatch = _JSON$parse.wantWatch;
Expand All @@ -59,16 +61,16 @@ module.exports = /*#__PURE__*/function () {

__ = i18n.__(config.language);
contents = ejs.renderFile(path.join(__dirname, 'templates/bangumi.ejs'), {
'quote': config.bangumi.quote,
'show': config.bangumi.show || 1,
'loading': config.bangumi.loading,
'metaColor': config.bangumi.metaColor ? "style=\"color:".concat(config.bangumi.metaColor, "\"") : "",
'color': config.bangumi.color ? "style=\"color:".concat(config.bangumi.color, "\"") : "",
'wantWatch': wantWatch,
'watched': watched,
'watching': watching,
'__': __,
'root': root
quote: config.bangumi.quote,
show: config.bangumi.show || 1,
loading: config.bangumi.loading,
metaColor: config.bangumi.metaColor ? "style=\"color:".concat(config.bangumi.metaColor, "\"") : '',
color: config.bangumi.color ? "style=\"color:".concat(config.bangumi.color, "\"") : '',
wantWatch: wantWatch,
watched: watched,
watching: watching,
__: __,
root: root
}, function (err, result) {
if (err) console.log(err);
return result;
Expand All @@ -82,7 +84,7 @@ module.exports = /*#__PURE__*/function () {
layout: ['page', 'post']
});

case 10:
case 12:
case "end":
return _context.stop();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/templates/pagination.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8e79ae5

Please sign in to comment.