Selaa lähdekoodia

Cache images from tautulli/plex

Images now viewable outside of LAN
Henry Whitaker 6 vuotta sitten
vanhempi
commit
2952a82606

+ 20 - 0
api/functions/homepage-connect-functions.php

@@ -2439,12 +2439,32 @@ function getTautulli()
 			if ($homestats->success) {
 				$homestats = json_decode($homestats->body, true);
 				$api['homestats'] = $homestats['response'];
+				// Cache art & thumb for first result in each tautulli API result
+				$categories = [ 'top_movies', 'top_tv', 'popular_movies', 'popular_tv' ];
+				foreach($categories as $cat) {
+					$key = array_search($cat, array_column($api['homestats']['data'], 'stat_id'));
+					$img = $api['homestats']['data'][$key]['rows'][0];
+					cacheImage($GLOBALS['tautulliURL'] . 'pms_image_proxy?img=' . $img['art'], $img['title'] . '-art');
+					cacheImage($GLOBALS['tautulliURL'] . 'pms_image_proxy?img=' . $img['thumb'], $img['title'] . '-thumb');
+					$img['art'] = '/plugins/images/cache/' . $img['title'] . '-art.jpg';
+					$img['thumb'] = '/plugins/images/cache/' . $img['title'] . '-thumb.jpg';
+					$api['homestats']['data'][$key]['rows'][0] = $img;
+				}
+				// Cache the platform icon
+				$key = array_search('top_platforms', array_column($api['homestats']['data'], 'stat_id'));
+				$platform = $api['homestats']['data'][$key]['rows'][0]['platform_name'];
+				cacheImage($GLOBALS['tautulliURL'] . 'images/platforms/' . $platform . '.svg', 'tautulli-' . $platform, 'svg');
 			}
 			$libstatsUrl = $url . '&cmd=get_libraries';
 			$libstats = Requests::get($libstatsUrl, [], []);
 			if ($libstats->success) {
 				$libstats = json_decode($libstats->body, true);
 				$api['libstats'] = $libstats['response'];
+				$categories = [ 'movie.svg', 'show.svg', 'artist.png' ];
+				foreach($categories as $cat) {
+					$parts = explode('.', $cat);
+					cacheImage($GLOBALS['tautulliURL'] . 'images/libraries/' . $cat, 'tautulli-' . $parts[0], $parts[1]);
+				}
 			}
 			$api['options'] = [
 				'url' => $GLOBALS['tautulliURL'],

+ 7 - 7
api/functions/organizr-functions.php

@@ -2060,14 +2060,14 @@ function getImage()
 	}
 }
 
-function cacheImage($url, $name)
+function cacheImage($url, $name, $extension = 'jpg')
 {
-	$cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
-	if (!file_exists($cacheDirectory)) {
-		mkdir($cacheDirectory, 0777, true);
-	}
-	$cachefile = $cacheDirectory . $name . '.jpg';
-	@copy($url, $cachefile);
+    $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
+    if (!file_exists($cacheDirectory)) {
+        mkdir($cacheDirectory, 0777, true);
+    }
+    $cachefile = $cacheDirectory . $name . '.' . $extension;
+    @copy($url, $cachefile);
 }
 
 function downloader($array)

+ 9 - 11
js/functions.js

@@ -6502,15 +6502,13 @@ function buildTautulliItem(array){
                 <div class="card text-white mb-3 homepage-tautulli-card card-bg-colour">
                     <div class="card-body">
                         <div class="row" style="display: flex;">
-                            <div class="col-lg-4 col-md-4 col-sm-4 hidden-xs align-self-center">
-                                <img src="`+options['url']+`images/libraries/`+type;
-                                if(type == 'artist') {
-                                    card += `.png`;
-                                } else {
-                                    card += '.svg';
-                                }
+                            <div class="col-lg-4 col-md-4 col-sm-4 hidden-xs align-self-center">`;
+                            if(type == 'artist') {
+                                card += `<img src="/plugins/images/cache/tautulli-`+type+`.jpg" class="lib-icon" alt="library icon">`;
+                            } else {
+                                card += `<img src="/plugins/images/cache/tautulli-`+type+`.svg" class="lib-icon" alt="library icon">`;
+                            }
             card += `
-                                " class="lib-icon" alt="library icon">
                             </div>
                             <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12">
                                 <ol class="pl-2">`;
@@ -6557,7 +6555,7 @@ function buildTautulliItem(array){
                         if(stat !== 'top_users' && stat !== 'top_platforms') {
                             card += `
                             <div class="bg-img-cont">
-                                <img class="bg-img" src="`+options['url']+`pms_image_proxy?img=`+e['rows'][0]['art']+`" alt="background art">
+                                <img class="bg-img" src="`+e['rows'][0]['art']+`" alt="background art">
                             </div>
                             `;
                         }
@@ -6568,9 +6566,9 @@ function buildTautulliItem(array){
                                 if(stat == 'top_users') {
                                     card += `<img src="`+e['rows'][0]['user_thumb']+`" class="poster avatar" alt="user avatar">`;
                                 } else if(stat == 'top_platforms') {
-                                    card += `<img src="`+options['url']+`images/platforms/`+e['rows'][0]['platform_name']+`.svg" class="poster" alt="platform icon">`;
+                                    card += `<img src="/plugins/images/cache/tautulli-`+e['rows'][0]['platform_name']+`.svg" class="poster" alt="platform icon">`;
                                 } else {
-                                    card += `<img src="`+options['url']+`pms_image_proxy?img=`+e['rows'][0]['thumb']+`" class="poster" alt="movie poster">`;
+                                    card += `<img src="`+e['rows'][0]['thumb']+`" class="poster" alt="movie poster">`;
                                 }
                 card += `
                                 </div>