UNPKG

@alline/core

Version:

Core for Alline.

60 lines 2.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EpisodeSeriesScraper = void 0; const winston_1 = require("winston"); const lodash_1 = __importDefault(require("lodash")); const createDefaultLogger = (level) => winston_1.createLogger({ level, format: winston_1.format.combine(winston_1.format.colorize({ level: true }), winston_1.format.timestamp(), winston_1.format.printf(({ timestamp, level, label = "default", message, ...other }) => { const otherStr = lodash_1.default.isEmpty(other) ? "" : ` (${JSON.stringify(other)})`; return `${timestamp} [${level}] <${label}> ${message}${otherStr}`; })), transports: [new winston_1.transports.Console()] }); class EpisodeSeriesScraper { constructor(option = {}) { const { logLevel = process.env.NODE_ENV === "production" ? "info" : "debug", logger = createDefaultLogger(logLevel) } = option; this.logger = logger; } async scrap(option) { const { scrapers, title, episodes, season, serializers } = option; const { data: dataSerializer, image: imageSerializer } = serializers; const scrapEpisodes = lodash_1.default.range(episodes.from, episodes.to).map(async (episode) => { const ctx = { title, season, episode, logger: this.logger }; const { data, thumbnails } = await this.scrapEpisode(ctx, scrapers); await Promise.all([ dataSerializer.serialize(data, ctx), imageSerializer.serialize(thumbnails, ctx) ]); }); await Promise.all(scrapEpisodes); } async scrapEpisode(ctx, scrapers) { const episode = { title: [], contentRating: "", directors: [], writers: [] }; const initial = { data: episode, thumbnails: [] }; return scrapers.reduce(async (result, scraper) => { const episode = await result; return scraper.scrap(episode, ctx); }, Promise.resolve(initial)); } } exports.EpisodeSeriesScraper = EpisodeSeriesScraper; //# sourceMappingURL=episodeSeries.js.map