Skip to content

Commit

Permalink
feat: add tagline, add schema and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrevano committed Apr 17, 2024
1 parent 10f4eee commit 0971514
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 149 deletions.
29 changes: 29 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function checkItemProperties(items) {
expect(item.image).not.toBeNull();
expect(item.image).toMatch(/\.(jpg|jpeg|png|gif)(\?[a-zA-Z0-9=&]*)?$/i);

item.is_active === true ? expect(items.filter((item) => item.tagline).length).toBeGreaterThanOrEqual(config.minimumNumberOfItems.default) : null;

expect(item.ratings_average).not.toBeNull();

item.item_type === "tvshow" && item.platforms_links ? expect(item.platforms_links.filter((link) => link.link_url.startsWith("https")).length).toBe(item.platforms_links.length) : null;
Expand Down Expand Up @@ -177,6 +179,28 @@ function checkItemProperties(items) {
});
}

function checkTypes(item, schema) {
Object.keys(schema).forEach((key) => {
if (item[key] && item.hasOwnProperty(key)) {
const expectedType = schema[key];
const actualType = typeof item[key];

// Check for an array of objects
if (Array.isArray(expectedType) && expectedType.length > 0 && typeof expectedType[0] === "object") {
item[key].forEach((obj) => {
checkTypes(obj, expectedType[0]);
});
} else if (typeof expectedType === "object" && !Array.isArray(expectedType)) {
// Check if the item is an object and recurse
checkTypes(item[key], expectedType);
} else {
// Simple type check
expect(actualType).toBe(expectedType);
}
}
});
}

/**
* An object containing various query parameters and their expected results.
* @param {object} params - An object containing various query parameters and their expected results.
Expand Down Expand Up @@ -268,6 +292,11 @@ const params = {
}),
},

all_keys_type_check: {
query: "",
expectedResult: (items) => items.forEach((item) => checkTypes(item, config.schema)),
},

default_movies: {
query: "",
expectedResult: (items) =>
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whatson-api",
"version": "2.3.1",
"version": "2.4.0",
"description": "What's on? API to retrieve movies and tvshows",
"main": "index.js",
"scripts": {
Expand All @@ -12,7 +12,8 @@
"check-dataset": "bash src/data/checkIds.sh check_dataset movie && bash src/data/checkIds.sh check_dataset tvshow",
"commit": "git add . && git cz",
"push-all-platforms": "git push origin main && git push bitbucket main && git push bitbucket_2 main && git push bitbucket_3 main",
"push": "npm run check-api-version && npm run check-dataset && npm run prettier && npm run test && npm run commit && npm run push-all-platforms",
"unchanged-dataset": "git update-index --assume-unchanged src/assets/films_ids.txt && git update-index --assume-unchanged src/assets/series_ids.txt",
"push": "npm run check-api-version && npm run check-dataset && npm run prettier && npm run test && npm run commit && npm run push-all-platforms && npm run unchanged-dataset",
"update-movie-data-first-half": "node -e \"const { getDateValue } = require('./src/utils/getDateValue'); console.log(getDateValue(true));\" | xargs -I {} node src/updateData.js movie no_update_ids update_db circleci no_active no_check_db_ids {} no_check_data no_force mojo check_services no_delete 7",
"update-movie-data-second-half": "node -e \"const { getDateValue } = require('./src/utils/getDateValue'); console.log(getDateValue(false));\" | xargs -I {} node src/updateData.js movie no_update_ids update_db circleci no_active no_check_db_ids {} no_check_data no_force mojo check_services no_delete 7",
"update-tvshow-data-all": "node -e \"const { getDateValue } = require('./src/utils/getDateValue'); console.log(getDateValue(true));\" | xargs -I {} node src/updateData.js tvshow no_update_ids update_db circleci no_active no_check_db_ids {} no_check_data no_force mojo check_services no_delete 7"
Expand All @@ -31,7 +32,6 @@
"express": "^4.18.2",
"fs": "^0.0.1-security",
"mongodb": "^4.17.2",
"node-fetch": "^2.7.0",
"prettier": "^2.8.8",
"shelljs": "^0.8.5",
"vercel": "^32.7.2"
Expand Down
276 changes: 141 additions & 135 deletions src/assets/series_ids.txt

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,90 @@ const baseURL = {
};

const config = {
/* Schema */
schema: {
_id: "string",
allocine: {
id: "number",
url: "string",
users_rating: "number",
critics_rating: "number",
critics_number: "number",
critics_rating_details: [
{
critic_name: "string",
critic_rating: "number",
},
],
popularity: "number",
},
betaseries: {
id: "string",
url: "string",
users_rating: "number",
},
id: "number",
image: "string",
imdb: {
id: "string",
url: "string",
users_rating: "number",
popularity: "number",
},
is_active: "boolean",
item_type: "string",
platforms_links: [
{
name: "string",
link_url: "string",
},
],
seasons_number: "number",
status: "string",
title: "string",
trailer: "string",
metacritic: {
id: "string",
url: "string",
users_rating: "number",
critics_rating: "number",
},
mojo: {
rank: "number",
url: "string",
lifetime_gross: "string",
},
rotten_tomatoes: {
id: "string",
url: "string",
users_rating: "number",
critics_rating: "number",
},
letterboxd: {
id: "string",
url: "string",
users_rating: "number",
},
senscritique: {
id: "number",
url: "string",
users_rating: "number",
},
updated_at: "string",
trakt: {
id: "string",
url: "string",
users_rating: "number",
},
tmdb: {
id: "number",
url: "string",
users_rating: "number",
},
popularity_average: "number",
ratings_average: "number",
},

