Skip to content
Snippets Groups Projects
Commit efa19be4 authored by Jan Grewe's avatar Jan Grewe
Browse files

switch to SickBeard API instead of querying the DB

cache JSON
add loading indicator
parent 243d05bd
Branches
No related tags found
No related merge requests found
......@@ -2,3 +2,4 @@
/logo/*.png
/fanart/*.jpg
/poster/*.jpg
/cache/*.json
......@@ -3,21 +3,24 @@ include_once 'config.php';
if ($_GET['get'] == 'shows' && $_GET['limit'] && isset($_GET['offset'])) {
$sbdb = new PDO('sqlite:'.$sbPath.'/sickbeard.db');
$shows = $sbdb->query("SELECT tvdb_id AS id, show_name AS name, location FROM tv_shows ORDER BY show_name ASC LIMIT ".$_GET['limit']." OFFSET ".$_GET['offset'].";");
$output = array();
foreach($shows as $show) {
$rows = $sbdb->query("SELECT season, COUNT(episode_id) AS count FROM tv_episodes WHERE showid = '".$show['id']."' GROUP BY season ORDER BY season ASC");
$data = querySB('shows');
uasort($data, function($a, $b) {
return strcmp($a['show_name'], $b['show_name']);
});
$shows = array_slice($data, $_GET['offset'], $_GET['limit'], true);
foreach($shows as $key => $show) {
$dataShow = querySB('show', $key);
$dataSeasons = querySB('show.seasons', $key);
$seasons = array();
foreach($rows as $row) {
array_push($seasons, array("season" => $row['season'], "count" => $row['count']));
foreach($dataSeasons as $season => $episodes) {
array_push($seasons, array("season" => $season, "count" => count($episodes)));
}
array_push($output, array(
"id" => $show['id'],
"name" => $show['name'],
"folder" => str_replace($showsPath.'/', '', $show['location']),
"thumb" => cleanName($show['name']),
"id" => $key,
"name" => $show['show_name'],
"folder" => str_replace($showsPath.'/', '', $dataShow['location']),
"thumb" => cleanName($show['show_name']),
"seasons" => $seasons
));
unset($seasons);
......@@ -170,13 +173,12 @@ if ($_GET['get'] == 'logo' && (!empty($_GET['show']) || !empty($_GET['movie'])))
if ($_GET['get'] == 'episodes' && !empty($_GET['show']) && isset($_GET['season'])) {
$sbdb = new PDO('sqlite:'.$sbPath.'/sickbeard.db');
$episodes = $sbdb->query("SELECT episode, name, airdate, status FROM tv_episodes WHERE showid = '".$_GET['show']."' AND season = '".$_GET['season']."' ORDER BY episode ASC;");
$output = array();
foreach ($episodes as $episode) {
$date = new DateTime('0001-01-00');
$date->add(new DateInterval('P'.$episode['airdate'].'D'));
array_push($output, array("episode" => $episode['episode'], "name" => $episode['name'], "status" => (string)$episode['status'], "airdate" => $date->format('Y-m-d')));
if($json = file_get_contents('http://'.$sb['host'].':'.$sb['port'].$sb['path'].'/api/'.$sb['key'].'/?cmd=show.seasons&tvdbid='.$_GET['show'].'&season='.$_GET['season'])) {
$data = current(json_decode($json, true));
foreach ($data as $key => $episode) {
array_push($output, array("episode" => $key, "name" => $episode['name'], "status" => $episode['status'], "airdate" => $episode['airdate']));
}
}
echo json_encode($output);
die;
......@@ -184,14 +186,16 @@ if ($_GET['get'] == 'episodes' && !empty($_GET['show']) && isset($_GET['season']
if ($_GET['get'] == 'latest' && $_GET['type'] == 'shows') {
$sbdb = new PDO('sqlite:'.$sbPath.'/sickbeard.db');
$eps = $sbdb->query("SELECT s.show_name, ep.name, ep.episode, ep.season, ep.airdate FROM tv_episodes AS ep JOIN tv_shows AS s ON ep.showid=s.tvdb_id WHERE ep.status LIKE '%4' ORDER BY ep.airdate DESC LIMIT 10;");
//$eps = $sbdb->query("SELECT s.show_name, ep.name, ep.episode, ep.season, ep.airdate FROM tv_episodes AS ep JOIN tv_shows AS s ON ep.showid=s.tvdb_id WHERE ep.status LIKE '%4' ORDER BY ep.airdate DESC LIMIT 10;");
$eps = querySB('history');
$output = array();
foreach ($eps as $ep) {
$date = new DateTime('0001-01-00');
$date->add(new DateInterval('P'.$ep['airdate'].'D'));
array_push($output, array("show" => $ep['show_name'], "episode" => "S".str_pad($ep['season'], 2, '0', STR_PAD_LEFT)."E".str_pad($ep['episode'], 2, '0', STR_PAD_LEFT), "name" => $ep['name'], "airdate" => $date->format('Y-m-d')));
if($ep['status'] == 'Downloaded') {
array_push($output, array("show" => $ep['show_name'], "episode" => "S".str_pad($ep['season'], 2, '0', STR_PAD_LEFT)."E".str_pad($ep['episode'], 2, '0', STR_PAD_LEFT), "airdate" => $ep['date']));
}
}
$output = array_slice($output, 0, 10);
echo json_encode($output);
die;
}
......@@ -237,4 +241,19 @@ function get_absolute_path($path) {
return implode(DIRECTORY_SEPARATOR, $absolutes);
}
function querySB($cmd, $id = '', $season = '') {
global $sb, $cacheTTL;
if($id == '') {
$cache = './cache/'.$cmd.'.json';
}else{
$cache = './cache/'.$cmd.'-'.$id.'.json';
}
if (!file_exists($cache) || filemtime($cache) < time()-$cacheTTL) {
file_put_contents($cache, file_get_contents('http://'.$sb['host'].':'.$sb['port'].$sb['path'].'/api/'.$sb['key'].'/?cmd='.$cmd.'&tvdbid='.$id.'&season='.$season));
}
$json = file_get_contents($cache);
$data = current(json_decode($json, true));
return $data;
}
?>
\ No newline at end of file
<?php
$sbPath = '/opt/sickbeard';
$cpPath = '/opt/sickbeard';
$cpPath = '/opt/couchpotato';
$sb['host'] = 'localhost';
$sb['post'] = '8080';
$sb['path'] = '/';
$sb['key'] = 'YOUR-SICKBEARD-API-KEY';
$sb['ssl'] = false;
$showsPath = '/mnt/storage/media/tv';
$moviesPath = '/mnt/storage/media/movies';
$cacheTTL = 3600;
?>
\ No newline at end of file
......@@ -88,6 +88,10 @@ div.epLabel {
}
#loading {
display: none;
}
.jcarousel-wrapper {
margin: 20px auto;
position: relative;
......
img/loading.gif

10.6 KiB

img/loading_dark.gif

10.6 KiB

......@@ -91,6 +91,15 @@
<div class="content" id="movies"></div>
<div class="panel panel-default" id="loading">
<div class="panel-heading text-center">
<img src="img/loading.gif" alt="Loading..." />
</div>
<div class="panel-body text-center">
<img src="img/loading_dark.gif" alt="Loading..." />
</div>
</div>
</div> <!-- /container -->
......
......@@ -58,7 +58,7 @@ function getLatest(type) {
$.each(data, function (key, ep) {
var ulLatestContainer = $("#latest"+type);
if(type == 'Shows') {
var liLatestItem = $('<li><img src="api.php?get=poster&show='+escape(ep.show)+'" /><div class="epLabel">'+ep.show+'<br />'+ep.episode+'<br />'+ep.name+'<br />'+ep.airdate+'</div></li>');
var liLatestItem = $('<li><img src="api.php?get=poster&show='+escape(ep.show)+'" /><div class="epLabel">'+ep.show+'<br />'+ep.episode+'<br />'+ep.airdate+'</div></li>');
}else{
var liLatestItem = $('<li><img src="api.php?get=poster&movie='+escape(ep.movie)+'" /><div class="epLabel">'+ep.movie+'</div></li>');
}
......@@ -110,6 +110,7 @@ function getLatest(type) {
}
function getShows() {
$("#loading").show();
$.getJSON('api.php', {
'get': 'shows',
'limit': showLimit.toString(),
......@@ -175,6 +176,7 @@ function getShows() {
if(i > 0) {
loadShows = true;
}
$("#loading").hide();
}
);
}
......@@ -189,22 +191,21 @@ function getEpisodes(show, season) {
liSeason.find("table.episodes").remove();
var olEpisodes = $('<table class="table table-bordered table-condensed episodes"></table>');
$.each(data, function (key, episode) {
console.log(episode.status.substr(-1, 1));
var status;
switch(episode.status.substr(-1, 1)) {
case '5': // ignored
switch(episode.status) {
case 'Ignored':
status = "info";
break;
case '4': // downloaded
case 'Downloaded':
status = "success";
break;
case '3': // wanted
case 'Wanted':
status = "danger";
break;
case '2': // snatched
case 'Snatched':
status = "warning";
break;
case '1': // unaired
case 'Unaired':
status = "";
break;
default:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment