Skip to content

Instantly share code, notes, and snippets.

@rjames86 rjames86/tvShows.js Secret
Created Dec 9, 2018

Embed
What would you like to do?
var apiKey = '';
var title_id = 'fld-6c97429a5f8e46a9933a86119280a554';
var year_id = 'fld-1c0c8069a16042398f3fbad20cd26c37';
var released_id = 'fld-eed9e37f8fc54b15acf578f54a16badf';
var poster_id = 'fld-48de251236474e1a8df96e25d9b1959f';
var imdbid_id = 'fld-d02993f5eea644e69d53edcd805b6217';
var type_id = 'fld-3347d9d91d0a4cdea945e9d271914c9a';
var table_id = 'fld-12581e5b520d455bacdd5ef164969239';
var episode_title_id = 'fld-7b0c60f6600a4561bf57871c33ef4b00';
var episode_released_id = 'fld-b25b2b76771c497c9e9081643ce02311';
var episode_episode_number_id = 'fld-53db17d71c5549cdbc328718e1c4a29e';
var episode_imdbid_id = 'fld-8ff216377a174bf4af43fda4b393d1de';
var episode_season_number_id = 'fld-db4177a34d334a34a1f210f147d66b61';
var imdbURL;
var imdbURLPattern = /\/title\/(\w+)\//;
function fetchSeriesFromURL(seriesID) {
fetchURL = `https://www.omdbapi.com/?i=${seriesID}&apikey=${apiKey}&type=series&r=json`;
return Utils.getJsonFromUrl(fetchURL);
}
function fetchEpisodesFromURL(imdbID, seasonNumber) {
fetchURL = `https://www.omdbapi.com/?i=${imdbID}&apikey=${apiKey}&season=${seasonNumber}`;
return Utils.getJsonFromUrl(fetchURL);
}
function getEpisodes(seriesRecord, totalSeasons, seriesID) {
let seasonNumber = totalSeasons != null ? totalSeasons : 0;
console.log('getting season number ' + seasonNumber);
var episodesIds = new Set();
var currentEpisodesByImdbID = {};
var allCurrentEpisodes = seriesRecord.getFieldValue(table_id);
for (let currentEpisode of allCurrentEpisodes) {
currentEpisodesByImdbID[currentEpisode.getFieldValue(episode_imdbid_id)] = currentEpisode;
episodesIds.add(currentEpisode.getFieldValue(episode_imdbid_id));
}
do {
var seasonEpisodesResp = fetchEpisodesFromURL(seriesID, seasonNumber);
var {
Season,
Episodes,
} = seasonEpisodesResp
for (eachEpisode of Episodes) {
var {
Title,
Released,
Episode,
imdbID,
} = eachEpisode;
console.log('Getting episode ' + Episode);
let tableRecord;
if (episodesIds.has(imdbID)) {
console.log('epsiode already exists: ' + Title);
tableRecord = currentEpisodesByImdbID[imdbID];
} else {
tableRecord = seriesRecord.addNewRecordToField(table_id)
}
tableRecord.setFieldValues({
[episode_title_id]: Title,
[episode_released_id]: new Date(Released),
[episode_episode_number_id]: Number(Episode),
[episode_imdbid_id]: imdbID,
[episode_season_number_id]: seasonNumber,
})
}
seasonNumber -= 1;
} while (seasonNumber > 0)
document.saveAllChanges();
}
function getSeries(seriesID) {
var resp = fetchSeriesFromURL(seriesID);
var {
Title,
Released,
Poster,
imdbID,
Type,
totalSeasons
} = resp;
if (totalSeasons != null) {
totalSeasons = Number(totalSeasons);
}
if (Released != null) {
Released = new Date(Released);
}
var seriesIds = new Set();
var currentSeriesByImdbID = {};
var allCurrentSeries = form.getRecords();
for (let currentSeries of allCurrentSeries) {
currentSeriesByImdbID[currentSeries.getFieldValue(imdbid_id)] = currentSeries;
seriesIds.add(currentSeries.getFieldValue(imdbid_id));
}
let newRecord;
if (seriesIds.has(imdbID)) {
console.log('series already exists ' + Title);
newRecord = currentSeriesByImdbID[imdbID];
} else {
newRecord = form.addNewRecord();
newRecord.setFieldValues({
[title_id]: Title,
[released_id]: Released,
[imdbid_id]: imdbID,
[type_id]: Type,
})
}
if (Poster != null) {
newRecord.addPhotoFromUrlToField(Poster, poster_id);
}
// This seem to be required to create table items
document.saveAllChanges();
if (totalSeasons != null) {
getEpisodes(newRecord, totalSeasons, imdbID);
}
}
function processImdbUrl() {
var result = imdbURLPattern.exec(imdbURL);
if (result != null) {
var [_, seriesID] = result;
getSeries(seriesID);
}
}
var prompter = Prompter.new();
prompter.cancelButtonTitle = 'Cancel';
prompter.continueButtonTitle = 'Go';
prompter.addParameter('IMDB Url', 'imdbURL');
prompter.show('Enter an IMDB url', processImdbUrl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.