/* Database settings */
dbName: "whatson",
collectionName: "data",
Expand Down
7 changes: 1 addition & 6 deletions src/content/getSeasonsNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ const getSeasonsNumber = async (allocineHomepage, tmdbId) => {
let seasonsNumber = null;

const { data } = await getTMDBResponse(allocineHomepage, tmdbId);
if (data && data.number_of_seasons) {
seasonsNumber = data.number_of_seasons;
} else {
if (allocineHomepage.includes(config.baseURLTypeSeries)) seasonsNumber = parseInt($(".stats-number").eq(0).text());
}

if (data && data.number_of_seasons) seasonsNumber = data.number_of_seasons;
if (isNaN(seasonsNumber)) seasonsNumber = null;

return seasonsNumber;
Expand Down
4 changes: 4 additions & 0 deletions src/content/getTraktRating.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ const getTraktRating = async (traktHomepage, traktId) => {
let usersRating = parseInt($(".trakt-rating .rating").text().replace("%", ""));
if (isNaN(usersRating)) usersRating = null;

let tagline = $("#tagline").text();
if (!tagline) tagline = null;

traktObj = {
id: traktId,
url: traktHomepage,
usersRating: usersRating,
tagline: tagline,
};
}
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions src/createJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ const createJSON = async (
}
: null;

const tagline = traktRating && traktRating.tagline;

const mojoObj =
mojoValues !== null
? {
Expand All @@ -186,6 +188,7 @@ const createJSON = async (
title: allocineFirstInfo.allocineTitle,
image: allocineFirstInfo.allocineImage,
trailer: allocineFirstInfo.trailer,
tagline: tagline,
platforms_links: platformsLinks,
seasons_number: allocineFirstInfo.seasonsNumber,
status: allocineFirstInfo.status,
Expand Down
10 changes: 5 additions & 5 deletions src/getMoviesIds.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fetch = require("node-fetch");
const axios = require("axios");

const { config } = require("./config");
const { logErrors } = require("./utils/logErrors");
Expand All @@ -18,15 +18,15 @@ const getMoviesIds = async (cinemaIdParam) => {
};

try {
const response = await fetch(`${base_url}${cinemaIdParam}/p-1/`, options);
const data = await response.json();
let response = await axios.get(`${base_url}${cinemaIdParam}/p-1/`, options);
let data = response.data;
const page = data.pagination.page;
const totalPages = data.pagination.totalPages;

const allMoviesIds = [];
for (let index = page; index <= totalPages; index++) {
const response = await fetch(`${base_url}${cinemaIdParam}/p-${index}/`);
const data = await response.json();
response = await axios.get(`${base_url}${cinemaIdParam}/p-${index}/`, options);
data = response.data;
const results = data.results;
results.forEach((element) => {
allMoviesIds.push(element.movie.internalId);
Expand Down

0 comments on commit 0971514

Please sign in to comment.