Utilizing the TMDB API for retrieving movie data and integrating it into a Google Sheet. The original Google Sheet was revamped from Reddit user 6745408's "MediaSheet 3.0". This sheet incorporates a Javascript-based script. By following the patterns/code used in the initial script, I managed to extract values for keys that were not present originally. However, I am faced with a challenge:
There exist multiple arrays nested within the "results" key. Each array under "result" represents a video associated with the movie title. My goal is to fetch the URL ending ("key") of a particular video into the Google Sheet. Specifically, I aim to filter the "results" array to include videos with values ("site": "YouTube") and either ("type": "trailer) or ("type": "teaser"), selecting the first result meeting these criteria and returning its corresponding "key".
The JSON data from the TMDB API that requires parsing:
{
"id": 290098,
"results": [
{
"iso_639_1": "en",
"iso_3166_1": "US",
"name": "In the Projection Booth - Park Chan-wook, director of The Handmaiden (contains spoilers)",
"key": "P8g8QJk96M4",
"site": "YouTube",
"size": 1080,
"type": "Featurette",
"official": true,
"published_at": "2017-04-14T08:30:01.000Z",
"id": "65be6283902012012fc9a5a7"
},
...
]
}
The Google Sheets App Script code currently being worked on:
function TMDBmovietrailer123(rows) {
var tmdbKey = PropertiesService.getScriptProperties().getProperty('tmdbkey');
const requests = rows.map(id => {
return {
url: `https://api.themoviedb.org/3/movie/${id}/videos?api_key=${tmdbKey}`,
muteHttpExceptions: true
}
})
const responses = UrlFetchApp.fetchAll(requests)
return responses.map(request => {
try {
const data = JSON.parse(request.getContentText());
const id = data.id
return [id]
} catch (err) {
return ['']
}
})
}