Skip to content

Commit

Permalink
Add support for TV shows in IMDb
Browse files Browse the repository at this point in the history
Ref #14
  • Loading branch information
SpaceK33z committed Dec 18, 2016
1 parent 78cbb25 commit 0cc67f6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
38 changes: 34 additions & 4 deletions src/imdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ function isMovie() {
return tag && tag.content === 'video.movie';
}

function isShow() {
const tag = document.querySelector('meta[property="og:type"]');
return tag && tag.content === 'video.tv_show';
}

function getImdbId() {
const tag = document.querySelector('meta[property="pageId"]');
return tag && tag.content;
Expand All @@ -22,7 +27,7 @@ function renderPlexButton() {
return el;
}

function initPlexThingy() {
function initPlexMovie(type) {
$button = renderPlexButton();
if (!$button) {
return;
Expand All @@ -34,14 +39,39 @@ function initPlexThingy() {
// The year element contains `()`, so we need to strip it out.
const year = parseInt($year.textContent.trim().replace(/\(|\)/g, ''));

handlePlex(config, { title, year, button: $button, imdbId });
handlePlex(config, { type: 'movie', title, year, button: $button, imdbId });
}

function initPlexShow(type) {
$button = renderPlexButton();
if (!$button) {
return;
}
const $title = document.querySelector('.title_wrapper h1');
const date = document.querySelector('title').textContent;
const dateMatch = date.match(/Series (\d{4})/);
if (!$title || !dateMatch) {
modifyPlexButton($button, 'error', 'Could not extract title or year');
return;
}
const title = $title.textContent.trim();
const year = parseInt(dateMatch[1]);

handlePlex(config, { type: 'show', title, year, button: $button, imdbId });
}

let config;
if (isMovie() && imdbId) {
if ((isMovie() || isShow()) && imdbId) {
getOptions().then((options) => {
config = options;
initPlexThingy();
if (isMovie()) {
initPlexMovie();
} else {
// TODO: Legacy configs may not have TV show sections set.
if (config.server.showSections) {
initPlexShow();
}
}
}, () => {
showNotification('warning', 'Not all options for the Web to Plex extension are filled in.');
});
Expand Down
19 changes: 14 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ function wait(check, then) {

function doPlexRequest(config, options) {
// TODO: it is possible that there are multiple movie sections in Plex, so optimally we'd loop through all of them.
const sectionId = config.server.movieSections[0];
let sectionId;
if (options.type === 'show') {
sectionId = config.server.showSections[0];
} else {
sectionId = config.server.movieSections[0];
}
const url = `${config.server.url}/library/sections/${sectionId}/all`;
return fetch(`${url}?title=${options.title}&year=${options.year}`, {
headers: {
Expand All @@ -21,7 +26,9 @@ function doPlexRequest(config, options) {
const size = res.MediaContainer && res.MediaContainer.size;
let key = null;
if (size) {
key = res.MediaContainer.Metadata[0].key;
// With TV shows, the API returns a pathname with `/children` after it.
// We don't need that part.
key = res.MediaContainer.Metadata[0].key.replace('/children', '');
}
return { size, key };
});
Expand Down Expand Up @@ -59,6 +66,7 @@ function getOptions() {
url: items.plexUrlRoot || items.server.url,
token: items.server && items.server.token || items.plexToken,
movieSections: items.plexLibraryId || items.server.movieSections,
showSections: items.server && items.server.showSections,
},
};
if (items.couchpotatoBasicAuthUsername) {
Expand Down Expand Up @@ -175,13 +183,14 @@ function modifyPlexButton(el, action, title, key) {
}

function handlePlex(config, options) {
plexRequest(config, { title: options.title, year: options.year })
plexRequest(config, options)
.then(({ size, key }) => {
if (size) {
modifyPlexButton(options.button, 'found', 'Found on Plex', key);
} else {
const action = config.couchpotatoUrl ? 'couchpotato' : 'error';
const title = config.couchpotatoUrl ? 'Could not find, add on Couchpotato?' : 'Could not find on Plex';
const showCouchpotato = config.couchpotatoUrl && options.type !== 'show';
const action = showCouchpotato ? 'couchpotato' : 'error';
const title = showCouchpotato ? 'Could not find, add on Couchpotato?' : 'Could not find on Plex';
modifyPlexButton(options.button, action, title, options.imdbId);
}
})
Expand Down

0 comments on commit 0cc67f6

Please sign in to comment.