Skip to content

Commit

Permalink
添加代理设置选项;添加数据缓存功能;修复已知BUG(125)
Browse files Browse the repository at this point in the history
  • Loading branch information
HCLonely committed Jan 31, 2022
1 parent 1ec5c3e commit b34de48
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 114 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ bangumi: # 追番设置
color:
webp:
progress:
proxy:
host: '代理host'
port: '代理端口'
extra_options:
key: value
cinema: # 追剧设置
Expand All @@ -56,6 +59,7 @@ cinema: # 追剧设置
- **enable**: 是否启用
- **source**: 数据源,仅支持追番,追剧仅支持哔哩哔哩源。`bili`: [哔哩哔哩源](https://www.bilibili.com/), `bgm`: [Bangumi源](https://bangumi.tv/)
- **proxy**: 代理设置,仅在使用支持`bgm`源追番时生效。默认`false`
- **path**: 页面路径,默认`bangumis/index.html`, `cinemas/index.html`
- **vmid**: 哔哩哔哩的 `vmid(uid)`[如何获取?](#获取-bilibili-uid)或Bangumi的用户`id`[如何获取?](#获取-bangumi-id)
- **title**: 该页面的标题
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ 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);
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);
} else {
var _this$config$bangumi$2;

Expand Down
4 changes: 2 additions & 2 deletions lib/bangumi-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/de

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); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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; }

var ejs = require('ejs');

Expand Down
209 changes: 140 additions & 69 deletions lib/get-bgm-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers

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

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

var path = require('path');
Expand All @@ -23,6 +24,8 @@ var ProgressBar = require('progress');

var cheerio = require('cheerio');

var tunnel = require('tunnel');

var bangumiData = require('bangumi-data');

var getItemsId = /*#__PURE__*/function () {
Expand All @@ -41,7 +44,7 @@ var getItemsId = /*#__PURE__*/function () {
response = _context2.sent;

if (!(response !== null && response !== void 0 && response.data)) {
_context2.next = 18;
_context2.next = 19;
break;
}

Expand All @@ -55,24 +58,24 @@ var getItemsId = /*#__PURE__*/function () {
};
}).get();

if (!(pageNum < 2)) {
_context2.next = 10;
break;
}

return _context2.abrupt("return", items);

case 10:
if (showProgress) {
// eslint-disable-next-line no-nested-ternary
bar = new ProgressBar("\u6B63\u5728\u83B7\u53D6 ".concat(status === 'wish' ? '[想看]' : status === 'do' ? '[在看]' : '[已看]', " \u756A\u5267 [:bar] :percent :elapseds"), {
total: pageNum,
total: pageNum < 2 ? 1 : pageNum,
complete: '█'
});
bar.tick();
} // eslint-disable-next-line no-plusplus
}

if (!(pageNum < 2)) {
_context2.next = 12;
break;
}

log.info('正在获取番剧详细数据,请耐心等待...');
return _context2.abrupt("return", items);

