@fcc-cdc/it-events
Version:
IT Events Crawler of China
78 lines (77 loc) • 4.6 kB
JavaScript
;
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BagEventAgenda = void 0;
const jsdom_1 = require("jsdom");
require("urlpattern-polyfill");
const Crawler_1 = require("../core/Crawler");
const core_1 = require("./core");
class BagEventAgenda extends core_1.AgendaCrawler {
getList(URI) {
return __asyncGenerator(this, arguments, function* getList_1() {
const { window: { document } } = yield __await(jsdom_1.JSDOM.fromURL(URI));
this.forums = [...document.querySelectorAll('.s_li_fhc')].map(item => (Object.assign({ name: (0, Crawler_1.textFrom)(item, '.s_li_ti .theme'), summary: (0, Crawler_1.textFrom)(item, '.s_li_ti .intro') }, this.getDuration(item.querySelector('.s_li_ti .time')))));
for (const item of document.querySelectorAll('.li > .box.clearfix')) {
const forum = (0, Crawler_1.textFrom)(item.closest('.s_li_fhc'), '.s_li_ti .theme');
yield yield __await(Object.assign({ forum: this.forums.find(({ name }) => name === forum) }, (yield __await(this.getItemFrom(item)))));
}
});
}
getDuration(box) {
if (!box)
return;
const [date, startTime, _, endTime] = box.children;
const dateText = (0, Crawler_1.textOf)(date);
return {
startTime: `${dateText} ${(0, Crawler_1.textOf)(startTime)}`,
endTime: `${dateText} ${(0, Crawler_1.textOf)(endTime)}`
};
}
async getItemFrom(box) {
var _b, _c, _d, _e, _f;
const { baseURI, schema } = this.constructor;
const { pathname: { groups: { event } } } = schema.exec(box.baseURI);
const ID = (_d = (_c = (_b = box
.querySelector('.theme')) === null || _b === void 0 ? void 0 : _b.getAttribute('onclick')) === null || _c === void 0 ? void 0 : _c.match(/\d+/)) === null || _d === void 0 ? void 0 : _d[0];
if (!ID)
return;
const detail = await this.getItem(`${baseURI}/${event}?aId=${ID}`), name = (0, Crawler_1.textFrom)(box, '.g_na');
let mentor = this.mentors.find(({ name: n }) => n === name);
if (!mentor) {
mentor = Object.assign(Object.assign({}, detail.mentor), { name, position: (0, Crawler_1.textFrom)(box, '.g_gszw'), avatar: (_f = (_e = box
.querySelector('img')) === null || _e === void 0 ? void 0 : _e.dataset.original) === null || _f === void 0 ? void 0 : _f.split('?')[0] });
this.mentors.push(mentor);
}
return Object.assign(Object.assign(Object.assign({}, detail), this.getDuration(box.querySelector('.time'))), { mentor });
}
async getItem(URI) {
var _b;
const { window: { document } } = await jsdom_1.JSDOM.fromURL(URI);
return {
title: (0, Crawler_1.textFrom)(document, '.sc_info .ti'),
mentor: {
name: (0, Crawler_1.textFrom)(document, '.g_info .name'),
avatar: 'https://img.bagevent.com' +
((_b = document.querySelector('.gu_li img')) === null || _b === void 0 ? void 0 : _b.dataset.original),
position: (0, Crawler_1.textFrom)(document, '.g_gszw'),
summary: (0, Crawler_1.textFrom)(document, '.intro')
}
};
}
}
_a = BagEventAgenda;
BagEventAgenda.baseURI = 'https://www.bagevent.com/event';
BagEventAgenda.schema = new URLPattern(`${_a.baseURI}/:event(\\d+){/p/:page(\\d+)}?`);
exports.BagEventAgenda = BagEventAgenda;