pixiv-illust
Version:
A third-party Node.js library for Pixiv.
299 lines (246 loc) • 12.9 kB
JavaScript
;
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 _desc, _value, _class;
require('babel-polyfill');
var _Bluebird = require('Bluebird');
var _Bluebird2 = _interopRequireDefault(_Bluebird);
var _cheerio = require('cheerio');
var _cheerio2 = _interopRequireDefault(_cheerio);
var _login = require('./login');
var _login2 = _interopRequireDefault(_login);
var _utility = require('./utility');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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"); }); }; }
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;
}
var request = _Bluebird2.default.promisifyAll(require('request'));
var Author = (_class = function () {
function Author(id) {
_classCallCheck(this, Author);
this.id = id;
}
_createClass(Author, [{
key: 'illustPageUrl',
value: function illustPageUrl() {
var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return 'http://www.pixiv.net/member_illust.php?id=' + this.id + '&type=all&p=' + page;
}
}, {
key: 'getInfo',
value: function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
var url, response, $;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
url = this.infoUrl;
_context.next = 3;
return request.getAsync({
url: url,
jar: _login2.default.cookieJar
});
case 3:
response = _context.sent;
$ = _cheerio2.default.load(response.body);
return _context.abrupt('return', $('.worksListOthers .ws_table').eq(0).find('tr').map(function (i, elem) {
var $elem = $(elem);
return {
name: $elem.find('.td1').text(),
value: $elem.find('.td2').text().trim()
};
}).get());
case 6:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function getInfo() {
return _ref.apply(this, arguments);
}
return getInfo;
}()
}, {
key: '_getIllustsPage',
value: function () {
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(page) {
var url, response;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
url = this.illustPageUrl(page);
_context2.next = 3;
return request.getAsync({
url: url,
jar: _login2.default.cookieJar
});
case 3:
response = _context2.sent;
return _context2.abrupt('return', _cheerio2.default.load(response.body));
case 5:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function _getIllustsPage(_x2) {
return _ref2.apply(this, arguments);
}
return _getIllustsPage;
}()
}, {
key: 'getIllustsContent',
value: function () {
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3() {
var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var $;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return this._getIllustsPage(page);
case 2:
$ = _context3.sent;
return _context3.abrupt('return', $('.image-item').map(function (i, elem) {
var $elem = $(elem);
// The keys here are consistent with the ranking page.
return {
illust_id: +($elem.find('a.work').attr('href').match(/\d+$/) || [0])[0],
url: $elem.find('img._thumbnail').attr('src'),
title: $elem.find('.title').text()
};
}).get());
case 4:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function getIllustsContent(_x3) {
return _ref3.apply(this, arguments);
}
return getIllustsContent;
}()
}, {
key: 'getIllusts',
value: function () {
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4() {
var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var $, contents, total;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return this._getIllustsPage(page);
case 2:
$ = _context4.sent;
_context4.next = 5;
return this.getIllustsContent(page);
case 5:
contents = _context4.sent;
total = parseInt($('.count-badge').text().trim(), 10) || 0;
return _context4.abrupt('return', {
currentPage: page,
// 20 illusts per page
totalPage: (total / 20 | 0) + 1,
total: total,
contents: contents
});
case 8:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
function getIllusts(_x5) {
return _ref4.apply(this, arguments);
}
return getIllusts;
}()
}, {
key: 'getAllIllusts',
value: function () {
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5() {
var page0, totalPage, contents, i, pageContents;
return regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return this.getIllusts();
case 2:
page0 = _context5.sent;
totalPage = page0.totalPage;
contents = page0.contents;
i = 2;
case 6:
if (!(i <= totalPage)) {
_context5.next = 14;
break;
}
_context5.next = 9;
return this.getIllustsContent(i);
case 9:
pageContents = _context5.sent;
contents = contents.concat(pageContents);
case 11:
i++;
_context5.next = 6;
break;
case 14:
return _context5.abrupt('return', contents);
case 15:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
function getAllIllusts() {
return _ref5.apply(this, arguments);
}
return getAllIllusts;
}()
}, {
key: 'infoUrl',
get: function get() {
return 'http://www.pixiv.net/member.php?id=' + this.id;
}
}]);
return Author;
}(), (_applyDecoratedDescriptor(_class.prototype, 'getInfo', [_login.loginRequired, _utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, 'getInfo'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_getIllustsPage', [_login.loginRequired, _utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, '_getIllustsPage'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'getIllustsContent', [_utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, 'getIllustsContent'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'getIllusts', [_utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, 'getIllusts'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'getAllIllusts', [_utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, 'getAllIllusts'), _class.prototype)), _class);
exports.default = Author;
module.exports = exports['default'];