Skip to content

Commit

Permalink
Make it even easier to add multiple providers
Browse files Browse the repository at this point in the history
Ref #6
  • Loading branch information
SpaceK33z committed Dec 15, 2016
1 parent 0734271 commit 066e8ee
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 86 deletions.
10 changes: 5 additions & 5 deletions src/imdb.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.imdb-to-plex-button {
.movieo-to-plex-button {
margin-top: 10px;
display: inline-block;
background: #727272;
Expand All @@ -9,19 +9,19 @@
text-transform: uppercase;
}

.imdb-to-plex-button--couchpotato {
.movieo-to-plex-button--couchpotato {
background: #f45a26;
}

.imdb-to-plex-button--couchpotato:hover {
.movieo-to-plex-button--couchpotato:hover {
background: #f67e56;
}

.imdb-to-plex-button--found {
.movieo-to-plex-button--found {
background: #e5a00d;
}

.imdb-to-plex-button--found:hover {
.movieo-to-plex-button--found:hover {
background: #f9be03;
}

Expand Down
43 changes: 2 additions & 41 deletions src/imdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,11 @@ function renderPlexButton() {
return;
}
const el = document.createElement('a');
el.classList.add('imdb-to-plex-button');
el.classList.add('movieo-to-plex-button');
$plotSummary.appendChild(el);
return el;
}

function modifyPlexButton(el, action, title, key) {
if (action === 'found') {
el.href = getPlexMediaUrl(config.plexMachineId, key);
el.textContent = 'On Plex';
el.classList.add('imdb-to-plex-button--found');
}
if (action === 'error') {
el.removeAttribute('href');
el.textContent = 'Not on Plex';
el.classList.remove('imdb-to-plex-button--found');
}
if (action === 'couchpotato') {
el.href = '#';
el.textContent = 'Download';
el.classList.add('imdb-to-plex-button--couchpotato');
el.addEventListener('click', (e) => {
e.preventDefault();
addToCouchpotato(config, imdbId);
});
}

if (title) {
el.title = title;
}
}

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

plexRequest({ url: config.plexUrl, token: config.plexToken, title, year })
.then(({ size, key }) => {
if (size) {
modifyPlexButton($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';
modifyPlexButton($button, action, title);
}
})
.catch((err) => {
modifyPlexButton($button, 'error', 'Request to Plex failed');
console.error('Request to Plex failed', err);
});
handlePlex(config, { title, year, button: $button, imdbId });
}

let config;
Expand Down
42 changes: 2 additions & 40 deletions src/movieo.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,9 @@ function initPlexThingy() {
}
const title = $title.dataset.title.trim();
const year = parseInt($date.content.slice(0, 4));
const imdbId = getImdbId();

plexRequest({ url: config.plexUrl, token: config.plexToken, title, year })
.then(({ size, key }) => {
if (size) {
modifyPlexButton($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';
modifyPlexButton($button, action, title);
}
})
.catch((err) => {
modifyPlexButton($button, 'error', 'Request to Plex failed');
console.error('Request to Plex failed', err);
});
handlePlex(config, { title, year, button: $button, imdbId });
}

function renderPlexButton() {
Expand All @@ -88,32 +76,6 @@ function renderPlexButton() {
return el;
}

function modifyPlexButton(el, action, title, key) {
if (action === 'found') {
el.href = getPlexMediaUrl(config.plexMachineId, key);
el.textContent = 'On Plex';
el.classList.add('movieo-to-plex-button--found');
}
if (action === 'error') {
el.removeAttribute('href');
el.textContent = 'Not on Plex';
el.classList.remove('movieo-to-plex-button--found');
}
if (action === 'couchpotato') {
el.href = '#';
el.textContent = 'Download';
el.classList.add('movieo-to-plex-button--couchpotato');
el.addEventListener('click', (e) => {
e.preventDefault();
addToCouchpotato(config, getImdbId());
});
}

if (title) {
el.title = title;
}
}

function getImdbId() {
const $link = document.querySelector('.tt-parent[href^="http://www.imdb.com/title/tt"]');
if ($link) {
Expand Down
43 changes: 43 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,46 @@ function addToCouchPotatoRequest(imdbId) {
}
});
}

function modifyPlexButton(el, action, title, key) {
if (action === 'found') {
el.href = getPlexMediaUrl(config.plexMachineId, key);
el.textContent = 'On Plex';
el.classList.add('movieo-to-plex-button--found');
}
if (action === 'error') {
el.removeAttribute('href');
el.textContent = 'Not on Plex';
el.classList.remove('movieo-to-plex-button--found');
}
if (action === 'couchpotato') {
el.href = '#';
el.textContent = 'Download';
el.classList.add('movieo-to-plex-button--couchpotato');
el.addEventListener('click', (e) => {
e.preventDefault();
addToCouchpotato(config, key);
});
}

if (title) {
el.title = title;
}
}

function handlePlex(config, options) {
plexRequest({ url: config.plexUrl, token: config.plexToken, title: options.title, year: options.year })
.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';
modifyPlexButton(options.button, action, title, options.imdbId);
}
})
.catch((err) => {
modifyPlexButton(options.button, 'error', 'Request to Plex failed');
console.error('Request to Plex failed', err);
});
}

0 comments on commit 066e8ee

Please sign in to comment.