UNPKG

card-sharing

Version:

card sharing config generator by parsing online sources

267 lines (199 loc) 9.7 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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 _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios); var _cheerio = require('cheerio'); var _cheerio2 = _interopRequireDefault(_cheerio); 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"); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Source = function () { function Source(options) { _classCallCheck(this, Source); this.name = undefined; this.url = undefined; this.configs = []; var constructor = this.constructor; // Check if name is defined if (!constructor.source) { throw new Error('No source name, please check source file'); } // Check if url is defined if (!constructor.url) { throw new Error('No source url, please check source file'); } this.options = _extends({}, constructor.defaultOptions, options); } _createClass(Source, [{ key: 'get', value: function () { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() { var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var _constructor, source, url, content, parsedConfigs, configs, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, config; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(!force && this.configs && this.configs.length > 0)) { _context.next = 2; break; } return _context.abrupt('return', this.configs); case 2: _constructor = this.constructor, source = _constructor.source, url = _constructor.url; // lets fetch page content _context.prev = 3; console.warn('Fetching source \'' + source + '\' over url: ' + url); _context.next = 7; return this.fetch(); case 7: content = _context.sent; console.warn('Fetched source \'' + source + '\''); _context.next = 14; break; case 11: _context.prev = 11; _context.t0 = _context['catch'](3); return _context.abrupt('return', []); case 14: _context.next = 16; return this.parseConfigs(content); case 16: parsedConfigs = _context.sent; console.warn('Found ' + parsedConfigs.length + ' line(s) on source \'' + source + '\''); configs = []; // filter configs by removing blacklisted _iteratorNormalCompletion = true; _didIteratorError = false; _iteratorError = undefined; _context.prev = 22; _iterator = parsedConfigs[Symbol.iterator](); case 24: if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { _context.next = 34; break; } config = _step.value; _context.next = 28; return config.blacklisted(); case 28: if (!_context.sent) { _context.next = 30; break; } return _context.abrupt('continue', 31); case 30: // if ( await config.unusable() ) { // continue // } configs.push(config); case 31: _iteratorNormalCompletion = true; _context.next = 24; break; case 34: _context.next = 40; break; case 36: _context.prev = 36; _context.t1 = _context['catch'](22); _didIteratorError = true; _iteratorError = _context.t1; case 40: _context.prev = 40; _context.prev = 41; if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } case 43: _context.prev = 43; if (!_didIteratorError) { _context.next = 46; break; } throw _iteratorError; case 46: return _context.finish(43); case 47: return _context.finish(40); case 48: console.warn('Using ' + configs.length + ' line(s) from source \'' + source + '\''); // save configs and return them this.configs = configs; return _context.abrupt('return', configs); case 51: case 'end': return _context.stop(); } } }, _callee, this, [[3, 11], [22, 36, 40, 48], [41,, 43, 47]]); })); function get() { return _ref.apply(this, arguments); } return get; }() }, { key: 'fetch', value: function () { var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2() { var _options, userAgent, timeout, configs, url, res; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _options = this.options, userAgent = _options.userAgent, timeout = _options.timeout, configs = this.configs, url = this.constructor.url; _context2.next = 3; return _axios2.default.get(url, { headers: { 'User-Agent': userAgent }, timeout: timeout }); case 3: res = _context2.sent; return _context2.abrupt('return', res.data); case 5: case 'end': return _context2.stop(); } } }, _callee2, this); })); function fetch() { return _ref2.apply(this, arguments); } return fetch; }() }, { key: 'parseConfigs', value: function parseConfigs(content) { var $ = _cheerio2.default.load(content); // Detect CLines var clines = $('body').html().replace(/(\n|\t|<[^>]*>)/g, ' ').replace(/\s+/g, ' ').match(/C: [a-z0-9.-]+ [0-9]+ [a-z0-9.-]+ [a-z0-9.-]+/g) || []; // should be included here otherwise will give recursive requirement errors var CCcam = require('../configs/cccam').default; // return a mapping of clines to config return clines.map(function (cline) { var _cline$split = cline.split(' '), _cline$split2 = _slicedToArray(_cline$split, 5), proto = _cline$split2[0], host = _cline$split2[1], port = _cline$split2[2], username = _cline$split2[3], password = _cline$split2[4]; return new CCcam({ host: host, port: port, username: username, password: password }); }); } }]); return Source; }(); Source.defaultOptions = { timeout: 10 * 1000, // 10s userAgent: 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Mobile Safari/537.36', maxRepeat: 2 }; exports.default = Source;