Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephellon authored May 10, 2018
1 parent d5d20eb commit 25bffc8
Show file tree
Hide file tree
Showing 11 changed files with 465 additions and 168 deletions.
19 changes: 15 additions & 4 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function addCouchpotato(request, sendResponse) {
})
.then(response => response.json())
.then(response => {
console.log('%cAdded to CouchPotato', 'color: #0f0');

sendResponse({ success: response.success });
})
.catch(error => {
Expand Down Expand Up @@ -64,6 +66,7 @@ function addRadarr(request, sendResponse) {
minimumAvailability: 'preDB',
qualityProfileId: request.QualityProfileId,
rootFolderPath: request.StoragePath,
imdbId: request.IMDbID,
addOptions: {
searchForMovie: true,
},
Expand Down Expand Up @@ -91,6 +94,8 @@ function addRadarr(request, sendResponse) {
location: '+Radarr'
});
} else if (data && data.path) {
console.log(`%cAdded to Radarr [${data.path}]`, 'color: #0f0');

sendResponse({
success: 'Added to ' + data.path
});
Expand Down Expand Up @@ -120,8 +125,8 @@ function addSonarr(request, sendResponse) {
fetch(`${ request.url }/lookup?term=${ query }`, { headers })
.then(response => response.json())
.then(data => {
if (!Array.isArray(data) || data.length < 1) {
throw new Error('Movie not found');
if (!data instanceof Array || !~data.length) {
throw new Error('TV Show not found');
}

let body = {
Expand All @@ -130,8 +135,10 @@ function addSonarr(request, sendResponse) {
minimumAvailability: 'preDB',
qualityProfileId: request.QualityProfileId,
rootFolderPath: request.StoragePath,
imdbId: request.IMDbID,
tvdbId: request.TVDbID,
addOptions: {
searchForMovie: true,
searchForMissingEpisodes: true,
},
};

Expand All @@ -157,6 +164,8 @@ function addSonarr(request, sendResponse) {
location: '+Sonarr'
});
} else if (data && data.path) {
console.log(`%cAdded to Sonarr [${data.path}]`, 'color: #0f0');

sendResponse({
success: 'Added to ' + data.path
});
Expand Down Expand Up @@ -256,7 +265,7 @@ async function searchPlex(request, sendResponse) {
let { options, serverConfig } = request,
headers = {
'X-Plex-Token': serverConfig.token,
Accept: 'application/json',
'Accept': 'application/json',
};

// Try all Plex connection URLs
Expand All @@ -279,6 +288,8 @@ async function searchPlex(request, sendResponse) {
}

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('From:', sender);

switch (request.type) {
case 'SEARCH_PLEX':
searchPlex(request, sendResponse);
Expand Down
49 changes: 8 additions & 41 deletions src/flenix.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ function isMoviePageReady() {
return !!document.querySelector('iframe[title="Disqus"]');
}

function init() {
async function init() {
if (isMoviePage())
if (isMoviePageReady())
initPlexThingy();
await initPlexThingy();
else
// This almost never happens, but sometimes the page is too slow so we need to wait a bit.
setTimeout(init, 1000);
}

parseOptions().then(() => {
parseOptions().then(async() => {
window.addEventListener('popstate', init);
window.addEventListener('pushstate-changed', init);
init();
await init();
});

function initPlexThingy() {
async function initPlexThingy() {
let $button = renderPlexButton();
if (!$button)
return;
Expand All @@ -41,9 +41,9 @@ function initPlexThingy() {

let title = $title.innerText.trim(),
year = parseInt($date.innerText),
IMDbID = getIMDbID(title, year);
IMDbID = await getIDs({ title, year, type: 'imdb' });

findPlexMedia({ title, year, button: $button, IMDbID, remote: '/engine/ajax/get.php', locale: 'flenix' });
findPlexMedia({ title, year, button: $button, IMDbID, type: 'movie', remote: '/engine/ajax/get.php', locale: 'flenix' });
}

function renderPlexButton() {
Expand Down Expand Up @@ -74,44 +74,11 @@ function renderPlexButton() {

pa.classList.add('web-to-plex-wrapper');
el.textContent = 'Web to Plex+';
el.title = 'Loading...';
el.classList.add((li? 'flatButton': 'roundButton'), 'web-to-plex-button');
e.appendChild(li? (pa.appendChild(el), pa): el);
return els.push(el);
});

return els;
}

async function getIMDbID(_title, _year) {
let title = null,
year = null;

if(!_title || !_year){
let $title = document.querySelector('#dle-content .about > h1'),
$date = document.querySelector('.features > .reset:nth-child(2) a');

if(!$title || !$date)
return;

title = $title.innerText.trim();
year = parseInt($date.innerText);
} else {
title = _title;
year = _year;
}

let json = {};

await fetch(`https://www.theimdbapi.org/api/find/movie?title=${ title }&year=${ year }`)
.then(response => {
return response.json();
})
.catch(error => {
throw error;
})
.then(data => {
return json = data[0];
});

return json.imdb_id;
}
30 changes: 18 additions & 12 deletions src/imdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@ function isList() {

function getIMDbID() {
let tag = document.querySelector('meta[property="pageId"]');
return tag && tag.content;
return tag ? tag.content : undefined;
}

const IMDbID = getIMDbID();
let IMDbID = getIMDbID();

function cleanYear(year) {
// The year can contain `()`, so we need to strip it out.
return parseInt(year.trim().replace(/^\(|\)$/g, ''));
}

function renderPlexButton($parent) {
if (!$parent)
return;
if (!$parent) return;

let existingButton = document.querySelector('a.web-to-plex-button');
if (existingButton)
existingButton.remove();

let el = document.createElement('a');

el.textContent = 'Web to Plex+';
el.title = 'Loading...';
el.classList.add('web-to-plex-button');
el.style.display = 'none';
$parent.appendChild(el);
Expand All @@ -57,23 +58,28 @@ function initPlexMovie() {
findPlexMedia({ type: 'movie', title, year, button: $button, IMDbID });
}

function initPlexShow() {
async function initPlexShow() {
let $parent = document.querySelector('.plot_summary'),
$button = renderPlexButton($parent);

if (!$button)
return;

let $title = document.querySelector('.title_wrapper h1'),
date = document.querySelector('title').textContent,
dateMatch = date.match(/Series (\d{4})/);
dateMatch = date.match(/Series\s+(\d{4})/);

if (!$title || !dateMatch)
return modifyPlexButton($button, 'error', 'Could not extract title or year');
return modifyPlexButton($button, 'error', `Could not extract ${ !$title? 'title': 'year' }`);

let title = $title.textContent.trim(),
year = parseInt(dateMatch[1]);
year = parseInt(dateMatch[1]),
Db = await getIDs({ title, year, IMDbID }),
TVDbID = Db.thetvdb;

IMDbID = IMDbID || Db.imdb;

findPlexMedia({ type: 'show', title, year, button: $button, IMDbID });
findPlexMedia({ type: 'show', title, year, button: $button, IMDbID, TVDbID });
}

function addInListItem(el) {
Expand Down Expand Up @@ -105,11 +111,11 @@ function initList() {
}

if (((isMovie() || isShow()) && IMDbID) || isList()) {
parseOptions().then(() => {
parseOptions().then(async() => {
if (isMovie())
initPlexMovie();
await initPlexMovie();
else if (isShow())
initPlexShow();
await initPlexShow();
else
initList();
});
Expand Down
3 changes: 2 additions & 1 deletion src/letterboxd.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function initPlexThingy() {
year = parseInt($date.textContent.trim()),
IMDbID = getIMDbID();

findPlexMedia({ title, year, button: $button, IMDbID });
findPlexMedia({ title, year, button: $button, type: 'movie', IMDbID });
}

function renderPlexButton() {
Expand All @@ -38,6 +38,7 @@ function renderPlexButton() {

parentEl.appendChild(el);
el.textContent = 'Web to Plex+';
el.title = 'Loading...';
el.classList.add('label', 'web-to-plex-button');
$actions.appendChild(parentEl);
return el;
Expand Down
15 changes: 10 additions & 5 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Adds a button on various movie & TV show sites to open it in Plex.",
"homepage_url": "https://github.com/Ephellon/web-to-plex/",
"manifest_version": 2,
"version": "0.2.2.0",
"version": "0.3.0.0",
"icons": {
"16": "img/16.png",
"48": "img/48.png",
Expand All @@ -14,7 +14,7 @@
},
"content_scripts": [
{
"matches": ["https://movieo.me/*"],
"matches": ["https://*.movieo.me/*"],
"js": ["history-hack.js", "utils.js", "movieo.js"],
"css": ["movieo.css"]
},
Expand All @@ -24,19 +24,24 @@
"css": ["imdb.css"]
},
{
"matches": ["https://trakt.tv/*"],
"matches": ["https://*.trakt.tv/*"],
"js": ["history-hack.js", "utils.js", "trakt.js"],
"css": ["trakt.css"]
},
{
"matches": ["https://letterboxd.com/film/*"],
"matches": ["https://*.letterboxd.com/film/*"],
"js": ["utils.js", "letterboxd.js"],
"css": ["letterboxd.css"]
},
{
"matches": ["https://flenix.co/movies/*"],
"matches": ["https://*.flenix.co/movies/*"],
"js": ["utils.js", "flenix.js"],
"css": ["flenix.css"]
},
{
"matches": ["https://*.tvmaze.com/shows/*"],
"js": ["utils.js", "tvmaze.js"],
"css": ["tvmaze.css"]
}
],
"background": {
Expand Down
18 changes: 14 additions & 4 deletions src/manifest_firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Adds a button on various movie & TV show sites to open it in Plex.",
"homepage_url": "https://github.com/Ephellon/web-to-plex/",
"manifest_version": 2,
"version": "0.2.2.0",
"version": "0.3.0.0",
"icons": {
"16": "img/16.png",
"48": "img/48.png",
Expand All @@ -18,7 +18,7 @@
},
"content_scripts": [
{
"matches": ["https://movieo.me/*"],
"matches": ["https://*.movieo.me/*"],
"js": ["history-hack.js", "utils.js", "movieo.js"],
"css": ["movieo.css"]
},
Expand All @@ -28,14 +28,24 @@
"css": ["imdb.css"]
},
{
"matches": ["https://trakt.tv/*"],
"matches": ["https://*.trakt.tv/*"],
"js": ["history-hack.js", "utils.js", "trakt.js"],
"css": ["trakt.css"]
},
{
"matches": ["https://flenix.co/movies/*"],
"matches": ["https://*.letterboxd.com/film/*"],
"js": ["utils.js", "letterboxd.js"],
"css": ["letterboxd.css"]
},
{
"matches": ["https://*.flenix.co/movies/*"],
"js": ["utils.js", "flenix.js"],
"css": ["flenix.css"]
},
{
"matches": ["https://*.tvmaze.com/shows/*"],
"js": ["utils.js", "tvmaze.js"],
"css": ["tvmaze.css"]
}
],
"background": {
Expand Down
3 changes: 2 additions & 1 deletion src/movieo.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function initPlexThingy() {
year = parseInt($date.content.slice(0, 4)),
IMDbID = getIMDbID();

findPlexMedia({ title, year, button: $button, IMDbID });
findPlexMedia({ title, year, button: $button, type: 'movie', IMDbID });
}

function renderPlexButton() {
Expand All @@ -76,6 +76,7 @@ function renderPlexButton() {

let el = document.createElement('a');
el.textContent = 'Web to Plex+';
el.title = 'Loading...';
el.classList.add('button', 'comments-link', 'web-to-plex-button');
$actions.appendChild(el);
return el;
Expand Down
Loading

0 comments on commit 25bffc8

Please sign in to comment.