| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004 |
- <?php
- /**
- * User Watch Statistics Homepage Plugin
- * Provides comprehensive user watching statistics from Plex/Emby/Jellyfin
- */
- trait HomepageUserWatchStats
- {
- public function userWatchStatsSettingsArray($infoOnly = false)
- {
- $homepageInformation = [
- 'name' => 'UserWatchStats',
- 'enabled' => true,
- 'image' => 'plugins/images/homepage/userWatchStats.png',
- 'category' => 'Media Server',
- 'settingsArray' => __FUNCTION__
- ];
- if ($infoOnly) {
- return $homepageInformation;
- }
- $homepageSettings = [
- 'debug' => true,
- 'settings' => [
- 'Enable' => [
- $this->settingsOption('enable', 'homepageUserWatchStatsEnabled'),
- $this->settingsOption('auth', 'homepageUserWatchStatsAuth'),
- ],
- 'Connection' => [
- $this->settingsOption('select', 'homepageUserWatchStatsService', ['label' => 'Media Server', 'options' => [
- ['name' => 'Plex (via Tautulli)', 'value' => 'plex'],
- ['name' => 'Emby', 'value' => 'emby'],
- ['name' => 'Jellyfin', 'value' => 'jellyfin']
- ]]),
- $this->settingsOption('url', 'userWatchStatsURL'),
- $this->settingsOption('token', 'userWatchStatsApikey'),
- $this->settingsOption('disable-cert-check', 'userWatchStatsDisableCertCheck'),
- $this->settingsOption('use-custom-certificate', 'userWatchStatsUseCustomCertificate'),
- ],
- 'Display Options' => [
- $this->settingsOption('number', 'homepageUserWatchStatsRefresh', ['label' => 'Auto-refresh Interval (minutes)', 'min' => 1, 'max' => 60]),
- $this->settingsOption('number', 'homepageUserWatchStatsDays', ['label' => 'Statistics Period (days)', 'min' => 1, 'max' => 365]),
- $this->settingsOption('switch', 'homepageUserWatchStatsCompactView', ['label' => 'Use Compact View']),
- $this->settingsOption('switch', 'homepageUserWatchStatsShowTopUsers', ['label' => 'Show Top Users']),
- $this->settingsOption('switch', 'homepageUserWatchStatsShowMostWatched', ['label' => 'Show Most Watched']),
- $this->settingsOption('switch', 'homepageUserWatchStatsShowRecentActivity', ['label' => 'Show Recent Activity']),
- $this->settingsOption('number', 'homepageUserWatchStatsMaxItems', ['label' => 'Maximum Items to Display', 'min' => 5, 'max' => 50]),
- ],
- 'Test Connection' => [
- $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
- $this->settingsOption('test', 'userWatchStats'),
- ]
- ]
- ];
- return array_merge($homepageInformation, $homepageSettings);
- }
- public function testConnectionUserWatchStats()
- {
- if (!$this->homepageItemPermissions($this->userWatchStatsHomepagePermissions('test'), true)) {
- return false;
- }
-
- $mediaServer = $this->config['homepageUserWatchStatsService'] ?? 'plex';
-
- // Get URL and token from plugin-specific config
- $url = $this->config['userWatchStatsURL'] ?? '';
- $token = $this->config['userWatchStatsApikey'] ?? '';
- $disableCert = $this->config['userWatchStatsDisableCertCheck'] ?? false;
- $customCert = $this->config['userWatchStatsUseCustomCertificate'] ?? false;
-
- if (empty($url) || empty($token)) {
- $serverName = ucfirst($mediaServer) . ($mediaServer === 'plex' ? ' (Tautulli)' : '');
- $this->setAPIResponse('error', $serverName . ' URL or API key not configured', 500);
- return false;
- }
-
- // Test the connection based on media server type
- try {
- $options = $this->requestOptions($url, null, $disableCert, $customCert);
-
- switch (strtolower($mediaServer)) {
- case 'plex':
- // Test Tautulli connection
- $testUrl = $this->qualifyURL($url) . '/api/v2?apikey=' . $token . '&cmd=get_server_info';
- $response = Requests::get($testUrl, [], $options);
- if ($response->success) {
- $data = json_decode($response->body, true);
- if (isset($data['response']['result']) && $data['response']['result'] === 'success') {
- $this->setAPIResponse('success', 'Successfully connected to Tautulli', 200);
- return true;
- }
- }
- break;
- case 'emby':
- // Test Emby connection
- $testUrl = $this->qualifyURL($url) . '/emby/System/Info?api_key=' . $token;
- $response = Requests::get($testUrl, [], $options);
- if ($response->success) {
- $data = json_decode($response->body, true);
- if (isset($data['ServerName'])) {
- $this->setAPIResponse('success', 'Successfully connected to Emby server: ' . $data['ServerName'], 200);
- return true;
- }
- }
- break;
- case 'jellyfin':
- // Test Jellyfin connection
- $testUrl = $this->qualifyURL($url) . '/System/Info?api_key=' . $token;
- $response = Requests::get($testUrl, [], $options);
- if ($response->success) {
- $data = json_decode($response->body, true);
- if (isset($data['ServerName'])) {
- $this->setAPIResponse('success', 'Successfully connected to Jellyfin server: ' . $data['ServerName'], 200);
- return true;
- }
- }
- break;
- }
-
- $this->setAPIResponse('error', 'Connection test failed - invalid response from server', 500);
- return false;
-
- } catch (Exception $e) {
- $this->setAPIResponse('error', 'Connection test failed: ' . $e->getMessage(), 500);
- return false;
- }
- }
- public function userWatchStatsHomepagePermissions($key = null)
- {
- $permissions = [
- 'test' => [
- 'enabled' => [
- 'homepageUserWatchStatsEnabled',
- ],
- 'auth' => [
- 'homepageUserWatchStatsAuth',
- ],
- 'not_empty' => [
- 'userWatchStatsURL',
- 'userWatchStatsApikey'
- ]
- ],
- 'main' => [
- 'enabled' => [
- 'homepageUserWatchStatsEnabled'
- ],
- 'auth' => [
- 'homepageUserWatchStatsAuth'
- ],
- 'not_empty' => [
- 'userWatchStatsURL',
- 'userWatchStatsApikey'
- ]
- ]
- ];
- return $this->homepageCheckKeyPermissions($key, $permissions);
- }
- public function homepageOrderUserWatchStats()
- {
- if ($this->homepageItemPermissions($this->userWatchStatsHomepagePermissions('main'))) {
- $refreshInterval = ($this->config['homepageUserWatchStatsRefresh'] ?? 5) * 60000; // Convert minutes to milliseconds
- $compactView = ($this->config['homepageUserWatchStatsCompactView'] ?? false) ? 'true' : 'false';
- $days = $this->config['homepageUserWatchStatsDays'] ?? 30;
- $maxItems = $this->config['homepageUserWatchStatsMaxItems'] ?? 10;
- $showTopUsers = ($this->config['homepageUserWatchStatsShowTopUsers'] ?? true) ? 'true' : 'false';
- $showMostWatched = ($this->config['homepageUserWatchStatsShowMostWatched'] ?? true) ? 'true' : 'false';
- $showRecentActivity = ($this->config['homepageUserWatchStatsShowRecentActivity'] ?? true) ? 'true' : 'false';
- return '
- <div id="' . __FUNCTION__ . '">
- <div class="white-box">
- <div class="white-box-header">
- <i class="fa fa-bar-chart"></i> User Watch Statistics
- <span class="pull-right">
- <small id="watchstats-last-update" class="text-muted"></small>
- <button class="btn btn-xs btn-primary" onclick="refreshUserWatchStats()" title="Refresh Data">
- <i class="fa fa-refresh" id="watchstats-refresh-icon"></i>
- </button>
- </span>
- </div>
- <div class="white-box-content">
- <div class="row" id="watchstats-content">
- <div class="col-lg-12 text-center">
- <i class="fa fa-spinner fa-spin"></i> Loading statistics...
- </div>
- </div>
- </div>
- </div>
- </div>
- <script>
- var watchStatsRefreshTimer;
- var watchStatsLastRefresh = 0;
- function refreshUserWatchStats() {
- var refreshIcon = $("#watchstats-refresh-icon");
- refreshIcon.addClass("fa-spin");
- // Show loading state
- $("#watchstats-content").html(\'<div class="col-lg-12 text-center"><i class="fa fa-spinner fa-spin"></i> Loading statistics...</div>\');
- // Load watch statistics
- getUserWatchStatsData()
- .always(function() {
- refreshIcon.removeClass("fa-spin");
- watchStatsLastRefresh = Date.now();
- updateWatchStatsLastRefreshTime();
- });
- }
- function updateWatchStatsLastRefreshTime() {
- if (watchStatsLastRefresh > 0) {
- var ago = Math.floor((Date.now() - watchStatsLastRefresh) / 1000);
- var timeText = ago < 60 ? ago + "s ago" : Math.floor(ago / 60) + "m ago";
- $("#watchstats-last-update").text("Updated " + timeText);
- }
- }
- function getUserWatchStatsData() {
- return organizrAPI2("GET", "api/v2/homepage/userWatchStats")
- .done(function(data) {
- if (data && data.response && data.response.result === "success" && data.response.data) {
- var stats = data.response.data;
- var html = "";
-
- // Display statistics period
- html += \'<div class="col-lg-12"><h4>Statistics for \' + (stats.period || "30 days") + \'</h4></div>\';
-
- // Show watch history (Movies & Shows)
- if (stats.watch_history && stats.watch_history.length > 0) {
- html += \'<div class="col-lg-12"><h5>Watch History</h5><table class="table table-striped table-condensed">\';
- html += \'<thead>\';
- html += \'<tr>\';
- html += \'<th>Title</th>\';
- html += \'<th>Type</th>\';
- html += \'<th>Play Count</th>\';
- html += \'<th>Runtime</th>\';
- html += \'</tr>\';
- html += \'</thead>\';
- html += \'<tbody>\';
- stats.watch_history.slice(0, 10).forEach(function(item) {
- html += \'<tr>\';
- html += \'<td>\' + (item.title || "Unknown Title") + \'</td>\';
- html += \'<td>\' + (item.type || "Unknown") + \'</td>\';
- html += \'<td>\' + (item.play_count || 0) + \'</td>\';
- html += \'<td>\' + (item.runtime || "Unknown") + \'</td>\';
- html += \'</tr>\';
- });
- html += \'</tbody>\';
- html += \'</table>\';
- html += \'</div>\';
- }
-
- // Show user stats (Emby users)
- if (stats.user_stats && stats.user_stats.length > 0) {
- html += \'<div class="col-lg-12"><h5>Server Users (\' + stats.user_stats.length + \' total)</h5><ul class="list-group">\';
- stats.user_stats.slice(0, 10).forEach(function(user) {
- var lastActivity = "Never";
- if (user.LastActivityDate && user.LastActivityDate !== "0001-01-01T00:00:00.0000000Z") {
- var activityDate = new Date(user.LastActivityDate);
- lastActivity = activityDate.toLocaleDateString();
- }
- var isAdmin = user.Policy && user.Policy.IsAdministrator ? " [Admin]" : "";
- var isDisabled = user.Policy && user.Policy.IsDisabled ? " [Disabled]" : "";
- html += \'<li class="list-group-item">\' + (user.Name || "Unknown User") + isAdmin + isDisabled + \' - Last Activity: \' + lastActivity + \'</li>\';
- });
- html += \'</ul></div>\';
- }
-
- // Show top users if enabled and has data
- if (' . $showTopUsers . ' && stats.top_users && stats.top_users.length > 0) {
- html += \'<div class="col-lg-6"><h5>Top Users</h5><ul class="list-group">\';
- stats.top_users.slice(0, 5).forEach(function(user) {
- html += \'<li class="list-group-item">\' + (user.friendly_name || user.username || "Unknown User") + \' - \' + (user.play_count || 0) + \' plays</li>\';
- });
- html += \'</ul></div>\';
- }
-
- // Show most watched if enabled and has data
- if (' . $showMostWatched . ' && stats.most_watched && stats.most_watched.length > 0) {
- html += \'<div class="col-lg-6"><h5>Most Watched</h5><ul class="list-group">\';
- stats.most_watched.slice(0, 5).forEach(function(item) {
- html += \'<li class="list-group-item">\' + (item.title || "Unknown Title") + \' - \' + (item.play_count || item.total_plays || 0) + \' plays</li>\';
- });
- html += \'</ul></div>\';
- }
-
- // Show recent activity if enabled and has data
- if (' . $showRecentActivity . ' && stats.recent_activity && stats.recent_activity.length > 0) {
- html += \'<div class="col-lg-12"><h5>Recent Activity</h5><ul class="list-group">\';
- stats.recent_activity.slice(0, 10).forEach(function(activity) {
- html += \'<li class="list-group-item">\' + (activity.title || "Unknown Title") + \' - \' + (activity.added_at || "Unknown Date") + \'</li>\';
- });
- html += \'</ul></div>\';
- }
-
- // Check if we have any data to display
- var hasUserStats = stats.user_stats && stats.user_stats.length > 0;
- var hasTopUsers = stats.top_users && stats.top_users.length > 0;
- var hasMostWatched = stats.most_watched && stats.most_watched.length > 0;
- var hasRecentActivity = stats.recent_activity && stats.recent_activity.length > 0;
- var hasWatchHistory = stats.watch_history && stats.watch_history.length > 0;
-
- if (!hasUserStats && !hasTopUsers && !hasMostWatched && !hasRecentActivity && !hasWatchHistory) {
- html += \'<div class="col-lg-12 text-center text-muted">No statistics available</div>\';
- }
-
- $("#watchstats-content").html(html);
- } else {
- $("#watchstats-content").html(\'<div class="col-lg-12 text-center text-danger">Failed to load statistics</div>\');
- }
- })
- .fail(function(xhr, status, error) {
- $("#watchstats-content").html(\'<div class="col-lg-12 text-center text-danger">Error loading statistics</div>\');
- });
- }
- // Auto-refresh setup
- var refreshInterval = ' . $refreshInterval . ';
- if (refreshInterval > 0) {
- watchStatsRefreshTimer = setInterval(function() {
- refreshUserWatchStats();
- }, refreshInterval);
- }
- // Update time display every 30 seconds
- setInterval(updateWatchStatsLastRefreshTime, 30000);
- // Initial load
- $(document).ready(function() {
- refreshUserWatchStats();
- });
- // Cleanup timer when page unloads
- $(window).on("beforeunload", function() {
- if (watchStatsRefreshTimer) {
- clearInterval(watchStatsRefreshTimer);
- }
- });
- </script>
- ';
- }
- }
- /**
- * Main function to get watch statistics
- */
- public function getUserWatchStats($options = null)
- {
- if (!$this->homepageItemPermissions($this->userWatchStatsHomepagePermissions('main'), true)) {
- $this->setAPIResponse('error', 'User not approved to view this homepage item - check plugin configuration', 401);
- return false;
- }
- try {
- $mediaServer = $this->config['homepageUserWatchStatsService'] ?? 'plex';
- $days = intval($this->config['homepageUserWatchStatsDays'] ?? 30);
-
- switch (strtolower($mediaServer)) {
- case 'plex':
- $stats = $this->getPlexWatchStats($days);
- break;
- case 'emby':
- $stats = $this->getEmbyWatchStats($days);
- break;
- case 'jellyfin':
- $stats = $this->getJellyfinWatchStats($days);
- break;
- default:
- $stats = $this->getPlexWatchStats($days);
- break;
- }
-
- if (isset($stats['error']) && $stats['error']) {
- $this->setAPIResponse('error', $stats['message'], 500);
- return false;
- }
-
- $this->setAPIResponse('success', 'Watch statistics retrieved successfully', 200, $stats);
- return true;
-
- } catch (Exception $e) {
- $this->writeLog('error', 'User Watch Stats Error: ' . $e->getMessage(), 'SYSTEM');
- $this->setAPIResponse('error', 'Failed to retrieve watch statistics: ' . $e->getMessage(), 500);
- return false;
- }
- }
- /**
- * Get Plex watch statistics via Tautulli API
- */
- private function getPlexWatchStats($days = 30)
- {
- $tautulliUrl = $this->config['userWatchStatsURL'] ?? '';
- $tautulliToken = $this->config['userWatchStatsApikey'] ?? '';
-
- if (empty($tautulliUrl) || empty($tautulliToken)) {
- return ['error' => true, 'message' => 'Tautulli URL or API key not configured'];
- }
- $endDate = date('Y-m-d');
- $startDate = date('Y-m-d', strtotime("-{$days} days"));
-
- $stats = [
- 'period' => "{$days} days",
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'most_watched' => $this->getTautulliMostWatched($tautulliUrl, $tautulliToken, $days),
- 'least_watched' => $this->getTautulliLeastWatched($tautulliUrl, $tautulliToken, $days),
- 'user_stats' => $this->getTautulliUserStats($tautulliUrl, $tautulliToken, $days),
- 'recent_activity' => $this->getTautulliRecentActivity($tautulliUrl, $tautulliToken),
- 'top_users' => $this->getTautulliTopUsers($tautulliUrl, $tautulliToken, $days)
- ];
- return $stats;
- }
- /**
- * Get most watched content from Tautulli
- */
- private function getTautulliMostWatched($url, $token, $days)
- {
- $apiURL = rtrim($url, '/') . '/api/v2?apikey=' . $token . '&cmd=get_home_stats&time_range=' . $days . '&stats_type=plays&stats_count=10';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- return $data['response']['data'] ?? [];
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Tautulli Most Watched Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get user statistics from Tautulli
- */
- private function getTautulliUserStats($url, $token, $days)
- {
- $apiURL = rtrim($url, '/') . '/api/v2?apikey=' . $token . '&cmd=get_user_watch_time_stats&time_range=' . $days;
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- return $data['response']['data'] ?? [];
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Tautulli User Stats Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get top users from Tautulli
- */
- private function getTautulliTopUsers($url, $token, $days)
- {
- $apiURL = rtrim($url, '/') . '/api/v2?apikey=' . $token . '&cmd=get_users&length=25';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- $users = $data['response']['data']['data'] ?? [];
-
- // Sort by play count
- usort($users, function($a, $b) {
- return ($b['play_count'] ?? 0) - ($a['play_count'] ?? 0);
- });
-
- return array_slice($users, 0, 10);
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Tautulli Top Users Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get recent activity from Tautulli
- */
- private function getTautulliRecentActivity($url, $token)
- {
- $apiURL = rtrim($url, '/') . '/api/v2?apikey=' . $token . '&cmd=get_recently_added&count=10';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- return $data['response']['data']['recently_added'] ?? [];
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Tautulli Recent Activity Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get least watched content (inverse of most watched)
- */
- private function getTautulliLeastWatched($url, $token, $days)
- {
- $apiURL = rtrim($url, '/') . '/api/v2?apikey=' . $token . '&cmd=get_libraries';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- $libraries = $data['response']['data'] ?? [];
-
- $leastWatched = [];
- foreach ($libraries as $library) {
- $libraryStats = $this->getTautulliLibraryStats($url, $token, $library['section_id'], $days);
- if (!empty($libraryStats)) {
- $leastWatched = array_merge($leastWatched, array_slice($libraryStats, -10));
- }
- }
-
- return $leastWatched;
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Tautulli Least Watched Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get library statistics for least watched calculation
- */
- private function getTautulliLibraryStats($url, $token, $sectionId, $days)
- {
- $apiURL = rtrim($url, '/') . '/api/v2?apikey=' . $token . '&cmd=get_library_media_info§ion_id=' . $sectionId . '&length=50&order_column=play_count&order_dir=asc';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- return $data['response']['data']['data'] ?? [];
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Tautulli Library Stats Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get Emby watch statistics
- */
- private function getEmbyWatchStats($days = 30)
- {
- $embyUrl = $this->config['userWatchStatsURL'] ?? '';
- $embyToken = $this->config['userWatchStatsApikey'] ?? '';
-
- if (empty($embyUrl) || empty($embyToken)) {
- return ['error' => true, 'message' => 'Emby URL or API key not configured'];
- }
- $endDate = date('Y-m-d');
- $startDate = date('Y-m-d', strtotime("-{$days} days"));
-
- $stats = [
- 'period' => "{$days} days",
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'most_watched' => $this->getEmbyMostWatched($embyUrl, $embyToken, $days),
- 'least_watched' => [], // Emby doesn't have a direct least watched API
- 'user_stats' => $this->getEmbyUserStats($embyUrl, $embyToken, $days),
- 'recent_activity' => $this->getEmbyRecentActivity($embyUrl, $embyToken),
- 'watch_history' => $this->getEmbyWatchHistory($embyUrl, $embyToken, $days),
- 'top_users' => $this->getEmbyTopUsers($embyUrl, $embyToken, $days)
- ];
- return $stats;
- }
- /**
- * Get Jellyfin watch statistics
- */
- private function getJellyfinWatchStats($days = 30)
- {
- $jellyfinUrl = $this->config['jellyfinURL'] ?? '';
- $jellyfinToken = $this->config['jellyfinToken'] ?? '';
-
- if (empty($jellyfinUrl) || empty($jellyfinToken)) {
- return ['error' => true, 'message' => 'Jellyfin URL or API key not configured'];
- }
- // Implement Jellyfin-specific statistics gathering
- return $this->getGenericMediaServerStats('jellyfin', $jellyfinUrl, $jellyfinToken, $days);
- }
- /**
- * Generic media server stats for Emby/Jellyfin
- */
- private function getGenericMediaServerStats($type, $url, $token, $days)
- {
- // Basic structure for now - can be expanded based on Emby/Jellyfin APIs
- return [
- 'period' => "{$days} days",
- 'start_date' => date('Y-m-d', strtotime("-{$days} days")),
- 'end_date' => date('Y-m-d'),
- 'message' => ucfirst($type) . ' statistics coming soon',
- 'most_watched' => [],
- 'least_watched' => [],
- 'user_stats' => [],
- 'recent_activity' => [],
- 'top_users' => []
- ];
- }
- /**
- * Format duration for display
- */
- private function formatDuration($seconds)
- {
- if ($seconds < 3600) {
- return gmdate('i:s', $seconds);
- } else {
- return gmdate('H:i:s', $seconds);
- }
- }
- /**
- * Get user avatar URL
- */
- private function getUserAvatar($userId, $mediaServer = 'plex')
- {
- switch ($mediaServer) {
- case 'plex':
- return $this->getPlexUserAvatar($userId);
- case 'emby':
- return $this->getEmbyUserAvatar($userId);
- case 'jellyfin':
- return $this->getJellyfinUserAvatar($userId);
- default:
- return '/plugins/images/organizr/user-bg.png';
- }
- }
- /**
- * Get Plex user avatar
- */
- private function getPlexUserAvatar($userId)
- {
- $tautulliUrl = $this->config['plexURL'] ?? '';
- $tautulliToken = $this->config['plexToken'] ?? '';
-
- if (empty($tautulliUrl) || empty($tautulliToken)) {
- return '/plugins/images/organizr/user-bg.png';
- }
- $apiURL = rtrim($tautulliUrl, '/') . '/api/v2?apikey=' . $tautulliToken . '&cmd=get_user_thumb&user_id=' . $userId;
- try {
- $options = $this->requestOptions($tautulliUrl, null, $this->config['plexDisableCertCheck'] ?? false, $this->config['plexUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
- if ($response->success) {
- $data = json_decode($response->body, true);
- return $data['response']['data']['thumb'] ?? '/plugins/images/organizr/user-bg.png';
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Tautulli User Avatar Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return '/plugins/images/organizr/user-bg.png';
- }
- /**
- * Get most watched content from Emby (server-wide statistics)
- */
- private function getEmbyMostWatched($url, $token, $days)
- {
- // Use activity logs to get most watched content
- $mostWatched = [];
-
- // Try to get activity data from Emby
- $apiURL = rtrim($url, '/') . '/emby/System/ActivityLog/Entries?api_key=' . $token .
- '&limit=1000&type=VideoPlayback';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- $items = $data['Items'] ?? [];
-
- $playCount = [];
- foreach ($items as $item) {
- if (isset($item['Name']) && isset($item['ShortOverview'])) {
- $title = $item['Name'];
- if (!isset($playCount[$title])) {
- $playCount[$title] = [
- 'title' => $title,
- 'total_plays' => 0,
- 'type' => 'Media',
- 'year' => null
- ];
- }
- $playCount[$title]['total_plays']++;
- }
- }
-
- // Sort by play count
- uasort($playCount, function($a, $b) {
- return $b['total_plays'] - $a['total_plays'];
- });
-
- $mostWatched = array_slice(array_values($playCount), 0, 10);
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Emby Most Watched Error: ' . $e->getMessage(), 'SYSTEM');
- }
-
- // If activity log doesn't work, try simpler approach
- if (empty($mostWatched)) {
- $mostWatched = $this->getEmbySimpleMostWatched($url, $token);
- }
- return $mostWatched;
- }
-
- /**
- * Simple approach to get most watched from Emby
- */
- private function getEmbySimpleMostWatched($url, $token)
- {
- $apiURL = rtrim($url, '/') . '/emby/Items?api_key=' . $token .
- '&Recursive=true&IncludeItemTypes=Movie,Episode&Fields=Name,RunTimeTicks,ProductionYear' .
- '&SortBy=DatePlayed&SortOrder=Descending&Limit=20';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- $items = $data['Items'] ?? [];
-
- $mostWatched = [];
- foreach ($items as $item) {
- $mostWatched[] = [
- 'title' => $item['Name'] ?? 'Unknown Title',
- 'total_plays' => 1, // We don't have actual play count, so assume 1
- 'runtime' => isset($item['RunTimeTicks']) ? $this->formatDuration($item['RunTimeTicks'] / 10000000) : 'Unknown',
- 'type' => $item['Type'] ?? 'Unknown',
- 'year' => $item['ProductionYear'] ?? null
- ];
- }
-
- return $mostWatched;
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Emby Simple Most Watched Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
-
- /**
- * Get total play count for a specific item across all users
- */
- private function getEmbyItemTotalPlays($url, $token, $itemId)
- {
- $totalPlays = 0;
- $users = $this->getEmbyUserStats($url, $token, 30);
-
- foreach ($users as $user) {
- if (isset($user['Policy']['IsDisabled']) && $user['Policy']['IsDisabled']) {
- continue;
- }
-
- $userId = $user['Id'];
- $userItemURL = rtrim($url, '/') . '/emby/Users/' . $userId . '/Items/' . $itemId . '?api_key=' . $token . '&Fields=UserData';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($userItemURL, [], $options);
-
- if ($response->success) {
- $itemData = json_decode($response->body, true);
- $totalPlays += $itemData['UserData']['PlayCount'] ?? 0;
- }
- } catch (Requests_Exception $e) {
- // Continue with other users if one fails
- continue;
- }
- }
-
- return $totalPlays;
- }
-
- /**
- * Get watched content for a specific user
- */
- private function getEmbyUserWatchedContent($url, $token, $userId, $days)
- {
- $apiURL = rtrim($url, '/') . '/emby/Users/' . $userId . '/Items?api_key=' . $token .
- '&Recursive=true&IncludeItemTypes=Movie,Episode&IsPlayed=true&Limit=50' .
- '&Fields=Name,PlayCount,UserData,RunTimeTicks,ProductionYear';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- $items = $data['Items'] ?? [];
- $watchedContent = [];
- foreach ($items as $item) {
- if (($item['UserData']['PlayCount'] ?? 0) > 0) {
- $watchedContent[] = [
- 'Id' => $item['Id'] ?? null,
- 'title' => $item['Name'] ?? 'Unknown Title',
- 'play_count' => $item['UserData']['PlayCount'] ?? 0,
- 'runtime' => $item['RunTimeTicks'] ? $this->formatDuration($item['RunTimeTicks'] / 10000000) : 'Unknown',
- 'type' => $item['Type'] ?? 'Unknown',
- 'year' => $item['ProductionYear'] ?? null
- ];
- }
- }
- return $watchedContent;
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Emby User Watched Content Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get user statistics from Emby
- */
- private function getEmbyUserStats($url, $token, $days)
- {
- $apiURL = rtrim($url, '/') . '/emby/Users?api_key=' . $token;
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- return $data ?? [];
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Emby User Stats Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get top users from Emby
- */
- private function getEmbyTopUsers($url, $token, $days)
- {
- $apiURL = rtrim($url, '/') . '/emby/Users?api_key=' . $token;
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- $users = $data ?? [];
-
- $topUsers = [];
- foreach ($users as $user) {
- if (!isset($user['Policy']['IsHidden']) || !$user['Policy']['IsHidden']) {
- $topUsers[] = [
- 'username' => $user['Name'] ?? 'Unknown User',
- 'friendly_name' => $user['Name'] ?? 'Unknown User',
- 'play_count' => 0, // Emby doesn't provide direct play count per user
- 'last_seen' => $user['LastActivityDate'] ?? null
- ];
- }
- }
-
- return array_slice($topUsers, 0, 10);
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Emby Top Users Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get recent activity from Emby
- */
- private function getEmbyRecentActivity($url, $token)
- {
- $apiURL = rtrim($url, '/') . '/emby/Items/Latest?api_key=' . $token . '&Limit=10&Recursive=true&IncludeItemTypes=Movie,Episode';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
-
- $recentActivity = [];
- foreach ($data as $item) {
- $recentActivity[] = [
- 'title' => $item['Name'] ?? 'Unknown Title',
- 'type' => $item['Type'] ?? 'Unknown',
- 'added_at' => $item['DateCreated'] ?? 'Unknown Date',
- 'year' => $item['ProductionYear'] ?? null
- ];
- }
-
- return $recentActivity;
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Emby Recent Activity Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get watch history from Emby
- */
- private function getEmbyWatchHistory($url, $token, $days)
- {
- // Try without date filter first to see if we get any played content
- $apiURL = rtrim($url, '/') . '/emby/UserLibrary/Items?api_key=' . $token .
- '&Recursive=true&IncludeItemTypes=Movie,Episode&IsPlayed=true&Limit=20' .
- '&Fields=Name,PlayCount,UserData,RunTimeTicks,DateCreated,UserDataLastPlayedDate';
-
- try {
- $options = $this->requestOptions($url, null, $this->config['userWatchStatsDisableCertCheck'] ?? false, $this->config['userWatchStatsUseCustomCertificate'] ?? false);
- $response = Requests::get($apiURL, [], $options);
-
- if ($response->success) {
- $data = json_decode($response->body, true);
- $items = $data['Items'] ?? [];
- $watchHistory = [];
- foreach ($items as $item) {
- $watchHistory[] = [
- 'title' => $item['Name'] ?? 'Unknown Title',
- 'play_count' => $item['UserData']['PlayCount'] ?? 0,
- 'runtime' => $item['RunTimeTicks'] ? $this->formatDuration($item['RunTimeTicks'] / 10000000) : 'Unknown',
- 'type' => $item['Type'] ?? 'Unknown',
- 'year' => $item['ProductionYear'] ?? null,
- 'last_played' => $item['UserData']['LastPlayedDate'] ?? 'Never'
- ];
- }
- return $watchHistory;
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Emby Watch History Error: ' . $e->getMessage(), 'SYSTEM');
- }
- return [];
- }
- /**
- * Get Emby user avatar
- */
- private function getEmbyUserAvatar($userId)
- {
- // Implement Emby avatar logic
- return '/plugins/images/organizr/user-bg.png';
- }
- /**
- * Get Jellyfin user avatar
- */
- private function getJellyfinUserAvatar($userId)
- {
- // Implement Jellyfin avatar logic
- return '/plugins/images/organizr/user-bg.png';
- }
- }
|