Fix: Linter warnings (#137)

* Automaticly fixable eslint issues, mostly 3 -> 2 space indentation

* fix: updated plex_userid to camelcase

* Linted and some consistency refactor on middleware

* eslint uses ecmaversion 2020 & allow empty catch rule

* Started linting source files

* Fixed eslint errors & improved a lot of error handling

* Set 2 eslint rules as warning temporarly
This commit was merged in pull request #137.
This commit is contained in:
2022-08-20 17:21:25 +02:00
committed by GitHub
parent cfbd4965db
commit 1815a429b0
83 changed files with 1625 additions and 1294 deletions

View File

@@ -3,27 +3,86 @@ const redisCache = require("../cache/redis");
const { Movie, Show, Person, Credits, ReleaseDates } = require("./types");
const tmdbErrorResponse = (error, typeString = undefined) => {
if (error.status === 404) {
let message = error.response.body.status_message;
class TMDBNotFoundError extends Error {
constructor(message) {
super(message);
throw {
status: 404,
message: message.slice(0, -1) + " in tmdb."
};
this.statusCode = 404;
}
}
class TMDBUnauthorizedError extends Error {
constructor(message = "TMDB returned access denied, requires api token.") {
super(message);
this.statusCode = 401;
}
}
class TMDBUnexpectedError extends Error {
constructor(type, errorMessage) {
const message = `An unexpected error occured while fetching ${type} from tmdb`;
super(message);
this.errorMessage = errorMessage;
this.statusCode = 500;
}
}
class TMDBNotReachableError extends Error {
constructor(
message = "TMDB api not reachable, check your internet connection"
) {
super(message);
}
}
const tmdbErrorResponse = (error, type = null) => {
if (error.status === 404) {
const message = error.response.body.status_message;
throw new TMDBNotFoundError(`${message.slice(0, -1)} in tmdb.`);
} else if (error.status === 401) {
throw {
status: 401,
message: error.response.body.status_message
};
throw new TMDBUnauthorizedError(error?.response?.body?.status_message);
} else if (error?.code === "ENOTFOUND") {
throw new TMDBNotReachableError();
}
throw {
status: 500,
message: `An unexpected error occured while fetching ${typeString} from tmdb`
};
throw new TMDBUnexpectedError(type, error);
};
/**
* Maps our response from tmdb api to a movie/show object.
* @param {String} response from tmdb.
* @param {String} The type declared in listSearch.
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
*/
function mapResults(response, type = null) {
const results = response?.results?.map(result => {
if (type === "movie" || result.media_type === "movie") {
const movie = Movie.convertFromTmdbResponse(result);
return movie.createJsonResponse();
}
if (type === "show" || result.media_type === "tv") {
const show = Show.convertFromTmdbResponse(result);
return show.createJsonResponse();
}
if (type === "person" || result.media_type === "person") {
const person = Person.convertFromTmdbResponse(result);
return person.createJsonResponse();
}
return {};
});
return {
results,
page: response?.page,
total_results: response?.total_results,
total_pages: response?.total_pages
};
}
class TMDB {
constructor(apiKey, cache, tmdbLibrary) {
this.tmdbLibrary = tmdbLibrary || moviedb(apiKey);
@@ -53,15 +112,17 @@ class TMDB {
this.defaultTTL = 86400;
}
getFromCacheOrFetchFromTmdb(cacheKey, tmdbMethod, query) {
return new Promise((resolve, reject) =>
this.cache
.get(cacheKey)
.then(resolve)
.catch(() => this.tmdb(tmdbMethod, query))
.then(resolve)
.catch(error => reject(tmdbErrorResponse(error, tmdbMethod)))
);
async getFromCacheOrFetchFromTmdb(cacheKey, tmdbMethod, query) {
try {
const result = await this.cache.get(cacheKey);
if (!result) throw new Error();
return result;
} catch {
return this.tmdb(tmdbMethod, query)
.then(result => this.cache.set(cacheKey, result, this.defaultTTL))
.catch(error => tmdbErrorResponse(error, tmdbMethod));
}
}
/**
@@ -75,9 +136,9 @@ class TMDB {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.movieInfo}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieInfo", query)
.then(movie => this.cache.set(cacheKey, movie, this.defaultTTL))
.then(movie => Movie.convertFromTmdbResponse(movie));
return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieInfo", query).then(
movie => Movie.convertFromTmdbResponse(movie)
);
}
/**
@@ -89,9 +150,11 @@ class TMDB {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.movieCredits}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieCredits", query)
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
.then(credits => Credits.convertFromTmdbResponse(credits));
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"movieCredits",
query
).then(credits => Credits.convertFromTmdbResponse(credits));
}
/**
@@ -107,11 +170,7 @@ class TMDB {
cacheKey,
"movieReleaseDates",
query
)
.then(releaseDates =>
this.cache.set(cacheKey, releaseDates, this.defaultTTL)
)
.then(releaseDates => ReleaseDates.convertFromTmdbResponse(releaseDates));
).then(releaseDates => ReleaseDates.convertFromTmdbResponse(releaseDates));
}
/**
@@ -124,18 +183,18 @@ class TMDB {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.showInfo}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvInfo", query)
.then(show => this.cache.set(cacheKey, show, this.defaultTTL))
.then(show => Show.convertFromTmdbResponse(show));
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvInfo", query).then(
show => Show.convertFromTmdbResponse(show)
);
}
showCredits(identifier) {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.showCredits}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvCredits", query)
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
.then(credits => Credits.convertFromTmdbResponse(credits));
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvCredits", query).then(
credits => Credits.convertFromTmdbResponse(credits)
);
}
/**
@@ -148,9 +207,9 @@ class TMDB {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.personInfo}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "personInfo", query)
.then(person => this.cache.set(cacheKey, person, this.defaultTTL))
.then(person => Person.convertFromTmdbResponse(person));
return this.getFromCacheOrFetchFromTmdb(cacheKey, "personInfo", query).then(
person => Person.convertFromTmdbResponse(person)
);
}
personCredits(identifier) {
@@ -161,18 +220,18 @@ class TMDB {
cacheKey,
"personCombinedCredits",
query
)
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
.then(credits => Credits.convertFromTmdbResponse(credits));
).then(credits => Credits.convertFromTmdbResponse(credits));
}
multiSearch(search_query, page = 1, include_adult = true) {
const query = { query: search_query, page, include_adult };
const cacheKey = `tmdb/${this.cacheTags.multiSearch}:${page}:${search_query}:${include_adult}`;
multiSearch(searchQuery, page = 1, includeAdult = true) {
const query = { query: searchQuery, page, include_adult: includeAdult };
const cacheKey = `tmdb/${this.cacheTags.multiSearch}:${page}:${searchQuery}:${includeAdult}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMulti", query)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response));
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"searchMulti",
query
).then(response => mapResults(response));
}
/**
@@ -181,13 +240,19 @@ class TMDB {
* @param {Number} page representing pagination of results
* @returns {Promise} dict with query results, current page and total_pages
*/
movieSearch(search_query, page = 1, include_adult = true) {
const tmdbquery = { query: search_query, page, include_adult };
const cacheKey = `tmdb/${this.cacheTags.movieSearch}:${page}:${search_query}:${include_adult}`;
movieSearch(searchQuery, page = 1, includeAdult = true) {
const tmdbquery = {
query: searchQuery,
page,
include_adult: includeAdult
};
const cacheKey = `tmdb/${this.cacheTags.movieSearch}:${page}:${searchQuery}:${includeAdult}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMovie", tmdbquery)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "movie"));
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"searchMovie",
tmdbquery
).then(response => mapResults(response, "movie"));
}
/**
@@ -196,13 +261,19 @@ class TMDB {
* @param {Number} page representing pagination of results
* @returns {Promise} dict with query results, current page and total_pages
*/
showSearch(search_query, page = 1, include_adult = true) {
const tmdbquery = { query: search_query, page, include_adult };
const cacheKey = `tmdb/${this.cacheTags.showSearch}:${page}:${search_query}:${include_adult}`;
showSearch(searchQuery, page = 1, includeAdult = true) {
const tmdbquery = {
query: searchQuery,
page,
include_adult: includeAdult
};
const cacheKey = `tmdb/${this.cacheTags.showSearch}:${page}:${searchQuery}:${includeAdult}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchTv", tmdbquery)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "show"));
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"searchTv",
tmdbquery
).then(response => mapResults(response, "show"));
}
/**
@@ -211,59 +282,37 @@ class TMDB {
* @param {Number} page representing pagination of results
* @returns {Promise} dict with query results, current page and total_pages
*/
personSearch(search_query, page = 1, include_adult = true) {
const tmdbquery = { query: search_query, page, include_adult };
const cacheKey = `tmdb/${this.cacheTags.personSearch}:${page}:${search_query}:${include_adult}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchPerson", tmdbquery)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "person"));
}
movieList(listname, page = 1) {
const query = { page: page };
const cacheKey = `tmdb/${this.cacheTags[listname]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listname, query)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "movie"));
}
showList(listname, page = 1) {
const query = { page: page };
const cacheKey = `tmdb/${this.cacheTags[listname]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "show"));
}
/**
* Maps our response from tmdb api to a movie/show object.
* @param {String} response from tmdb.
* @param {String} The type declared in listSearch.
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
*/
mapResults(response, type = undefined) {
let results = response.results.map(result => {
if (type === "movie" || result.media_type === "movie") {
const movie = Movie.convertFromTmdbResponse(result);
return movie.createJsonResponse();
} else if (type === "show" || result.media_type === "tv") {
const show = Show.convertFromTmdbResponse(result);
return show.createJsonResponse();
} else if (type === "person" || result.media_type === "person") {
const person = Person.convertFromTmdbResponse(result);
return person.createJsonResponse();
}
});
return {
results: results,
page: response.page,
total_results: response.total_results,
total_pages: response.total_pages
personSearch(searchQuery, page = 1, includeAdult = true) {
const tmdbquery = {
query: searchQuery,
page,
include_adult: includeAdult
};
const cacheKey = `tmdb/${this.cacheTags.personSearch}:${page}:${searchQuery}:${includeAdult}`;
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"searchPerson",
tmdbquery
).then(response => mapResults(response, "person"));
}
movieList(listName, page = 1) {
const query = { page };
const cacheKey = `tmdb/${this.cacheTags[listName]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query).then(
response => mapResults(response, "movie")
);
}
showList(listName, page = 1) {
const query = { page };
const cacheKey = `tmdb/${this.cacheTags[listName]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query).then(
response => mapResults(response, "show")
);
}
/**
@@ -278,7 +327,7 @@ class TMDB {
if (error) {
return reject(error);
}
resolve(reponse);
return resolve(reponse);
};
if (!argument) {

View File

@@ -1,7 +1,7 @@
const Movie = require('./types/movie.js')
const Show = require('./types/show.js')
const Person = require('./types/person.js')
const Credits = require('./types/credits.js')
const ReleaseDates = require('./types/releaseDates.js')
const Movie = require("./types/movie");
const Show = require("./types/show");
const Person = require("./types/person");
const Credits = require("./types/credits");
const ReleaseDates = require("./types/releaseDates");
module.exports = { Movie, Show, Person, Credits, ReleaseDates }
module.exports = { Movie, Show, Person, Credits, ReleaseDates };

View File

@@ -61,4 +61,4 @@ interface Genre {
name: string;
}
export { Movie, Show, Person, Genre }
export { Movie, Show, Person, Genre };

View File

@@ -1,65 +1,9 @@
import Movie from "./movie";
import Show from "./show";
/* eslint-disable camelcase */
const Movie = require("./movie");
const Show = require("./show");
class Credits {
constructor(id, cast = [], crew = []) {
this.id = id;
this.cast = cast;
this.crew = crew;
this.type = "credits";
}
static convertFromTmdbResponse(response) {
const { id, cast, crew } = response;
const allCast = cast.map(cast => {
if (cast["media_type"]) {
if (cast.media_type === "movie") {
return CreditedMovie.convertFromTmdbResponse(cast);
} else if (cast.media_type === "tv") {
return CreditedShow.convertFromTmdbResponse(cast);
}
}
return new CastMember(
cast.character,
cast.gender,
cast.id,
cast.name,
cast.profile_path
);
});
const allCrew = crew.map(crew => {
if (cast["media_type"]) {
if (cast.media_type === "movie") {
return CreditedMovie.convertFromTmdbResponse(cast);
} else if (cast.media_type === "tv") {
return CreditedShow.convertFromTmdbResponse(cast);
}
}
return new CrewMember(
crew.department,
crew.gender,
crew.id,
crew.job,
crew.name,
crew.profile_path
);
});
return new Credits(id, allCast, allCrew);
}
createJsonResponse() {
return {
id: this.id,
cast: this.cast.map(cast => cast.createJsonResponse()),
crew: this.crew.map(crew => crew.createJsonResponse())
};
}
}
class CreditedMovie extends Movie {}
class CreditedShow extends Show {}
class CastMember {
constructor(character, gender, id, name, profile_path) {
@@ -107,7 +51,66 @@ class CrewMember {
}
}
class CreditedMovie extends Movie {}
class CreditedShow extends Show {}
class Credits {
constructor(id, cast = [], crew = []) {
this.id = id;
this.cast = cast;
this.crew = crew;
this.type = "credits";
}
static convertFromTmdbResponse(response) {
const { id, cast, crew } = response;
const allCast = cast.map(cast => {
if (cast.media_type) {
if (cast.media_type === "movie") {
return CreditedMovie.convertFromTmdbResponse(cast);
}
if (cast.media_type === "tv") {
return CreditedShow.convertFromTmdbResponse(cast);
}
}
return new CastMember(
cast.character,
cast.gender,
cast.id,
cast.name,
cast.profile_path
);
});
const allCrew = crew.map(crew => {
if (cast.media_type) {
if (cast.media_type === "movie") {
return CreditedMovie.convertFromTmdbResponse(cast);
}
if (cast.media_type === "tv") {
return CreditedShow.convertFromTmdbResponse(cast);
}
}
return new CrewMember(
crew.department,
crew.gender,
crew.id,
crew.job,
crew.name,
crew.profile_path
);
});
return new Credits(id, allCast, allCrew);
}
createJsonResponse() {
return {
id: this.id,
cast: this.cast.map(cast => cast.createJsonResponse()),
crew: this.crew.map(crew => crew.createJsonResponse())
};
}
}
module.exports = Credits;

View File

@@ -1,7 +1,22 @@
/* eslint-disable camelcase */
class Movie {
constructor(id, title, year=undefined, overview=undefined, poster=undefined, backdrop=undefined,
releaseDate=undefined, rating=undefined, genres=undefined, productionStatus=undefined,
tagline=undefined, runtime=undefined, imdb_id=undefined, popularity=undefined) {
constructor(
id,
title,
year = undefined,
overview = undefined,
poster = undefined,
backdrop = undefined,
releaseDate = undefined,
rating = undefined,
genres = undefined,
productionStatus = undefined,
tagline = undefined,
runtime = undefined,
imdb_id = undefined,
popularity = undefined
) {
this.id = id;
this.title = title;
this.year = year;
@@ -16,27 +31,66 @@ class Movie {
this.runtime = runtime;
this.imdb_id = imdb_id;
this.popularity = popularity;
this.type = 'movie';
this.type = "movie";
}
static convertFromTmdbResponse(response) {
const { id, title, release_date, overview, poster_path, backdrop_path, vote_average, genres, status,
tagline, runtime, imdb_id, popularity } = response;
const {
id,
title,
release_date,
overview,
poster_path,
backdrop_path,
vote_average,
genres,
status,
tagline,
runtime,
imdb_id,
popularity
} = response;
const releaseDate = new Date(release_date);
const year = releaseDate.getFullYear();
const genreNames = genres ? genres.map(g => g.name) : undefined
const genreNames = genres ? genres.map(g => g.name) : undefined;
return new Movie(id, title, year, overview, poster_path, backdrop_path, releaseDate, vote_average, genreNames, status,
tagline, runtime, imdb_id, popularity)
return new Movie(
id,
title,
year,
overview,
poster_path,
backdrop_path,
releaseDate,
vote_average,
genreNames,
status,
tagline,
runtime,
imdb_id,
popularity
);
}
static convertFromPlexResponse(response) {
// console.log('response', response)
const { title, year, rating, tagline, summary } = response;
const _ = undefined
const _ = undefined;
return new Movie(null, title, year, summary, _, _, _, rating, _, _, tagline)
return new Movie(
null,
title,
year,
summary,
_,
_,
_,
rating,
_,
_,
tagline
);
}
createJsonResponse() {
@@ -55,7 +109,7 @@ class Movie {
runtime: this.runtime,
imdb_id: this.imdb_id,
type: this.type
}
};
}
}

View File

@@ -1,3 +1,5 @@
/* eslint-disable camelcase */
class Person {
constructor(
id,

View File

@@ -1,30 +1,13 @@
class ReleaseDates {
constructor(id, releases) {
this.id = id;
this.releases = releases;
}
const releaseTypeEnum = {
1: "Premier",
2: "Limited theatrical",
3: "Theatrical",
4: "Digital",
5: "Physical",
6: "TV"
};
static convertFromTmdbResponse(response) {
const { id, results } = response;
const releases = results.map(countryRelease =>
new Release(
countryRelease.iso_3166_1,
countryRelease.release_dates.map(rd => new ReleaseDate(rd.certification, rd.iso_639_1, rd.release_date, rd.type, rd.note))
))
return new ReleaseDates(id, releases)
}
createJsonResponse() {
return {
id: this.id,
results: this.releases.map(release => release.createJsonResponse())
}
}
}
class Release {
class Release {
constructor(country, releaseDates) {
this.country = country;
this.releaseDates = releaseDates;
@@ -33,8 +16,10 @@ class Release {
createJsonResponse() {
return {
country: this.country,
release_dates: this.releaseDates.map(releaseDate => releaseDate.createJsonResponse())
}
release_dates: this.releaseDates.map(releaseDate =>
releaseDate.createJsonResponse()
)
};
}
}
@@ -47,21 +32,13 @@ class ReleaseDate {
this.note = note;
}
releaseTypeLookup(releaseTypeKey) {
const releaseTypeEnum = {
1: 'Premier',
2: 'Limited theatrical',
3: 'Theatrical',
4: 'Digital',
5: 'Physical',
6: 'TV'
}
static releaseTypeLookup(releaseTypeKey) {
if (releaseTypeKey <= Object.keys(releaseTypeEnum).length) {
return releaseTypeEnum[releaseTypeKey]
} else {
// TODO log | Release type not defined, does this need updating?
return null
return releaseTypeEnum[releaseTypeKey];
}
// TODO log | Release type not defined, does this need updating?
return null;
}
createJsonResponse() {
@@ -71,7 +48,44 @@ class ReleaseDate {
release_date: this.releaseDate,
type: this.type,
note: this.note
}
};
}
}
class ReleaseDates {
constructor(id, releases) {
this.id = id;
this.releases = releases;
}
static convertFromTmdbResponse(response) {
const { id, results } = response;
const releases = results.map(
countryRelease =>
new Release(
countryRelease.iso_3166_1,
countryRelease.release_dates.map(
rd =>
new ReleaseDate(
rd.certification,
rd.iso_639_1,
rd.release_date,
rd.type,
rd.note
)
)
)
);
return new ReleaseDates(id, releases);
}
createJsonResponse() {
return {
id: this.id,
results: this.releases.map(release => release.createJsonResponse())
};
}
}

View File

@@ -1,7 +1,20 @@
/* eslint-disable camelcase */
class Show {
constructor(id, title, year=undefined, overview=undefined, poster=undefined, backdrop=undefined,
seasons=undefined, episodes=undefined, rank=undefined, genres=undefined, status=undefined,
runtime=undefined) {
constructor(
id,
title,
year = undefined,
overview = undefined,
poster = undefined,
backdrop = undefined,
seasons = undefined,
episodes = undefined,
rank = undefined,
genres = undefined,
status = undefined,
runtime = undefined
) {
this.id = id;
this.title = title;
this.year = year;
@@ -14,18 +27,44 @@ class Show {
this.genres = genres;
this.productionStatus = status;
this.runtime = runtime;
this.type = 'show';
this.type = "show";
}
static convertFromTmdbResponse(response) {
const { id, name, first_air_date, overview, poster_path, backdrop_path, number_of_seasons, number_of_episodes,
rank, genres, status, episode_run_time, popularity } = response;
const {
id,
name,
first_air_date,
overview,
poster_path,
backdrop_path,
number_of_seasons,
number_of_episodes,
rank,
genres,
status,
episode_run_time,
popularity
} = response;
const year = new Date(first_air_date).getFullYear()
const genreNames = genres ? genres.map(g => g.name) : undefined
const year = new Date(first_air_date).getFullYear();
const genreNames = genres ? genres.map(g => g.name) : undefined;
return new Show(id, name, year, overview, poster_path, backdrop_path, number_of_seasons, number_of_episodes,
rank, genreNames, status, episode_run_time, popularity)
return new Show(
id,
name,
year,
overview,
poster_path,
backdrop_path,
number_of_seasons,
number_of_episodes,
rank,
genreNames,
status,
episode_run_time,
popularity
);
}
createJsonResponse() {
@@ -43,7 +82,7 @@ class Show {
production_status: this.productionStatus,
runtime: this.runtime,
type: this.type
}
};
}
}