Skip to content

Commit

Permalink
Add support for CouchPotato if movie is not on Plex
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceK33z committed Dec 12, 2016
1 parent 21d4e46 commit e813e8b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 18 deletions.
10 changes: 7 additions & 3 deletions src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
opacity: .9;
}

.movieo-to-plex-warning {
.movieo-to-plex-notification {
display: block;
position: absolute;
position: fixed;
top: 80px;
left: 50%;
margin-left: -175px;
width: 350px;
background: #fd6a00;
background: #21262e;
color: #fff;
font-size: 20px;
padding: 10px;
border-radius: 4px;
z-index: 1000;
}

.movieo-to-plex-warning {
background: #fd6a00;
}
89 changes: 74 additions & 15 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
let plexUrl = '';
let plexToken = '';
let plexMachineId;
let couchpotatoUrlRoot = '';
let couchpotatoToken = '';

function isMoviePage() {
const path = window.location.pathname;
Expand All @@ -20,16 +22,28 @@ function init() {
}
}

function showNotification(state, text) {
const el = document.createElement('div')
el.classList.add('movieo-to-plex-notification');
if (state === 'warning') {
el.classList.add('movieo-to-plex-warning');
}
el.innerHTML = text;
document.body.appendChild(el);
setTimeout(() => {
document.body.removeChild(el);
}, 5000);
}

chrome.storage.sync.get(null, function(items) {
plexUrl = `${items.plexUrlRoot}/library/sections/${items.plexLibraryId}/all`;
plexToken = items.plexToken;
plexMachineId = items.plexMachineId;
couchpotatoUrlRoot = items.couchpotatoUrlRoot;
couchpotatoToken = items.couchpotatoToken;

if (!plexToken || !plexMachineId || !items.plexLibraryId || !items.plexUrlRoot) {
const warning = document.createElement('div')
warning.classList.add('movieo-to-plex-warning');
warning.innerHTML = 'Not all options for the Movieo to Plex extension are filled in.';
document.body.appendChild(warning);
showNotification('warning', 'Not all options for the Movieo to Plex extension are filled in.');
return;
}

Expand All @@ -48,13 +62,11 @@ function doPlexRequest($icon, title, year) {
})
.then((res) => {
const size = res.data.MediaContainer && res.data.MediaContainer.size;
let key = null;
if (size) {
key = res.data.MediaContainer.Metadata[0].key;
modifyPlexIcon($icon, 'Found on Plex', key);
} else {
modifyPlexIcon($icon, 'Could not find on Plex');
}
return size;
return { size, key };
});
}

Expand All @@ -64,24 +76,33 @@ function initPlexThingy() {
const $title = document.getElementById('doc_title');
const $date = document.querySelector('meta[itemprop="datePublished"]');
if (!$title || !$date) {
modifyPlexIcon($icon, 'Could not extract title or year from Movieo');
modifyPlexIcon($icon, 'error', 'Could not extract title or year from Movieo');
return;
}
const title = $title.dataset.title.trim();
const year = parseInt($date.content.slice(0, 4));

doPlexRequest($icon, title, year)
.then((size) => {
.then(({ size, key }) => {
if (!size) {
// This is fucked up, but Plex' definition of a year is year when it was available,
// not when it was released (which is Movieo's definition).
// For examples, see Bone Tomahawk, The Big Short, The Hateful Eight.
return doPlexRequest($icon, title, year + 1);
}
return null;
return { size, key };
})
.then(({ size, key }) => {
if (size) {
modifyPlexIcon($icon, 'found', 'Found on Plex', key);
} else {
const action = couchpotatoUrlRoot ? 'couchpotato' : 'error';
const title = couchpotatoUrlRoot ? 'Could not find, add on Couchpotato?' : 'Could not find on Plex';
modifyPlexIcon($icon, action, title);
}
})
.catch((err) => {
modifyPlexIcon($icon, 'Request to Plex failed');
modifyPlexIcon($icon, 'error', 'Request to Plex failed');
console.error('Request to Plex failed', err);
});
}
Expand All @@ -97,16 +118,54 @@ function renderPlexIcon() {
return el;
}

function modifyPlexIcon(el, title, key) {
if (key) {
function modifyPlexIcon(el, action, title, key) {
if (action === 'found') {
el.href = `https://app.plex.tv/web/app#!/server/${plexMachineId}/details/${encodeURIComponent(key)}`;
el.classList.add('plex-found');
} else {
}
if (action === 'error') {
el.removeAttribute('href');
el.classList.remove('plex-found');
}
if (action === 'couchpotato') {
el.href = '#';
el.addEventListener('click', (e) => {
e.preventDefault();
addToCouchpotato();
});
}

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

function getImdbId() {
const $link = document.querySelector('.tt-parent[href^="http://www.imdb.com/title/tt"]');
if ($link) {
return $link.href.replace('http://www.imdb.com/title/', '');
}
return null;
}

function addToCouchpotato(action) {
const url = `${couchpotatoUrlRoot}/api/${encodeURIComponent(couchpotatoToken)}/movie.add`;
const imdbId = getImdbId();
if (!imdbId) {
console.log('Cancelled adding to CouchPotato since there is no IMDB ID');
return;
}
axios.get(url, {
params: { identifier: imdbId },
headers: {
Authorization: 'Basic a2Vlczpib25rZXJzdGVpbg==',
},
})
.then((res) => {
showNotification('info', 'Added movie on CouchPotato.');
})
.catch((err) => {
showNotification('warning', 'Could not add to CouchPotato (look in DevTools for more info)');
console.error('Error with adding on CouchPotato:', err);
});
}
15 changes: 15 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@
<strong>Example:</strong> 1
</p>

<hr>

<p>
CouchPotato URL <em>(optional)</em>:
<input type="text" id="couchpotato_url_root"><br>
<strong>Example:</strong> https://example.com/couchpotato or http://192.168.1.100:5050 (no trailing slash!)
</p>

<p>
CouchPotato token <em>(optional)</em>:
<input type="text" id="couchpotato_token"><br>
In CouchPotato, go to Settings, click on "Show advanced" and copy paste the Api Token.
<strong>Example:</strong> aa756d33242f6g8ffbca2b3963586f21
</p>

<div id="status"></div>
<button id="save">Save</button>

Expand Down
6 changes: 6 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ function saveOptions() {
var plexMachineId = document.getElementById('plex_machine_id').value;
var plexUrlRoot = document.getElementById('plex_url_root').value;
var plexLibraryId = document.getElementById('plex_library_id').value;
var couchpotatoUrlRoot = document.getElementById('couchpotato_url_root').value;
var couchpotatoToken = document.getElementById('couchpotato_token').value;
chrome.storage.sync.set({
plexToken,
plexMachineId,
plexUrlRoot,
plexLibraryId,
couchpotatoUrlRoot,
couchpotatoToken,
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById('status');
Expand All @@ -28,6 +32,8 @@ function restoreOptions() {
document.getElementById('plex_machine_id').value = items.plexMachineId || '';
document.getElementById('plex_url_root').value = items.plexUrlRoot || '';
document.getElementById('plex_library_id').value = items.plexLibraryId || '';
document.getElementById('couchpotato_url_root').value = items.couchpotatoUrlRoot || '';
document.getElementById('couchpotato_token').value = items.couchpotatoToken || '';
});
}
document.addEventListener('DOMContentLoaded', restoreOptions);
Expand Down

0 comments on commit e813e8b

Please sign in to comment.