Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephellon committed May 10, 2018
1 parent 611aa7d commit 3196f6d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 32 deletions.
18 changes: 13 additions & 5 deletions src/flenix.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ function isMoviePageReady() {
return !!document.querySelector('iframe[title="Disqus"]');
}

async function init() {
function init() {
if (isMoviePage())
if (isMoviePageReady())
await initPlexThingy();
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(async() => {
parseOptions().then(() => {
window.addEventListener('popstate', init);
window.addEventListener('pushstate-changed', init);
await init();
init();
});

async function initPlexThingy() {

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

let meta = {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
mode: 'no-cors'
};

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

findPlexMedia({ title, year, button: $button, IMDbID, type: 'movie', remote: '/engine/ajax/get.php', locale: 'flenix' });
}
Expand Down Expand Up @@ -77,6 +84,7 @@ function renderPlexButton() {
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);
});

Expand Down
4 changes: 4 additions & 0 deletions src/letterboxd.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
line-height: 1.66666667;
}

.web-to-plex-button:hover {
color: #f67e56!important;
}

.web-to-plex-notification {
cursor: pointer;
display: block;
Expand Down
11 changes: 9 additions & 2 deletions src/letterboxd.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ function renderPlexButton() {
return;

let parentEl = document.createElement('p'),
el = document.createElement('a');
el = document.createElement('a'),
ch = document.createElement('span');

ch.classList.add('icon', '-web-to-plex');
ch.setAttribute('style', `background: url("${ chrome.extension.getURL('img/16.png') }") no-repeat !important`);

parentEl.appendChild(el);
el.textContent = 'Web to Plex+';
el.title = 'Loading...';
el.classList.add('label', 'web-to-plex-button');

parentEl.appendChild(ch);
parentEl.appendChild(el);
$actions.appendChild(parentEl);

return el;
}

Expand Down
3 changes: 2 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@
"optional_permissions": [
"http://*/",
"https://*/"
]
],
"web_accessible_resources": ["img/16.png", "img/48.png"]
}
3 changes: 2 additions & 1 deletion src/manifest_firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
"storage",
"http://*/",
"https://*/"
]
],
"web_accessible_resources": ["img/16.png", "img/48.png"]
}
51 changes: 28 additions & 23 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,28 @@ function parseOptions() {
);
}

