@alline/scraper-tvdb
Version:
TVDB scraper for Alline.
110 lines • 4.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TvdbEpisodeScraper = void 0;
const lodash_1 = __importDefault(require("lodash"));
const axios_1 = __importDefault(require("axios"));
const core_1 = require("@alline/core");
const tapable_1 = require("tapable");
const moment_1 = __importDefault(require("moment"));
class TvdbEpisodeScraper extends core_1.EpisodeScraper {
constructor(option) {
super();
this.initToken = false;
this.option = option;
this.axios = axios_1.default.create({
baseURL: "https://api.thetvdb.com/",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"Accept-Language": this.option.acceptLanguage
}
});
this.tvdbHooks = {
transformTitle: new tapable_1.AsyncSeriesWaterfallHook(["title", "ctx"]),
transformDate: new tapable_1.AsyncSeriesWaterfallHook(["aired", "ctx"]),
transformSummary: new tapable_1.AsyncSeriesWaterfallHook(["summary", "ctx"]),
transformThumbnails: new tapable_1.AsyncSeriesWaterfallHook(["thumbnails", "ctx"])
};
}
async onScrap(rlt, ctx) {
const { transformSummary, transformTitle, transformDate, transformThumbnails } = this.tvdbHooks;
const { logger } = ctx;
await this.initAuthToken(ctx);
const { data: episodeData } = await this.fetchEpisode(ctx);
logger.debug("onScrap", { label: "TvdbEpisodeScraper", data: episodeData });
const { fields } = this.option;
const data = {};
let thumbnails = [];
if (fields.includes("title") || fields.length <= 0) {
data.title = await transformTitle.promise([episodeData.episodeName], ctx);
}
if (fields.includes("aired") || fields.length <= 0) {
const date = moment_1.default(episodeData.firstAired);
const aired = await transformDate.promise(date, ctx);
data.aired = aired.format("YYYY-MM-DD");
}
if (fields.includes("summary") || fields.length <= 0) {
data.summary = await transformSummary.promise(episodeData.overview, ctx);
}
if (fields.includes("thumbnails") || fields.length <= 0) {
const { filename } = episodeData;
if (filename) {
thumbnails = await transformThumbnails.promise([`https://www.thetvdb.com/banners/${filename}`], ctx);
}
}
return lodash_1.default.merge({}, rlt, { data, thumbnails });
}
async fetchEpisode(ctx) {
var _a, _b;
const { season, episode, logger } = ctx;
const { tvdbId } = this.option;
try {
const res = await this.axios.get(`/series/${tvdbId}/episodes/query`, {
params: {
airedSeason: season,
airedEpisode: episode
}
});
return res.data;
}
catch (e) {
logger.error("Failed to find episode", {
label: "TvdbEpisodeScraper",
error: (_b = (_a = e.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.Error,
season,
episode
});
throw e;
}
}
async initAuthToken(ctx) {
var _a, _b;
if (this.initToken) {
return;
}
const { logger } = ctx;
const { apiKey: apikey, userKey: userkey, userName: username } = this.option;
try {
const res = await this.axios.post("/login", {
apikey,
userkey,
username
});
const { token } = res.data;
this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
this.initToken = true;
}
catch (e) {
logger.error("Failed to fetch authentication token.", {
label: "TvdbEpisodeScraper",
error: (_b = (_a = e.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.Error
});
throw e;
}
}
}
exports.TvdbEpisodeScraper = TvdbEpisodeScraper;
//# sourceMappingURL=index.js.map