UNPKG

pixiv-illust

Version:

A third-party Node.js library for Pixiv.

664 lines (545 loc) 27.6 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _dec, _dec2, _dec3, _dec4, _dec5, _desc, _value, _class; require('babel-polyfill'); var _login = require('./login'); var _login2 = _interopRequireDefault(_login); var _cheerio = require('cheerio'); var _cheerio2 = _interopRequireDefault(_cheerio); var _Bluebird = require('Bluebird'); var _Bluebird2 = _interopRequireDefault(_Bluebird); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _utility = require('./utility'); var _extend = require('extend'); var _extend2 = _interopRequireDefault(_extend); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new _Bluebird2.default(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 _Bluebird2.default.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } var request = _Bluebird2.default.promisifyAll(require('request')); var fs = _Bluebird2.default.promisifyAll(require('fs')); var PAGE = 'http://www.pixiv.net/member_illust.php'; var TYPE = { NORMAL: 0, UGOIRA: 1, MULTIPLE: 2 }; function validType() { for (var _len = arguments.length, types = Array(_len), _key = 0; _key < _len; _key++) { types[_key] = arguments[_key]; } var VALID_TYPES = Object.values(TYPE); types.forEach(function (type) { if (!VALID_TYPES.includes(type)) { throw new Error('Unvalid types.'); } }); return function (target, prop, descriptor) { var method = descriptor.value; descriptor.value = function () { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() { for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } var type; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return this._type(); case 2: type = _context.sent; if (types.includes(type)) { _context.next = 5; break; } throw new Error('Not allowed in this type of illust.'); case 5: return _context.abrupt('return', method.call(this, args)); case 6: case 'end': return _context.stop(); } } }, _callee, this); })); return function (_x) { return _ref.apply(this, arguments); }; }(); return descriptor; }; } var Illust = (_dec = validType(TYPE.NORMAL), _dec2 = validType(TYPE.MULTIPLE), _dec3 = validType(TYPE.MULTIPLE), _dec4 = validType(TYPE.MULTIPLE), _dec5 = validType(TYPE.UGOIRA), (_class = function () { function Illust(id) { _classCallCheck(this, Illust); this.id = id; } _createClass(Illust, [{ key: 'getPageUrl', value: function getPageUrl() { var mode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'medium'; return PAGE + '?mode=' + mode + '&illust_id=' + this.id; } }, { key: 'getContent', value: function () { var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2() { var response; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return request.getAsync({ url: this.url, headers: { 'Host': 'www.pixiv.net', 'Referer': 'http://www.pixiv.net/' }, jar: _login2.default.cookieJar }); case 2: response = _context2.sent; return _context2.abrupt('return', _cheerio2.default.load(response.body)); case 4: case 'end': return _context2.stop(); } } }, _callee2, this); })); function getContent() { return _ref2.apply(this, arguments); } return getContent; }() }, { key: 'getInfo', value: function () { var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3() { var $, $workInfo, $metas; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return this.getContent(); case 2: $ = _context3.sent; $workInfo = $('.work-info'); $metas = $workInfo.find('.meta li'); return _context3.abrupt('return', { id: this.id, title: $workInfo.find('.title').text(), author: $('.user').text().trim(), tools: $workInfo.find('.tools li').map(function (i, elem) { return $(elem).text().trim(); }).get(), page: this.url, date: $metas.eq(0).text() }); case 6: case 'end': return _context3.stop(); } } }, _callee3, this); })); function getInfo() { return _ref3.apply(this, arguments); } return getInfo; }() }, { key: '_type', value: function () { var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4() { var $; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return this.getContent(); case 2: $ = _context4.sent; if (!$('.works_display .multiple').length) { _context4.next = 7; break; } return _context4.abrupt('return', TYPE.MULTIPLE); case 7: if (!$('._ugoku-illust-player-container').length) { _context4.next = 11; break; } return _context4.abrupt('return', TYPE.UGOIRA); case 11: return _context4.abrupt('return', TYPE.NORMAL); case 12: case 'end': return _context4.stop(); } } }, _callee4, this); })); function _type() { return _ref4.apply(this, arguments); } return _type; }() }, { key: '_getImageUrl', value: function () { var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5() { var $; return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: _context5.next = 2; return this.getContent(); case 2: $ = _context5.sent; return _context5.abrupt('return', $('.original-image').attr('data-src')); case 4: case 'end': return _context5.stop(); } } }, _callee5, this); })); function _getImageUrl() { return _ref5.apply(this, arguments); } return _getImageUrl; }() }, { key: '_getNum', value: function () { var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6() { var $, text; return regeneratorRuntime.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: _context6.next = 2; return this.getContent(); case 2: $ = _context6.sent; text = $('.meta li').eq(1).text(); return _context6.abrupt('return', +(text.match(/(\d+)P/) || [])[1] || 0); case 5: case 'end': return _context6.stop(); } } }, _callee6, this); })); function _getNum() { return _ref6.apply(this, arguments); } return _getNum; }() }, { key: '_getImageUrls', value: function () { var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7() { var _this = this; var num; return regeneratorRuntime.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: _context7.next = 2; return this._getNum(); case 2: num = _context7.sent; return _context7.abrupt('return', _Bluebird2.default.all(_lodash2.default.range(num).map(function (index) { return request.getAsync({ url: _this.getPageUrl('manga_big') + '&page=' + index, jar: _login2.default.cookieJar }); })).then(function (responses) { return responses.map(function (response) { var $ = _cheerio2.default.load(response.body); return $('img').attr('src'); }); })); case 4: case 'end': return _context7.stop(); } } }, _callee7, this); })); function _getImageUrls() { return _ref7.apply(this, arguments); } return _getImageUrls; }() }, { key: '_guessImageUrls', value: function () { var _ref8 = _asyncToGenerator(regeneratorRuntime.mark(function _callee8() { var num, response, $, img0; return regeneratorRuntime.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: _context8.next = 2; return this._getNum(); case 2: num = _context8.sent; _context8.next = 5; return request.getAsync({ url: this.getPageUrl('manga_big') + '&page=0', jar: _login2.default.cookieJar }); case 5: response = _context8.sent; $ = _cheerio2.default.load(response.body); img0 = $('img').attr('src'); return _context8.abrupt('return', _lodash2.default.range(num).map(function (index) { return img0.replace(/_p(\d+)\..*?$/, function ($, $1) { return $.replace('_p' + $1, '_p' + index); }); })); case 9: case 'end': return _context8.stop(); } } }, _callee8, this); })); function _guessImageUrls() { return _ref8.apply(this, arguments); } return _guessImageUrls; }() }, { key: '_getPackUrl', value: function () { var _ref9 = _asyncToGenerator(regeneratorRuntime.mark(function _callee9() { var $, $script, pixiv; return regeneratorRuntime.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: _context9.next = 2; return this.getContent(); case 2: $ = _context9.sent; $script = $('script').filter(function (i, elem) { return $(elem).text().includes('pixiv.context.ugokuIllustFullscreenData'); }); pixiv = new Function('var pixiv={context:{}};' + $script.text() + ';return pixiv;')(); return _context9.abrupt('return', pixiv.context.ugokuIllustFullscreenData.src); case 6: case 'end': return _context9.stop(); } } }, _callee9, this); })); function _getPackUrl() { return _ref9.apply(this, arguments); } return _getPackUrl; }() }, { key: '_getDownloadQueue', value: function () { var _ref10 = _asyncToGenerator(regeneratorRuntime.mark(function _callee10() { var type, info, urls, url; return regeneratorRuntime.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: _context10.next = 2; return this._type(); case 2: type = _context10.sent; _context10.next = 5; return this.getInfo(); case 5: info = _context10.sent; if (!(type === TYPE.MULTIPLE)) { _context10.next = 9; break; } urls = this._guessImageUrls(); return _context10.abrupt('return', urls.map(function (url, index) { return (0, _extend2.default)({}, info, { title: info.title + ' - ' + _lodash2.default.padStart(index + 1, 2, '0'), url: url, suffix: _path2.default.extname(url) }); })); case 9: url = void 0; if (!(type === TYPE.NORMAL)) { _context10.next = 14; break; } _context10.next = 13; return this._getImageUrl(); case 13: url = _context10.sent; case 14: if (!(type === TYPE.UGOIRA)) { _context10.next = 18; break; } _context10.next = 17; return this._getPackUrl(); case 17: url = _context10.sent; case 18: (0, _extend2.default)(info, { url: url, suffix: _path2.default.extname(url) }); return _context10.abrupt('return', [info]); case 20: case 'end': return _context10.stop(); } } }, _callee10, this); })); function _getDownloadQueue() { return _ref10.apply(this, arguments); } return _getDownloadQueue; }() }, { key: 'download', value: function () { var _ref11 = _asyncToGenerator(regeneratorRuntime.mark(function _callee11() { var filepath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '{{author}} - {{title}}{{suffix}}'; var infos, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, info, file, resp; return regeneratorRuntime.wrap(function _callee11$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: _context11.next = 2; return this._getDownloadQueue(); case 2: infos = _context11.sent; _iteratorNormalCompletion = true; _didIteratorError = false; _iteratorError = undefined; _context11.prev = 6; _iterator = infos[Symbol.iterator](); case 8: if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { _context11.next = 25; break; } info = _step.value; _context11.prev = 10; file = (0, _utility.replacePlaceholder)(filepath, info); console.log('Downloading ' + _path2.default.basename(file) + ' ...'); _context11.next = 15; return request.getAsync({ url: info.url, headers: { 'Referer': this.url }, // do not encode the response data // directly dumps to the files. encoding: null, jar: _login2.default.cookieJar }); case 15: resp = _context11.sent; _context11.next = 18; return fs.writeFileAsync(file, resp.body); case 18: _context11.next = 22; break; case 20: _context11.prev = 20; _context11.t0 = _context11['catch'](10); case 22: _iteratorNormalCompletion = true; _context11.next = 8; break; case 25: _context11.next = 31; break; case 27: _context11.prev = 27; _context11.t1 = _context11['catch'](6); _didIteratorError = true; _iteratorError = _context11.t1; case 31: _context11.prev = 31; _context11.prev = 32; if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } case 34: _context11.prev = 34; if (!_didIteratorError) { _context11.next = 37; break; } throw _iteratorError; case 37: return _context11.finish(34); case 38: return _context11.finish(31); case 39: case 'end': return _context11.stop(); } } }, _callee11, this, [[6, 27, 31, 39], [10, 20], [32,, 34, 38]]); })); function download(_x3) { return _ref11.apply(this, arguments); } return download; }() }, { key: 'url', get: function get() { return this.getPageUrl(); } }]); return Illust; }(), (_applyDecoratedDescriptor(_class.prototype, 'getContent', [_utility.cachedProperty, _login.loginRequired], Object.getOwnPropertyDescriptor(_class.prototype, 'getContent'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'getInfo', [_utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, 'getInfo'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_type', [_utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, '_type'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_getImageUrl', [_utility.cachedProperty, _dec], Object.getOwnPropertyDescriptor(_class.prototype, '_getImageUrl'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_getNum', [_utility.cachedProperty, _dec2], Object.getOwnPropertyDescriptor(_class.prototype, '_getNum'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_getImageUrls', [_utility.cachedProperty, _dec3], Object.getOwnPropertyDescriptor(_class.prototype, '_getImageUrls'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_guessImageUrls', [_utility.cachedProperty, _dec4], Object.getOwnPropertyDescriptor(_class.prototype, '_guessImageUrls'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_getPackUrl', [_utility.cachedProperty, _dec5], Object.getOwnPropertyDescriptor(_class.prototype, '_getPackUrl'), _class.prototype)), _class)); exports.default = Illust; module.exports = exports['default'];