case 12:
_loop = /*#__PURE__*/_regenerator["default"].mark(function _loop(i) {
var response, $;
return _regenerator["default"].wrap(function _loop$(_context) {
Expand Down Expand Up @@ -105,24 +108,24 @@ var getItemsId = /*#__PURE__*/function () {
});
i = 2;

case 13:
case 14:
if (!(i <= pageNum)) {
_context2.next = 18;
_context2.next = 19;
break;
}

return _context2.delegateYield(_loop(i), "t0", 15);
return _context2.delegateYield(_loop(i), "t0", 16);

case 15:
case 16:
i++;
_context2.next = 13;
_context2.next = 14;
break;

case 18:
case 19:
log.info('正在获取番剧详细数据,耗时与追番数量成正比,请耐心等待...');
return _context2.abrupt("return", items);

case 20:
case 21:
case "end":
return _context2.stop();
}
Expand Down Expand Up @@ -152,31 +155,65 @@ var TYPE = {
};

var getBangumiData = /*#__PURE__*/function () {
var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(items) {
var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(items, sourceDir, proxy) {
return _regenerator["default"].wrap(function _callee2$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return axios.all(items.map(function (item) {
return axios.get("https://cdn.jsdelivr.net/gh/czy0729/Bangumi-Subject@master/data/".concat(parseInt(parseInt(item.id, 10) / 100, 10), "/").concat(item.id, ".json"), {
return Promise.all(items.map(function (item) {
var cachePath = path.join(sourceDir, '/_data/Bangumi-Subject-Cache');
var subjectPath = path.join(cachePath, "".concat(item.id, ".json"));

if (!fs.existsSync(cachePath)) {
fs.mkdirsSync(cachePath);
}

if (fs.existsSync(subjectPath)) {
return {
config: {
itemData: item
},
data: fs.readFileSync(subjectPath).toString(),
status: 200
};
}

var options = {
itemData: item,
responseType: 'json',
validateStatus: function validateStatus(status) {
return status >= 200 && status < 300 || status === 403; // default
return status >= 200 && status < 300 || status === 403;
}
};

if (proxy !== null && proxy !== void 0 && proxy.host && proxy !== null && proxy !== void 0 && proxy.port) {
options.httpsAgent = tunnel.httpsOverHttp({
proxy: proxy,
rejectUnauthorized: false
});
}

return axios.get("https://cdn.jsdelivr.net/gh/czy0729/Bangumi-Subject@master/data/".concat(parseInt(parseInt(item.id, 10) / 100, 10), "/").concat(item.id, ".json"), options).then(function (response) {
return response;
})["catch"](function (error) {
if (error.response) {
console.error('Error', error.response.status);
} else {
console.error('Error', error.stack);
}

return error;
});
}));

case 2:
return _context3.abrupt("return", _context3.sent.map(function (_ref3) {
var _data, _data2, _data3, _data4, _data5, _data5$rating, _data6, _data6$summary, _data7, _data7$collection, _data8, _data8$collection, _data9, _data9$collection, _data10, _data10$eps, _data11, _data11$eps;

var config = _ref3.config,
data = _ref3.data,
status = _ref3.status;

if (status === 403) {
if (status === 403 || !data) {
return {
id: config.itemData.id,
title: jp2cnName(config.itemData.name),
Expand All @@ -201,17 +238,57 @@ var getBangumiData = /*#__PURE__*/function () {
}
}

var _data = data,
id = _data.id,
name = _data.name,
type = _data.type,
image = _data.image,
_data$rating = _data.rating;
_data$rating = _data$rating === void 0 ? {} : _data$rating;
var score = _data$rating.score,
summary = _data.summary,
_data$collection = _data.collection;
_data$collection = _data$collection === void 0 ? {} : _data$collection;
var wish = _data$collection.wish,
doing = _data$collection.doing,
collect = _data$collection.collect,
eps = _data.eps,
epsLength = _data.epsLength;
var totalCount = epsLength || (eps === null || eps === void 0 ? void 0 : eps.length);
var subjectPath = path.join(sourceDir, '/_data/Bangumi-Subject-Cache', "".concat(config.itemData.id, ".json"));

if (!fs.existsSync(subjectPath)) {
if (id && name && type && image && score && summary && wish && doing && collect && (eps === null || eps === void 0 ? void 0 : eps.length) > 0) {
fs.writeFileSync(subjectPath, JSON.stringify({
id: id,
name: name,
type: type,
image: image,
rating: {
score: score
},
summary: summary,
collection: {
wish: wish,
doing: doing,
collect: collect
},
epsLength: eps.length
}));
}
}

return {
id: ((_data = data) === null || _data === void 0 ? void 0 : _data.id) || config.itemData.id,
title: jp2cnName(((_data2 = data) === null || _data2 === void 0 ? void 0 : _data2.name) || config.itemData.name),
type: TYPE[(_data3 = data) === null || _data3 === void 0 ? void 0 : _data3.type] || '未知',
cover: ((_data4 = data) === null || _data4 === void 0 ? void 0 : _data4.image) || config.itemData.cover,
score: ((_data5 = data) === null || _data5 === void 0 ? void 0 : (_data5$rating = _data5.rating) === null || _data5$rating === void 0 ? void 0 : _data5$rating.score) || '-',
des: ((_data6 = data) === null || _data6 === void 0 ? void 0 : (_data6$summary = _data6.summary) === null || _data6$summary === void 0 ? void 0 : _data6$summary.replace(/\r?\n/g, '').trim()) || '-',
wish: ((_data7 = data) === null || _data7 === void 0 ? void 0 : (_data7$collection = _data7.collection) === null || _data7$collection === void 0 ? void 0 : _data7$collection.wish) || '-',
doing: ((_data8 = data) === null || _data8 === void 0 ? void 0 : (_data8$collection = _data8.collection) === null || _data8$collection === void 0 ? void 0 : _data8$collection.doing) || '-',
collect: ((_data9 = data) === null || _data9 === void 0 ? void 0 : (_data9$collection = _data9.collection) === null || _data9$collection === void 0 ? void 0 : _data9$collection.collect) || '-',
totalCount: (_data10 = data) !== null && _data10 !== void 0 && (_data10$eps = _data10.eps) !== null && _data10$eps !== void 0 && _data10$eps.length ? "\u5168".concat((_data11 = data) === null || _data11 === void 0 ? void 0 : (_data11$eps = _data11.eps) === null || _data11$eps === void 0 ? void 0 : _data11$eps.length, "\u8BDD") : '-'
id: id || config.itemData.id,
title: jp2cnName(name || config.itemData.name),
type: TYPE[type] || '未知',
cover: image || config.itemData.cover,
score: score || '-',
des: (summary === null || summary === void 0 ? void 0 : summary.replace(/\r?\n/g, '').trim()) || '-',
wish: wish || '-',
doing: doing || '-',
collect: collect || '-',
totalCount: totalCount ? "\u5168".concat(totalCount, "\u8BDD") : '-'
};
}));

Expand All @@ -223,25 +300,13 @@ var getBangumiData = /*#__PURE__*/function () {
}, _callee2);
}));

return function getBangumiData(_x4) {
return function getBangumiData(_x4, _x5, _x6) {
return _ref2.apply(this, arguments);
};
}();
/*
(async () => {
console.log(await getBangumiData([{ id: '975' }]));
})();
function count(e) {
return e ? (e > 10000 && e < 100000000 ? `${(e / 10000).toFixed(1)} 万` : e > 100000000 ? `${(e / 100000000).toFixed(1)} 亿` : e) : '-';
}
function total(e, typeNum) {
return e ? (e === -1 ? '未完结' : `全${e}${typeNum === 1 ? '话' : '集'}`) : '-';
}
*/


module.exports.getBgmData = /*#__PURE__*/function () {
var _getBgmData = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(vmid, showProgress, sourceDir) {
var _getBgmData = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(vmid, showProgress, sourceDir, proxy) {
var startTime, wantWatch, watching, watched, endTime, bangumis;
return _regenerator["default"].wrap(function _callee3$(_context4) {
while (1) {
Expand All @@ -255,32 +320,38 @@ module.exports.getBgmData = /*#__PURE__*/function () {

case 5:
_context4.t1 = _context4.sent;
_context4.next = 8;
return (0, _context4.t0)(_context4.t1);
_context4.t2 = sourceDir;
_context4.t3 = proxy;
_context4.next = 10;
return (0, _context4.t0)(_context4.t1, _context4.t2, _context4.t3);

case 8:
case 10:
wantWatch = _context4.sent;
_context4.t2 = getBangumiData;
_context4.next = 12;
_context4.t4 = getBangumiData;
_context4.next = 14;
return getItemsId(vmid, 'do', showProgress);

case 12:
_context4.t3 = _context4.sent;
_context4.next = 15;
return (0, _context4.t2)(_context4.t3);
case 14:
_context4.t5 = _context4.sent;
_context4.t6 = sourceDir;
_context4.t7 = proxy;
_context4.next = 19;
return (0, _context4.t4)(_context4.t5, _context4.t6, _context4.t7);

case 15:
case 19:
watching = _context4.sent;
_context4.t4 = getBangumiData;
_context4.next = 19;
_context4.t8 = getBangumiData;
_context4.next = 23;
return getItemsId(vmid, 'collect', showProgress);

case 19:
_context4.t5 = _context4.sent;
_context4.next = 22;
return (0, _context4.t4)(_context4.t5);
case 23:
_context4.t9 = _context4.sent;
_context4.t10 = sourceDir;
_context4.t11 = proxy;
_context4.next = 28;
return (0, _context4.t8)(_context4.t9, _context4.t10, _context4.t11);

case 22:
case 28:
watched = _context4.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 @@ -299,19 +370,19 @@ module.exports.getBgmData = /*#__PURE__*/function () {
log.info('Failed to write data to bangumis.json');
console.error(err);
} else {
log.info('Bilibili bangumis data has been saved');
log.info('Bangumi bangumis data has been saved');
}
});

case 28:
case 34:
case "end":
return _context4.stop();
}
}
}, _callee3);
}));

function getBgmData(_x5, _x6, _x7) {
function getBgmData(_x7, _x8, _x9, _x10) {
return _getBgmData.apply(this, arguments);
}

Expand Down
Loading

0 comments on commit b34de48

Please sign in to comment.