Browse Source

Emby Support

Cerothen 9 years ago
parent
commit
2ef84a134b
1 changed files with 133 additions and 1 deletions
  1. 133 1
      functions.php

+ 133 - 1
functions.php

@@ -1,5 +1,11 @@
 <?php
 
+function debug_out($variable, $die = false) {
+	$trace = debug_backtrace()[0];
+	echo '<pre style="background-color: #f2f2f2; border: 2px solid black; border-radius: 5px; padding: 5px; margin: 5px";>'.$trace['file'].':'.$trace['line']."\n\n".print_r($variable, true).'</pre>';
+	if ($die) { http_response_code(503); die(); }
+}
+
 function clean($strin) {
     $strout = null;
 
@@ -227,6 +233,132 @@ function get_browser_name() {
     
 }
 
+function resolveEmbyItem($address, $token, $item) {
+	// Static Height
+	$height = 150;
+	
+	// Get Item Details
+	$itemDetails = json_decode(file_get_contents($address.'/Items?Ids='.$item['Id'].'&Fields=Overview&api_key='.$token),true)['Items'][0];
+	
+	switch ($item['Type']) {
+		case 'Episode':
+			$title = $item['SeriesName'].': '.$item['Name'].' (Season '.$item['ParentIndexNumber'].': Episode '.$item['IndexNumber'].')';
+			$imageId = $itemDetails['SeriesId'];
+			$width = 100;
+			$image = 'season';
+			break;
+		case 'Music':
+			$title = $item['Name'];
+			$imageId = $itemDetails['AlbumId'];
+			$width = 150;
+			$image = 'music';
+			break;
+		default:
+			$title = $item['Name'];
+			$imageId = $item['Id'];
+			$width = 100;
+			$image = 'movie';
+	}
+	
+	// If No Overview
+	if (!isset($itemDetails['Overview'])) {
+		$itemDetails['Overview'] = '';
+	}
+	
+	// Assemble Item And Cache Into Array 
+	return '<div class="item"><a href="'.$address.'/web/itemdetails.html?id='.$item['Id'].'" target="_blank"><img alt="'.$item['Name'].'" class="carousel-image '.$image.'" src="image.php?source=emby&img='.$imageId.'&height='.$height.'&width='.$width.'"></a><div class="carousel-caption '.$image.'""><h4>'.$title.'</h4><small><em>'.$itemDetails['Overview'].'</em></small></div></div>';
+}
+
+function outputCarousel($header, $size, $type, $items) {
+	// If None Populate Empty Item
+	if (!count($items)) {
+		$items = array('<div class="item"><img alt="nada" class="carousel-image movie" src="images/nadaplaying.jpg"><div class="carousel-caption"><h4>Nothing To Show</h4><small><em>Get Some Stuff Going!</em></small></div></div>');
+	}
+	
+	// Set First As Active
+	$items[0] = preg_replace('/^<div class="item ?">/','<div class="item active">', $items[0]);
+	
+	// Add Buttons
+	$buttons = '';
+	if (count($items) > 1) {
+		$buttons = '
+			<a class="left carousel-control '.$type.'" href="#carousel-'.$type.'" role="button" data-slide="prev"><span class="fa fa-chevron-left" aria-hidden="true"></span><span class="sr-only">Previous</span></a>
+			<a class="right carousel-control '.$type.'" href="#carousel-'.$type.'" role="button" data-slide="next"><span class="fa fa-chevron-right" aria-hidden="true"></span><span class="sr-only">Next</span></a>';
+	}
+	
+	return '
+	<div class="col-lg-'.$size.'">
+		<h5 class="text-center">'.$header.'</h5>
+		<div id="carousel-'.$type.'" class="carousel slide box-shadow white-bg" data-ride="carousel"><div class="carousel-inner" role="listbox">
+			'.implode('',$items).'
+		</div>'.$buttons.'
+	</div></div>'; 
+}
+
+function getEmbyStreams($url, $port, $token, $size, $header) {
+    if (stripos($url, "http") === false) {
+        $url = "http://" . $url;
+    }
+    
+    if ($port !== "") { 
+		$url = $url . ":" . $port;
+	}
+    
+    $address = $url;
+	
+	$api = json_decode(file_get_contents($address.'/Sessions?api_key='.$token),true);
+	
+	$playingItems = array();
+	foreach($api as $key => $value) {
+		if (isset($value['NowPlayingItem'])) {
+			$playingItems[] = resolveEmbyItem($address, $token, $value['NowPlayingItem']);
+		}
+	}
+	
+	return outputCarousel($header, $size, 'streams', $playingItems);
+}
+
+function getEmbyRecent($url, $port, $type, $token, $size, $header) {
+    if (stripos($url, "http") === false) {
+        $url = "http://" . $url;
+    }
+    
+    if ($port !== "") { 
+		$url = $url . ":" . $port;
+	}
+    
+    $address = $url;
+	
+	// Resolve Types
+	switch ($type) {
+		case 'movie':
+			$embyTypeQuery = 'IncludeItemTypes=Movie&';
+			break;
+		case 'season':
+			$embyTypeQuery = 'IncludeItemTypes=Episode&';
+			break;
+		case 'album':
+			$embyTypeQuery = 'IncludeItemTypes=Music&';
+			break;
+		default:
+			$embyTypeQuery = '';
+	}
+	
+	// Get A User
+	$userId = json_decode(file_get_contents($address.'/Users?api_key='.$token),true)[0]['Id'];
+	
+	// Get the latest Items
+	$latest = json_decode(file_get_contents($address.'/Users/'.$userId.'/Items/Latest?'.$embyTypeQuery.'EnableImages=false&api_key='.$token),true);
+	
+	// For Each Item In Category
+	$items = array();
+	foreach ($latest as $k => $v) {
+		$items[] = resolveEmbyItem($address, $token, $v);
+	}
+	
+	return outputCarousel($header, $size, $type, $items);
+}
+
 function getPlexRecent($url, $port, $type, $token, $size, $header){
     
     $urlCheck = stripos($url, "http");
@@ -781,4 +913,4 @@ function getHeadphonesCalendar($url, $port, $key, $list){
     if ($i != 0){ return $gotCalendar; }
 
 }
-?>
+?>