updates episode decompose

This commit is contained in:
TheBeastLT
2020-02-02 21:16:30 +01:00
parent 051c50de3f
commit 30419f3c64
6 changed files with 249 additions and 97 deletions

View File

@@ -7,59 +7,63 @@ const { Type } = require('./types');
const CINEMETA_URL = 'https://v3-cinemeta.strem.io';
const KITSU_URL = 'https://anime-kitsu.now.sh';
function getMetadata(id, type = Type.SERIES ) {
function getMetadata(id, type = Type.SERIES) {
const key = id.match(/^\d+$/) ? `kitsu:${id}` : id;
const metaType = type === Type.MOVIE ? Type.MOVIE : Type.SERIES;
return cacheWrapMetadata(key,
() => needle('get', `${KITSU_URL}/meta/series/${key}.json`, { open_timeout: 60000 })
.then((response) => {
const body = response.body;
if (body && body.meta && body.meta.id) {
return {
kitsuId: body.kitsu_id,
imdbId: body.imdb_id,
title: body.meta.name,
year: body.meta.year,
genres: body.meta.genres,
videos: body.meta.videos && body.meta.videos
.map((video) => video.imdbSeason
? {
episode: video.episode,
imdbSeason: video.imdbSeason,
imdbEpisode: video.imdbEpisode
}
: {
season: video.season,
episode: video.episode,
kitsuId: video.kitsu_id,
kitsuEpisode: video.kitsuEpisode,
released: video.released
}
),
episodeCount: body.meta.videos && Object.values(body.meta.videos
.filter((entry) => entry.season !== 0)
.sort((a, b) => a.season - b.season)
.reduce((map, next) => {
map[next.season] = map[next.season] + 1 || 1;
return map;
}, {}))
};
} else {
throw new Error('No search results');
}
})
.catch((error) => {
throw new Error(`failed kitsu query ${kitsuId} due: ${error.message}`);
}));
() => needle('get', `${KITSU_URL}/meta/${metaType}/${key}.json`, { open_timeout: 60000 })
.then((response) => {
const body = response.body;
if (body && body.meta && body.meta.id) {
return {
kitsuId: body.meta.kitsu_id,
imdbId: body.meta.imdb_id,
title: body.meta.name,
year: body.meta.year,
genres: body.meta.genres,
videos: (body.meta.videos || [])
.map((video) => video.imdbSeason
? {
season: video.season,
episode: video.episode,
imdbSeason: video.imdbSeason,
imdbEpisode: video.imdbEpisode
}
: {
season: video.season,
episode: video.episode,
kitsuId: video.kitsu_id,
kitsuEpisode: video.kitsuEpisode,
released: video.released
}
),
episodeCount: Object.values((body.meta.videos || [])
.filter((entry) => entry.season !== 0)
.sort((a, b) => a.season - b.season)
.reduce((map, next) => {
map[next.season] = map[next.season] + 1 || 1;
return map;
}, {})),
totalCount: body.meta.videos && body.meta.videos
.filter((entry) => entry.season !== 0).length
};
} else {
throw new Error('No search results');
}
})
.catch((error) => {
throw new Error(`failed kitsu query ${kitsuId} due: ${error.message}`);
}));
}
function escapeTitle(title, hyphenEscape = true) {
return title.toLowerCase()
.normalize('NFKD') // normalize non-ASCII characters
.replace(/[\u0300-\u036F]/g, '')
.replace(/&/g, 'and')
.replace(hyphenEscape ? /[.,_+ -]+/g : /[.,_+ ]+/g, ' ') // replace dots, commas or underscores with spaces
.replace(/[^\w- ()]/gi, '') // remove all non-alphanumeric chars
.trim();
.normalize('NFKD') // normalize non-ASCII characters
.replace(/[\u0300-\u036F]/g, '')
.replace(/&/g, 'and')
.replace(hyphenEscape ? /[.,_+ -]+/g : /[.,_+ ]+/g, ' ') // replace dots, commas or underscores with spaces
.replace(/[^\w- ()]/gi, '') // remove all non-alphanumeric chars
.trim();
}
async function getImdbId(info) {
@@ -67,7 +71,7 @@ async function getImdbId(info) {
return cacheWrapImdbId(key,
() => new Promise((resolve, reject) => {
nameToImdb(info, function(err, res) {
nameToImdb(info, function (err, res) {
if (res) {
resolve(res);
} else {
@@ -85,14 +89,14 @@ async function getKitsuId(title) {
const query = title.replace(/[;]+/g, ' ').replace(/[,%']+/g, '');
return cacheWrapImdbId(query,
() => needle('get', `${KITSU_URL}/catalog/series/kitsu-anime-list/search=${query}.json`, { open_timeout: 60000 })
.then((response) => {
const body = response.body;
if (body && body.metas && body.metas.length) {
return body.metas[0].id.replace('kitsu:', '');
} else {
throw new Error('No search results');
}
}));
.then((response) => {
const body = response.body;
if (body && body.metas && body.metas.length) {
return body.metas[0].id.replace('kitsu:', '');
} else {
throw new Error('No search results');
}
}));
}
module.exports = { escapeTitle, getMetadata, getImdbId, getKitsuId };