Skip to content

Commit

Permalink
番剧数据保存在source/_data目录
Browse files Browse the repository at this point in the history
  • Loading branch information
HCLonely committed Aug 8, 2020
1 parent 57ac795 commit 5b45c90
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
30 changes: 16 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"))

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

function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { 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 it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, 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 _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { 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 = o[Symbol.iterator](); }, 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 @@ -50,8 +50,8 @@ 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(this.source_dir, '/_data/'))) {
fs.rmdirSync(path.join(this.source_dir, '/_data/'));
log.info('Bangumis data has been deleted');
}
} else if (args.u) {
Expand All @@ -71,7 +71,7 @@ hexo.extend.console.register('bangumi', 'Generate pages of bilibili bangumis for
return;
}

saveBangumiData(this.config.bangumi.vmid, this.config.bangumi.webp, (_this$config$bangumi$ = this.config.bangumi.progress) !== null && _this$config$bangumi$ !== void 0 ? _this$config$bangumi$ : true);
saveBangumiData(this.config.bangumi.vmid, this.config.bangumi.webp, (_this$config$bangumi$ = this.config.bangumi.progress) !== null && _this$config$bangumi$ !== void 0 ? _this$config$bangumi$ : true, this.source_dir);
} else {
log.info('Unknown command, please use "hexo bangumi -h" to see the available commands');
}
Expand Down Expand Up @@ -301,6 +301,7 @@ function _saveBangumiData() {
_saveBangumiData = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(vmid) {
var webp,
progress,
sourceDir,
startTime,
wantWatch,
watching,
Expand All @@ -314,22 +315,23 @@ function _saveBangumiData() {
case 0:
webp = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : true;
progress = _args4.length > 2 ? _args4[2] : undefined;
sourceDir = _args4.length > 3 ? _args4[3] : undefined;
log.info('Getting bilibili bangumis, please wait...');
startTime = new Date().getTime();
_context4.next = 6;
_context4.next = 7;
return biliBangumi(vmid, 1, webp, progress);

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

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

case 12:
case 13:
watched = _context4.sent;
endTime = new Date().getTime();
log.info(wantWatch.length + watching.length + watched.length + ' bangumis have been loaded in ' + (endTime - startTime) + ' ms');
Expand All @@ -339,11 +341,11 @@ function _saveBangumiData() {
watched: watched
};

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

fs.writeFile(path.join(__dirname, '/data/bangumis.json'), JSON.stringify(bangumis), function (err) {
fs.writeFile(path.join(sourceDir, '/_data/bangumis.json'), JSON.stringify(bangumis), function (err) {
if (err) {
log.info('Failed to write data to bangumis.json');
console.error(err);
Expand All @@ -352,7 +354,7 @@ function _saveBangumiData() {
}
});

case 18:
case 19:
case "end":
return _context4.stop();
}
Expand Down
7 changes: 4 additions & 3 deletions lib/bangumi-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ module.exports = /*#__PURE__*/function () {
wantWatch = [];
watching = [];
watched = [];
console.log(path.join(this.source_dir, '/_data/bangumis.json'));

if (!fs.existsSync(path.resolve(__dirname, '../data/bangumis.json'))) {
if (!fs.existsSync(path.join(this.source_dir, '/_data/bangumis.json'))) {
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')));
_JSON$parse = JSON.parse(fs.readFileSync(path.join(this.source_dir, '/_data/bangumis.json')));
wantWatch = _JSON$parse.wantWatch;
watching = _JSON$parse.watching;
watched = _JSON$parse.watched;
Expand Down Expand Up @@ -84,7 +85,7 @@ module.exports = /*#__PURE__*/function () {
layout: ['page', 'post']
});

case 12:
case 13:
case "end":
return _context.stop();
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-bilibili-bangumi",
"version": "1.4.1",
"version": "1.5.0",
"description": "hexo bilibili番剧页",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -40,7 +40,7 @@
"clean-css-cli": "^4.3.0",
"standard": "^14.3.4"
},
"standard":{
"standard": {
"ignore": [
"index.js",
"lib/*"
Expand Down
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ 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(this.source_dir, '/_data/'))) {
fs.rmdirSync(path.join(this.source_dir, '/_data/'))
log.info('Bangumis data has been deleted')
}
} else if (args.u) {
Expand All @@ -41,7 +41,7 @@ hexo.extend.console.register('bangumi', 'Generate pages of bilibili bangumis for
log.info('Please add vmid to _config.yml')
return
}
saveBangumiData(this.config.bangumi.vmid, this.config.bangumi.webp, this.config.bangumi.progress ?? true)
saveBangumiData(this.config.bangumi.vmid, this.config.bangumi.webp, this.config.bangumi.progress ?? true, this.source_dir)
} else {
log.info('Unknown command, please use "hexo bangumi -h" to see the available commands')
}
Expand Down Expand Up @@ -114,7 +114,7 @@ async function biliBangumi (vmid, status, webp, progress) {
return []
}
}
async function saveBangumiData (vmid, webp = true, progress) {
async function saveBangumiData(vmid, webp = true, progress, sourceDir) {
log.info('Getting bilibili bangumis, please wait...')
const startTime = new Date().getTime()
const wantWatch = await biliBangumi(vmid, 1, webp, progress)
Expand All @@ -123,10 +123,10 @@ async function saveBangumiData (vmid, webp = true, progress) {
const endTime = new Date().getTime()
log.info(wantWatch.length + watching.length + watched.length + ' bangumis have been loaded in ' + (endTime - startTime) + ' ms')
const bangumis = { wantWatch, watching, watched }
if (!fs.existsSync(path.join(__dirname, '/data/'))) {
fs.mkdirsSync(path.join(__dirname, '/data/'))
if (!fs.existsSync(path.join(sourceDir, '/_data/'))) {
fs.mkdirsSync(path.join(sourceDir, '/_data/'))
}
fs.writeFile(path.join(__dirname, '/data/bangumis.json'), JSON.stringify(bangumis), err => {
fs.writeFile(path.join(sourceDir, '/_data/bangumis.json'), JSON.stringify(bangumis), err => {
if (err) {
log.info('Failed to write data to bangumis.json')
console.error(err)
Expand Down
5 changes: 3 additions & 2 deletions src/lib/bangumi-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ module.exports = async function (locals) {
root = root.slice(0, root.length - 1)
}
let wantWatch = []; let watching = []; let watched = []
if (!fs.existsSync(path.resolve(__dirname, '../data/bangumis.json'))) {
console.log(path.join(this.source_dir, '/_data/bangumis.json'))
if (!fs.existsSync(path.join(this.source_dir, '/_data/bangumis.json'))) {
log.info('Can\'t find bilibili bangumi data, please use \'hexo bangumi -u\' command to get data')
} else {
({ wantWatch, watching, watched } = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../data/bangumis.json'))))
({ wantWatch, watching, watched } = JSON.parse(fs.readFileSync(path.join(this.source_dir, '/_data/bangumis.json'))))
log.info(wantWatch.length + watching.length + watched.length + ' bangumis have been loaded')
}

Expand Down

0 comments on commit 5b45c90

Please sign in to comment.