This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from tuhinpal/stage
Migrate 2 API routes to new system
- Loading branch information
Showing
4 changed files
with
169 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import apiRequestRawHtml from "./apiRequestRawHtml"; | ||
import DomParser from "dom-parser"; | ||
import seriesFetcher from "./seriesFetcher"; | ||
|
||
export default async function getTitle(id) { | ||
const parser = new DomParser(); | ||
const html = await apiRequestRawHtml(`https://www.imdb.com/title/${id}`); | ||
const dom = parser.parseFromString(html); | ||
const nextData = dom.getElementsByAttribute("id", "__NEXT_DATA__"); | ||
const json = JSON.parse(nextData[0].textContent); | ||
|
||
const props = json.props.pageProps; | ||
|
||
const getCredits = (lookFor, v) => { | ||
const result = props.aboveTheFoldData.principalCredits.find( | ||
(e) => e?.category?.id === lookFor | ||
); | ||
|
||
return result | ||
? result.credits.map((e) => { | ||
if (v === "2") | ||
return { | ||
id: e.name.id, | ||
name: e.name.nameText.text, | ||
}; | ||
|
||
return e.name.nameText.text; | ||
}) | ||
: []; | ||
}; | ||
|
||
return { | ||
id: id, | ||
review_api_path: `/reviews/${id}`, | ||
imdb: `https://www.imdb.com/title/${id}`, | ||
contentType: props.aboveTheFoldData.titleType.id, | ||
contentRating: props.aboveTheFoldData.certificate.rating, | ||
isSeries: props.aboveTheFoldData.titleType.isSeries, | ||
productionStatus: | ||
props.aboveTheFoldData.productionStatus.currentProductionStage.id, | ||
isReleased: | ||
props.aboveTheFoldData.productionStatus.currentProductionStage.id === | ||
"released", | ||
title: props.aboveTheFoldData.titleText.text, | ||
image: props.aboveTheFoldData.primaryImage.url, | ||
images: props.mainColumnData.titleMainImages.edges | ||
.filter((e) => e.__typename === "ImageEdge") | ||
.map((e) => e.node.url), | ||
plot: props.aboveTheFoldData.plot.plotText.plainText, | ||
runtime: | ||
props.aboveTheFoldData.runtime?.displayableProperty?.value?.plainText ?? | ||
"", | ||
runtimeSeconds: props.aboveTheFoldData.runtime?.seconds ?? 0, | ||
rating: { | ||
count: props.aboveTheFoldData.ratingsSummary.voteCount, | ||
star: props.aboveTheFoldData.ratingsSummary.aggregateRating, | ||
}, | ||
award: { | ||
wins: props.mainColumnData.wins.total, | ||
nominations: props.mainColumnData.nominations.total, | ||
}, | ||
genre: props.aboveTheFoldData.genres.genres.map((e) => e.id), | ||
releaseDetailed: { | ||
date: new Date( | ||
props.aboveTheFoldData.releaseDate.year, | ||
props.aboveTheFoldData.releaseDate.month - 1, | ||
props.aboveTheFoldData.releaseDate.day | ||
).toISOString(), | ||
day: props.aboveTheFoldData.releaseDate.day, | ||
month: props.aboveTheFoldData.releaseDate.month, | ||
year: props.aboveTheFoldData.releaseDate.year, | ||
releaseLocation: { | ||
country: props.mainColumnData.releaseDate?.country?.text, | ||
cca2: props.mainColumnData.releaseDate?.country?.id, | ||
}, | ||
originLocations: props.mainColumnData.countriesOfOrigin.countries.map( | ||
(e) => ({ | ||
country: e.text, | ||
cca2: e.id, | ||
}) | ||
), | ||
}, | ||
year: props.aboveTheFoldData.releaseDate.year, | ||
spokenLanguages: props.mainColumnData.spokenLanguages.spokenLanguages.map( | ||
(e) => ({ | ||
language: e.text, | ||
id: e.id, | ||
}) | ||
), | ||
filmingLocations: props.mainColumnData.filmingLocations.edges.map( | ||
(e) => e.node.text | ||
), | ||
actors: getCredits("cast"), | ||
actors_v2: getCredits("cast", "2"), | ||
creators: getCredits("creator"), | ||
creators_v2: getCredits("creator", "2"), | ||
directors: getCredits("director"), | ||
directors_v2: getCredits("director", "2"), | ||
writers: getCredits("writer"), | ||
writers_v2: getCredits("writer", "2"), | ||
top_credits: props.aboveTheFoldData.principalCredits.map((e) => ({ | ||
id: e.category.id, | ||
name: e.category.text, | ||
credits: e.credits.map((e) => e.name.nameText.text), | ||
})), | ||
...(props.aboveTheFoldData.titleType.isSeries | ||
? await seriesFetcher(id) | ||
: {}), | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.