pixiv-illust
Version:
A third-party Node.js library for Pixiv.
283 lines (229 loc) • 11.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 _cheerio = require('cheerio');
var _cheerio2 = _interopRequireDefault(_cheerio);
var _Bluebird = require('Bluebird');
var _Bluebird2 = _interopRequireDefault(_Bluebird);
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 URL = 'http://www.pixiv.net/bookmark.php';
var Bookmark = (_class = function () {
function Bookmark() {
_classCallCheck(this, Bookmark);
}
_createClass(Bookmark, [{
key: '_getPage',
value: function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(page) {
var response;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return request.getAsync({
url: URL + '?p=' + page,
jar: _login2.default.cookieJar
});
case 2:
response = _context.sent;
if (!(response.statusCode !== 200)) {
_context.next = 5;
break;
}
throw response;
case 5:
return _context.abrupt('return', _cheerio2.default.load(response.body));
case 6:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function _getPage(_x) {
return _ref.apply(this, arguments);
}
return _getPage;
}()
}, {
key: 'getPageContent',
value: function () {
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2() {
var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var $;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return this._getPage(page);
case 2:
$ = _context2.sent;
return _context2.abrupt('return', $('.image-item').map(function (i, elem) {
var $elem = $(elem);
var $user = $elem.find('.user');
var url = $elem.find('.work').attr('href') || '';
// The keys here are consistent with the ranking page.
return {
illust_id: +(url.match(/illust_id=(\d+)/) || [])[1] || 0,
url: $elem.find('.work img').attr('src'),
user_name: $user.attr('data-user_name'),
user_id: $user.attr('data-user_id'),
title: $elem.find('.title').text()
};
}).get());
case 4:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function getPageContent(_x2) {
return _ref2.apply(this, arguments);
}
return getPageContent;
}()
}, {
key: 'getPage',
value: function () {
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3() {
var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var $, total, contents;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return this._getPage(page);
case 2:
$ = _context3.sent;
total = parseInt($('.count-badge').text().trim(), 10) || 0;
_context3.next = 6;
return this.getPageContent(page);
case 6:
contents = _context3.sent;
return _context3.abrupt('return', {
currentPage: page,
// 20 illusts per page
totalPage: (total / 20 | 0) + 1,
total: total,
contents: contents
});
case 8:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function getPage(_x4) {
return _ref3.apply(this, arguments);
}
return getPage;
}()
}, {
key: 'get',
value: function () {
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4() {
var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
return _context4.abrupt('return', this.getPage(page));
case 1:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
function get(_x6) {
return _ref4.apply(this, arguments);
}
return get;
}()
}, {
key: 'getAll',
value: function () {
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5() {
var page0, totalPage, ret, i, contents;
return regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return this.getPage();
case 2:
page0 = _context5.sent;
totalPage = page0.totalPage;
ret = page0.contents;
i = 2;
case 6:
if (!(i < totalPage)) {
_context5.next = 14;
break;
}
_context5.next = 9;
return this.getPageContent(i);
case 9:
contents = _context5.sent;
ret = ret.concat(contents);
case 11:
i++;
_context5.next = 6;
break;
case 14:
return _context5.abrupt('return', ret);
case 15:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
function getAll() {
return _ref5.apply(this, arguments);
}
return getAll;
}()
}]);
return Bookmark;
}(), (_applyDecoratedDescriptor(_class.prototype, '_getPage', [_login.loginRequired, _utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, '_getPage'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'getPageContent', [_login.loginRequired, _utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, 'getPageContent'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'getPage', [_utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, 'getPage'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'getAll', [_login.loginRequired, _utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, 'getAll'), _class.prototype)), _class);
exports.default = Bookmark;
module.exports = exports['default'];