Skip to content

Commit

Permalink
添加分页功能优化选项(#143),详情请看配置选项中的pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
HCLonely committed Jul 11, 2022
1 parent 9c81082 commit 98e297a
Show file tree
Hide file tree
Showing 17 changed files with 319 additions and 48 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bangumi: # 追番设置
lazyload: true
loading:
showMyComment: false
pagination: false
metaColor:
color:
webp:
Expand Down Expand Up @@ -78,6 +79,7 @@ cinema: # 追剧设置
- **progress**: 获取番剧数据时是否显示进度条,默认`true`
- **extraOrder**: 手动添加的番剧/追剧数据是否优先显示,`1`为优先,其它为不优先
- **showMyComment**: 使用`bgm`源时显示自己的评价及评论,默认`false`
- **pagination**: 分页优化,只将第一页的数据渲染到`html`文件中,其余数据将通过异步请求加载,避免番剧过多时html文件过大导致页面加载缓慢,建议番剧较多时使用,默认`false`
- **extra_options**: 此配置会扩展到Hexo`page`变量中

## 使用
Expand Down
29 changes: 26 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,26 @@ hexo.extend.console.register('bangumi', 'Generate pages of bangumis for Hexo', o
if (this.config.bangumi.source === 'bgm') {
var _this$config$bangumi$;

getBgmData(this.config.bangumi.vmid, (_this$config$bangumi$ = this.config.bangumi.progress) !== null && _this$config$bangumi$ !== void 0 ? _this$config$bangumi$ : true, this.source_dir, this.config.bangumi.proxy);
getBgmData({
vmid: this.config.bangumi.vmid,
showProgress: (_this$config$bangumi$ = this.config.bangumi.progress) !== null && _this$config$bangumi$ !== void 0 ? _this$config$bangumi$ : true,
sourceDir: this.source_dir,
extraOrder: this.config.bangumi.extraOrder,
pagination: this.config.bangumi.pagination,
proxy: this.config.bangumi.proxy
});
} else {
var _this$config$bangumi$2;

getBiliData(this.config.bangumi.vmid, 'bangumi', (_this$config$bangumi$2 = this.config.bangumi.progress) !== null && _this$config$bangumi$2 !== void 0 ? _this$config$bangumi$2 : true, this.source_dir, this.config.bangumi.webp);
getBiliData({
vmid: this.config.bangumi.vmid,
type: 'bangumi',
showProgress: (_this$config$bangumi$2 = this.config.bangumi.progress) !== null && _this$config$bangumi$2 !== void 0 ? _this$config$bangumi$2 : true,
sourceDir: this.source_dir,
extraOrder: this.config.bangumi.extraOrder,
pagination: this.config.bangumi.pagination,
useWebp: this.config.bangumi.webp
});
}
} else {
log.info('Unknown command, please use "hexo bangumi -h" to see the available commands');
Expand Down Expand Up @@ -105,7 +120,15 @@ hexo.extend.console.register('cinema', 'Generate pages of bilibili cinemas for H
return;
}

getBiliData(this.config.cinema.vmid, 'cinema', (_this$config$cinema$p = this.config.cinema.progress) !== null && _this$config$cinema$p !== void 0 ? _this$config$cinema$p : true, this.source_dir, this.config.cinema.webp);
getBiliData({
vmid: this.config.cinema.vmid,
type: 'cinema',
showProgress: (_this$config$cinema$p = this.config.cinema.progress) !== null && _this$config$cinema$p !== void 0 ? _this$config$cinema$p : true,
sourceDir: this.source_dir,
extraOrder: this.config.cinema.extraOrder,
pagination: this.config.cinema.pagination,
useWebp: this.config.cinema.webp
});
} else {
log.info('Unknown command, please use "hexo cinema -h" to see the available commands');
}
Expand Down
4 changes: 3 additions & 1 deletion lib/bangumi-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var log = require('hexo-log')({

module.exports = /*#__PURE__*/function () {
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(locals) {
var _config$type, _config$type$lazyload, _config$type$source, _config$type$showMyCo, _config$type2;
var _config$type, _config$type$lazyload, _config$type$source, _config$type$showMyCo, _config$type$paginati, _config$type2;

var type,
config,
Expand Down Expand Up @@ -126,6 +126,8 @@ module.exports = /*#__PURE__*/function () {
lazyload: (_config$type$lazyload = config[type].lazyload) !== null && _config$type$lazyload !== void 0 ? _config$type$lazyload : true,
source: (_config$type$source = config[type].source) !== null && _config$type$source !== void 0 ? _config$type$source : 'bili',
showMyComment: (_config$type$showMyCo = config[type].showMyComment) !== null && _config$type$showMyCo !== void 0 ? _config$type$showMyCo : false,
pagination: (_config$type$paginati = config[type].pagination) !== null && _config$type$paginati !== void 0 ? _config$type$paginati : false,
ejsTemplate: fs.readFileSync(path.join(__dirname, "templates/".concat(config[type].source === 'bgm' ? 'bgm' : 'bili', "-template.ejs"))).toString().replace('class="bangumi-item"', 'class="bangumi-item bangumi-hide"'),
wantWatch: wantWatch,
watched: watched,
watching: watching,
Expand Down
73 changes: 61 additions & 12 deletions lib/get-bgm-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

/* eslint-disable no-underscore-dangle */
var fs = require('hexo-fs');

Expand Down Expand Up @@ -390,28 +396,30 @@ var getBangumiData = /*#__PURE__*/function () {
}();

module.exports.getBgmData = /*#__PURE__*/function () {
var _getBgmData = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(vmid, showProgress, sourceDir, proxy) {
var startTime, wantWatch, watching, watched, endTime, bangumis;
var _getBgmData = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(_ref6) {
var vmid, showProgress, sourceDir, extraOrder, pagination, proxy, startTime, wantWatch, watching, watched, endTime, bangumis, allBangumis, _JSON$parse, wantWatchExtra, watchingExtra, watchedExtra;

return _regenerator["default"].wrap(function _callee4$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
vmid = _ref6.vmid, showProgress = _ref6.showProgress, sourceDir = _ref6.sourceDir, extraOrder = _ref6.extraOrder, pagination = _ref6.pagination, proxy = _ref6.proxy;
log.info('Getting bangumis, please wait...');
startTime = new Date().getTime();
_context5.next = 4;
_context5.next = 5;
return getItemsId(vmid, 'wish', showProgress, sourceDir, proxy);

case 4:
case 5:
wantWatch = _context5.sent;
_context5.next = 7;
_context5.next = 8;
return getItemsId(vmid, 'do', showProgress, sourceDir, proxy);

case 7:
case 8:
watching = _context5.sent;
_context5.next = 10;
_context5.next = 11;
return getItemsId(vmid, 'collect', showProgress, sourceDir, proxy);

case 10:
case 11:
watched = _context5.sent;
endTime = new Date().getTime();
log.info("".concat(wantWatch.length + watching.length + watched.length, " bangumis have been loaded in ").concat(endTime - startTime, " ms"));
Expand All @@ -427,22 +435,63 @@ module.exports.getBgmData = /*#__PURE__*/function () {

fs.writeFile(path.join(sourceDir, '/_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 _data/bangumis.json');
console.error(err);
} else {
log.info('Bangumi bangumis data has been saved');
}
});

case 16:
if (pagination) {
allBangumis = _objectSpread({}, bangumis); // extra bangumis

if (fs.existsSync(path.join(sourceDir, '/_data/extra_bangumis.json'))) {
_JSON$parse = JSON.parse(fs.readFileSync(path.join(this.source_dir, "/_data/extra_".concat(type, "s.json")))), wantWatchExtra = _JSON$parse.wantWatchExtra, watchingExtra = _JSON$parse.watchingExtra, watchedExtra = _JSON$parse.watchedExtra;

if (wantWatchExtra) {
if (extraOrder === 1) {
allBangumis.wantWatch = [].concat((0, _toConsumableArray2["default"])(wantWatchExtra), (0, _toConsumableArray2["default"])(allBangumis.wantWatch));
} else {
allBangumis.wantWatch = [].concat((0, _toConsumableArray2["default"])(allBangumis.wantWatch), (0, _toConsumableArray2["default"])(wantWatchExtra));
}
}

if (watchingExtra) {
if (extraOrder === 1) {
allBangumis.watching = [].concat((0, _toConsumableArray2["default"])(watchingExtra), (0, _toConsumableArray2["default"])(allBangumis.watching));
} else {
allBangumis.watching = [].concat((0, _toConsumableArray2["default"])(allBangumis.watching), (0, _toConsumableArray2["default"])(watchingExtra));
}
}

if (watchedExtra) {
if (extraOrder === 1) {
allBangumis.watched = [].concat((0, _toConsumableArray2["default"])(watchedExtra), (0, _toConsumableArray2["default"])(allBangumis.watched));
} else {
allBangumis.watched = [].concat((0, _toConsumableArray2["default"])(allBangumis.watched), (0, _toConsumableArray2["default"])(watchedExtra));
}
}
}

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

case 18:
case "end":
return _context5.stop();
}
}
}, _callee4);
}, _callee4, this);
}));

function getBgmData(_x10, _x11, _x12, _x13) {
function getBgmData(_x10) {
return _getBgmData.apply(this, arguments);
}

Expand Down
72 changes: 57 additions & 15 deletions lib/get-bili-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _this = void 0;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
Expand Down Expand Up @@ -249,21 +257,14 @@ var processData = /*#__PURE__*/function () {
}();

module.exports.getBiliData = /*#__PURE__*/function () {
var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(vmid, type, showProgress, sourceDir) {
var useWebp,
typeNum,
startTime,
wantWatch,
watching,
watched,
endTime,
bangumis,
_args4 = arguments;
var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(_ref4) {
var vmid, type, showProgress, sourceDir, extraOrder, pagination, _ref4$useWebp, useWebp, typeNum, startTime, wantWatch, watching, watched, endTime, bangumis, allBangumis, _JSON$parse, wantWatchExtra, watchingExtra, watchedExtra;

return _regenerator["default"].wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
useWebp = _args4.length > 4 && _args4[4] !== undefined ? _args4[4] : true;
vmid = _ref4.vmid, type = _ref4.type, showProgress = _ref4.showProgress, sourceDir = _ref4.sourceDir, extraOrder = _ref4.extraOrder, pagination = _ref4.pagination, _ref4$useWebp = _ref4.useWebp, useWebp = _ref4$useWebp === void 0 ? true : _ref4$useWebp;
typeNum = type === 'cinema' ? 2 : 1;
log.info("Getting bilibili ".concat(type, ", please wait..."));
startTime = new Date().getTime();
Expand Down Expand Up @@ -296,22 +297,63 @@ module.exports.getBiliData = /*#__PURE__*/function () {

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

case 18:
if (pagination) {
allBangumis = _objectSpread({}, bangumis); // extra bangumis

if (fs.existsSync(path.join(sourceDir, "/_data/extra_".concat(type, "s.json")))) {
_JSON$parse = JSON.parse(fs.readFileSync(path.join(_this.source_dir, "/_data/extra_".concat(type, "s.json")))), wantWatchExtra = _JSON$parse.wantWatchExtra, watchingExtra = _JSON$parse.watchingExtra, watchedExtra = _JSON$parse.watchedExtra;

if (wantWatchExtra) {
if (extraOrder === 1) {
allBangumis.wantWatch = [].concat((0, _toConsumableArray2["default"])(wantWatchExtra), (0, _toConsumableArray2["default"])(allBangumis.wantWatch));
} else {
allBangumis.wantWatch = [].concat((0, _toConsumableArray2["default"])(allBangumis.wantWatch), (0, _toConsumableArray2["default"])(wantWatchExtra));
}
}

if (watchingExtra) {
if (extraOrder === 1) {
allBangumis.watching = [].concat((0, _toConsumableArray2["default"])(watchingExtra), (0, _toConsumableArray2["default"])(allBangumis.watching));
} else {
allBangumis.watching = [].concat((0, _toConsumableArray2["default"])(allBangumis.watching), (0, _toConsumableArray2["default"])(watchingExtra));
}
}

if (watchedExtra) {
if (extraOrder === 1) {
allBangumis.watched = [].concat((0, _toConsumableArray2["default"])(watchedExtra), (0, _toConsumableArray2["default"])(allBangumis.watched));
} else {
allBangumis.watched = [].concat((0, _toConsumableArray2["default"])(allBangumis.watched), (0, _toConsumableArray2["default"])(watchedExtra));
}
}
}

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

case 19:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));

return function (_x14, _x15, _x16, _x17) {
return _ref4.apply(this, arguments);
return function (_x14) {
return _ref5.apply(this, arguments);
};
}();
Loading

0 comments on commit 98e297a

Please sign in to comment.