UNPKG

pixiv-illust

Version:

A third-party Node.js library for Pixiv.

378 lines (302 loc) 13.2 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.setup = setup; require('babel-polyfill'); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _commander = require('commander'); var _commander2 = _interopRequireDefault(_commander); var _ = require('..'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } var pack = require('../../package.json'); // basic path settings. var BASE_PATH = void 0, cookieFile = void 0; function setup() { var root = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; BASE_PATH = root; cookieFile = _path2.default.join(root, '.cookies.json'); return _commander2.default.parse(process.argv); } _commander2.default.version(pack.version).option('-u --user <username>', 'username').option('-p --password <password>', 'password').option('-c --cookies <cookies>', 'cookie file').option('-d --dest [folder]', 'download destination folder', ''); var loginAction = function () { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() { return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(_commander2.default.user && _commander2.default.password)) { _context.next = 7; break; } _context.next = 3; return _.login.login(_commander2.default.user, _commander2.default.password); case 3: _context.next = 5; return _.login.dumps(cookieFile); case 5: _context.next = 9; break; case 7: _context.next = 9; return _.login.loads(_commander2.default.cookies || cookieFile); case 9: case 'end': return _context.stop(); } } }, _callee, undefined); })); return function loginAction() { return _ref.apply(this, arguments); }; }(); var getPath = function getPath(filePath) { var folder = _commander2.default.dest || ''; return folder.charAt(0) === '/' ? _path2.default.join(folder, filePath) : _path2.default.join(BASE_PATH, folder, filePath); }; var downloadIllusts = function () { var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(list) { var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '{{author}} - {{title}}{{suffix}}'; var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, info, illust; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _iteratorNormalCompletion = true; _didIteratorError = false; _iteratorError = undefined; _context2.prev = 3; _iterator = list[Symbol.iterator](); case 5: if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { _context2.next = 19; break; } info = _step.value; _context2.prev = 7; illust = new _.Illust(info.illust_id); _context2.next = 11; return illust.download(getPath(name)); case 11: _context2.next = 16; break; case 13: _context2.prev = 13; _context2.t0 = _context2['catch'](7); console.error('ID: ' + (info && info.id) + ' download error.'); case 16: _iteratorNormalCompletion = true; _context2.next = 5; break; case 19: _context2.next = 25; break; case 21: _context2.prev = 21; _context2.t1 = _context2['catch'](3); _didIteratorError = true; _iteratorError = _context2.t1; case 25: _context2.prev = 25; _context2.prev = 26; if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } case 28: _context2.prev = 28; if (!_didIteratorError) { _context2.next = 31; break; } throw _iteratorError; case 31: return _context2.finish(28); case 32: return _context2.finish(25); case 33: case 'end': return _context2.stop(); } } }, _callee2, undefined, [[3, 21, 25, 33], [7, 13], [26,, 28, 32]]); })); return function downloadIllusts(_x2, _x3) { return _ref2.apply(this, arguments); }; }(); var withErrors = function withErrors(func) { return function () { var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3() { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.prev = 0; _context3.next = 3; return func.apply(null, args); case 3: console.log('Download successfully.'); _context3.next = 9; break; case 6: _context3.prev = 6; _context3.t0 = _context3['catch'](0); console.error(_context3.t0 && _context3.t0.stack); case 9: case 'end': return _context3.stop(); } } }, _callee3, undefined, [[0, 6]]); })); return function (_x5) { return _ref3.apply(this, arguments); }; }(); }; _commander2.default.command('illust <id>').action(withErrors(function () { var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4(id) { var illust; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return loginAction(); case 2: illust = new _.Illust(id); _context4.next = 5; return illust.download(getPath('{{author}} - {{title}}{{suffix}}')); case 5: case 'end': return _context4.stop(); } } }, _callee4, undefined); })); return function (_x6) { return _ref4.apply(this, arguments); }; }())); _commander2.default.command('bookmark').action(withErrors(_asyncToGenerator(regeneratorRuntime.mark(function _callee5() { var bookmark, firstPage, total, page, list; return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: _context5.next = 2; return loginAction(); case 2: bookmark = new _.Bookmark(); _context5.next = 5; return bookmark.get(); case 5: firstPage = _context5.sent; total = firstPage.total; _context5.next = 9; return downloadIllusts(firstPage.contents); case 9: page = 2; case 10: if (!(page < total)) { _context5.next = 19; break; } _context5.next = 13; return bookmark.getPageContent(page); case 13: list = _context5.sent; _context5.next = 16; return downloadIllusts(list); case 16: page++; _context5.next = 10; break; case 19: case 'end': return _context5.stop(); } } }, _callee5, undefined); })))); _commander2.default.command('author <id>').action(withErrors(function () { var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6(id) { var author, firstPage, total, page, list; return regeneratorRuntime.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: _context6.next = 2; return loginAction(); case 2: author = new _.Author(id); _context6.next = 5; return author.getIllusts(); case 5: firstPage = _context6.sent; total = firstPage.total; _context6.next = 9; return downloadIllusts(firstPage.contents); case 9: page = 2; case 10: if (!(page < total)) { _context6.next = 19; break; } _context6.next = 13; return author.getIllustsContent(page); case 13: list = _context6.sent; _context6.next = 16; return downloadIllusts(list); case 16: page++; _context6.next = 10; break; case 19: case 'end': return _context6.stop(); } } }, _callee6, undefined); })); return function (_x7) { return _ref6.apply(this, arguments); }; }())); _commander2.default.command('rank').option('-m --mode <mode>', 'rank mode', 'daily').option('-t --date <date>', 'date of rank list').option('-r --rank <rank>', 'top n rank', parseInt, 50).action(withErrors(function () { var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7(cmd) { var rank, contents; return regeneratorRuntime.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: _context7.next = 2; return loginAction(); case 2: rank = new _.Rank(cmd.mode, cmd.date); _context7.next = 5; return rank.getRank(cmd.rank); case 5: contents = _context7.sent; _context7.next = 8; return downloadIllusts(contents); case 8: case 'end': return _context7.stop(); } } }, _callee7, undefined); })); return function (_x8) { return _ref7.apply(this, arguments); }; }()));