pixiv-illust
Version:
A third-party Node.js library for Pixiv.
291 lines (232 loc) • 11.2 kB
JavaScript
'use strict';
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 _login = require('./login');
var _login2 = _interopRequireDefault(_login);
var _utility = require('./utility');
var _Bluebird = require('Bluebird');
var _Bluebird2 = _interopRequireDefault(_Bluebird);
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) { return step("next", value); }, function (err) { return 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 MODES = ['daily', 'weekly', 'monthly', 'rookie', 'male', 'female'];
var RESTRICT_MODES = ['daily_r18', 'weekly_r18', 'r18g', 'male_r18', 'female_r18'];
var PAGE = 'http://www.pixiv.net/ranking.php?format=json';
function dateFormat(d) {
if (typeof d === 'string') {
return d;
}
if (!(d instanceof Date)) {
d = new Date(d);
}
return d.toISOString().match(/^(\d{4})-(\d{2})-(\d{2})/).slice(1).join('');
}
var Rank = (_class = function () {
function Rank() {
var mode = arguments.length <= 0 || arguments[0] === undefined ? 'daily' : arguments[0];
var date = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
_classCallCheck(this, Rank);
if (!MODES.includes(mode) && !RESTRICT_MODES.includes(mode)) {
throw Error('Unvalid mode.');
}
this.mode = mode;
// default yesterday.
this.date = dateFormat(date || new Date() - 86400 * 1000);
}
_createClass(Rank, [{
key: 'getPage',
value: function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
var page = arguments.length <= 0 || arguments[0] === undefined ? 1 : arguments[0];
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!MODES.includes(this.mode)) {
_context.next = 6;
break;
}
_context.next = 3;
return this._getPage(page);
case 3:
return _context.abrupt('return', _context.sent);
case 6:
if (!RESTRICT_MODES.includes(this.mode)) {
_context.next = 10;
break;
}
_context.next = 9;
return this._getRestrictPage(page);
case 9:
return _context.abrupt('return', _context.sent);
case 10:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function getPage(_x3) {
return _ref.apply(this, arguments);
}
return getPage;
}()
}, {
key: '_getRestrictPage',
value: function () {
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2() {
var page = arguments.length <= 0 || arguments[0] === undefined ? 1 : arguments[0];
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
return _context2.abrupt('return', this._getPage(page));
case 1:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function _getRestrictPage(_x5) {
return _ref2.apply(this, arguments);
}
return _getRestrictPage;
}()
}, {
key: '_getPage',
value: function () {
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(page) {
var resp;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return request.getAsync(this.url + '&p=' + page);
case 2:
resp = _context3.sent;
if (!(resp.statusCode !== 200)) {
_context3.next = 5;
break;
}
throw resp;
case 5:
return _context3.abrupt('return', JSON.parse(resp.body));
case 6:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function _getPage(_x7) {
return _ref3.apply(this, arguments);
}
return _getPage;
}()
}, {
key: 'get',
value: function () {
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4(page) {
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return this.getPage(page);
case 2:
return _context4.abrupt('return', _context4.sent);
case 3:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
function get(_x8) {
return _ref4.apply(this, arguments);
}
return get;
}()
}, {
key: 'getAllContents',
value: function () {
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5() {
var contents, page, data;
return regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
contents = [];
page = 1;
case 2:
if (!page) {
_context5.next = 13;
break;
}
console.log(page);
_context5.next = 6;
return this.getPage(page);
case 6:
data = _context5.sent;
if (data && data.contents && data.contents.length) {
_context5.next = 9;
break;
}
return _context5.abrupt('break', 13);
case 9:
page = data.next;
contents = contents.concat(data.contents);
_context5.next = 2;
break;
case 13:
return _context5.abrupt('return', contents);
case 14:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
function getAllContents() {
return _ref5.apply(this, arguments);
}
return getAllContents;
}()
}, {
key: 'url',
get: function get() {
return PAGE + '&mode=' + this.mode + '&date=' + this.date;
}
}]);
return Rank;
}(), (_applyDecoratedDescriptor(_class.prototype, '_getRestrictPage', [_login.loginRequired], Object.getOwnPropertyDescriptor(_class.prototype, '_getRestrictPage'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'getAllContents', [_utility.cachedProperty], Object.getOwnPropertyDescriptor(_class.prototype, 'getAllContents'), _class.prototype)), _class);
var r = new Rank('daily', '20161001');
r.getAllContents().then(console.log);
// r.getPage(2).then(console.log)