async function getIDs({ title, year, type, IMDbID, APIID }) {
async function getIDs({ title, year, type, IMDbID, APIID, meta }) {
let json = {},
data = {},
promise;
promise,
api = {
tvdb: 'bcb95f026f9a01ffa707fcff71900e94',
};

type = type || '*';

let url =
(type === 'imdb' || (type === '*' && !IMDbID && title && year))?
`https://www.theimdbapi.org/api/find/movie?title=${ title }&year=${ year }`:
`https://api.themoviedb.org/3/search/movie?api_key=${ api.tvdb }&query=${ encodeURI(title) }&year=${ year }`:
(type === 'thetvdb' || APIID || (type === '*' && (IMDbID || (title && year))))?
(IMDbID)?
`https://api.themoviedb.org/3/find/${IMDbID}?api_key=bcb95f026f9a01ffa707fcff71900e94&external_source=imdb_id`:
`https://api.tvmaze.com/${ (APIID)? `shows/${APIID}`: `search/shows?q=${ title }`}`:
`https://api.themoviedb.org/3/find/${ IMDbID }?api_key=${ api.tvdb }&external_source=imdb_id`:
`https://api.tvmaze.com/${ (APIID)? `shows/${ APIID }`: `search/shows?q=${ encodeURI(title) }`}`:
null;

if(url === null) return 0;

await fetch(`${url}`)
await(meta? fetch(url/*, meta*/): fetch(url))
.then(response => {
return response.json();
})
Expand All @@ -130,11 +133,9 @@ async function getIDs({ title, year, type, IMDbID, APIID }) {
return json = objects;
});

json = ('movie_results' in json)?
json.movie_results:
('tv_results' in json)?
json.tv_results:
json;
if('results' in json) {
json = json.results;
}

if(json instanceof Array) {
let crush = (string) => string.toLowerCase().replace(/\&/g, 'and').replace(/\W+/g, '');
Expand All @@ -148,7 +149,7 @@ async function getIDs({ title, year, type, IMDbID, APIID }) {
found = (IMDbID === $data.show.externals.imdb || ($data.show.name === title && year === $data.show.premiered.slice(0, 4)))?
$data:
found;
//api.themoviedb.org/
//api.themoviedb.org/ < local
else if('movie_results' in $data || 'tv_results' in $data)
found = (DATA => {
for(var i = 0, f = !1, o = DATA.movie_results, l = o.length | 0; i < l; i++)
Expand All @@ -159,9 +160,9 @@ async function getIDs({ title, year, type, IMDbID, APIID }) {

return f? o: f;
})($data);
//theimdbapi.org/
//api.themoviedb.org/ < remote
else
found = (IMDbID === $data.imdb_id || $data.title === title && year === $data.year)?
found = ($data.title === title && year == $data.release_date.slice(0, 4))?
$data:
found;
}
Expand All @@ -175,7 +176,7 @@ async function getIDs({ title, year, type, IMDbID, APIID }) {
found = (crush($data.show.name) == crush(title))?
$data:
found;
//api.themoviedb.org/
//api.themoviedb.org/ < local
else if('movie_results' in $data || 'tv_results' in $data)
found = (DATA => {
for(var i = 0, f = !1, o = DATA.movie_results, l = o.length | 0; i < l; i++)
Expand All @@ -186,7 +187,7 @@ async function getIDs({ title, year, type, IMDbID, APIID }) {

return f? o: f;
})($data);
//theimdbapi.org/
//api.themoviedb.org/ < remote
else
found = (crush($data.title) == crush(title))?
$data:
Expand All @@ -196,6 +197,8 @@ async function getIDs({ title, year, type, IMDbID, APIID }) {
json = found;
}

console.log(json);

if('show' in json)
data = json.show.externals;
else if('imdb_id' in json)
Expand All @@ -222,6 +225,8 @@ function showNotification(state, text, timeout, callback) {
lastNotification = null;
}

callback = callback? callback: () => {};

let existingEl = document.querySelector('.web-to-plex-notification');
if (existingEl) {
document.body.removeChild(existingEl);
Expand All @@ -231,7 +236,7 @@ function showNotification(state, text, timeout, callback) {
el.classList.add('web-to-plex-notification');
el.onclick = () => {
el.remove();
callback && callback();
callback();
};

if (state === 'warning') {
Expand Down Expand Up @@ -328,10 +333,10 @@ function pushRadarrRequest(options) {
return showNotification('warning', 'Could not add to Radarr: ' + response.error),
console.error('Error adding to Radarr:', response.error, response.location);
} else if (response && response.success) {
let title = options.title.replace(/\&/g, 'and').replace(/\s+/g, '-').toLowerCase(),
let title = options.title.replace(/\&/g, 'and').replace(/\s+/g, '-').replace(/\W+/g, '').toLowerCase(),
TVDbID = options.TVDbID;

showNotification('info', 'Added movie to Radarr', 0, () => window.open(`${config.radarrURL}movies/${title}-${TVDbID}`, '_blank'));
showNotification('info', 'Added movie to Radarr', 0, () => window.open(`${config.radarrURL}movies/${TVDbID? `${title}-${TVDbID}`: '' }`, '_blank'));
} else {
showNotification('warning', 'Could not add to Radarr: Unknown Error');
}
Expand Down Expand Up @@ -363,7 +368,7 @@ function pushSonarrRequest(options) {
return showNotification('warning', 'Could not add to Sonarr: ' + response.error),
console.error('Error adding to Sonarr:', response.error, response.location);
} else if (response && response.success) {
let title = options.title.replace(/\&/g, 'and').replace(/\s+/g, '-').toLowerCase();
let title = options.title.replace(/\&/g, 'and').replace(/\s+/g, '-').replace(/\W+/g, '').toLowerCase();

showNotification('info', 'Added series to Sonarr', 0, () => window.open(`${config.sonarrURL}series/${title}`, '_blank'));
} else {
Expand Down Expand Up @@ -429,7 +434,7 @@ function modifyPlexButton(el, action, title, options) {
el.hrefs = dum;
el.download = `${options.title} (${options.year})`;
el.href = el.hrefs.slice(0, el.index = 1);
tl = (el.href.replace(/.*(?:\.(\w+))$/, '$1') || 'mp3').toUpperCase();
tl = (el.href.replace(/.*(?:\.(\w+))?$/, '$1') || 'mp3').toUpperCase();
el.textContent = el.textContent.replace(/\d+\/.+?$/, `${el.index}/${el.hrefs.length} (${tl})`);
el.hrefs = el.hrefs.join(delimeter);
};
Expand All @@ -449,7 +454,7 @@ function modifyPlexButton(el, action, title, options) {
}

el.href = hs.slice(el.index, 1);
tl = (el.href.replace(/.*(?:\.(\w+))$/, '$1') || 'mp3').toUpperCase();
tl = (el.href.replace(/.*(?:\.(\w+))?$/, '$1') || 'mp3').toUpperCase();
el.textContent = el.textContent.replace(/\d+\/.+?$/, `${++el.index}/${hs.length} (${tl})`);
});
break;
Expand Down Expand Up @@ -479,7 +484,7 @@ function modifyPlexButton(el, action, title, options) {
xhr.send(data);
} else {
el.href = '#';
el.textContent = 'Download ' + ty;
el.textContent = 'Get ' + ty;
el.classList.add('web-to-plex-button--downloader');
el.addEventListener('click', e => {
e.preventDefault();
Expand Down

0 comments on commit 3196f6d

Please sign in to comment.