|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\SupportedApps\Jellystat; |
| 4 | + |
| 5 | +class Jellystat extends \App\SupportedApps implements \App\EnhancedApps |
| 6 | +{ |
| 7 | + public $config; |
| 8 | + |
| 9 | + //protected $login_first = true; // Uncomment if api requests need to be authed first |
| 10 | + //protected $method = 'POST'; // Uncomment if requests to the API should be set by POST |
| 11 | + |
| 12 | + public function __construct() |
| 13 | + { |
| 14 | + //$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set |
| 15 | + } |
| 16 | + |
| 17 | + public function test() |
| 18 | + { |
| 19 | + $attrs = $this->getRequestAttrs(); |
| 20 | + $test = parent::appTest($this->url('stats/getLibraryCardStats'), $attrs); |
| 21 | + echo $test->status; |
| 22 | + } |
| 23 | + |
| 24 | + public function livestats() |
| 25 | + { |
| 26 | + $status = 'inactive'; |
| 27 | + $attrs = $this->getRequestAttrs(); |
| 28 | + |
| 29 | + $res = parent::execute($this->url('stats/getAllUserActivity'), $attrs); |
| 30 | + |
| 31 | + $results = json_decode($res->getBody()); |
| 32 | + |
| 33 | + $details = ["visiblestats" => []]; |
| 34 | + |
| 35 | + $newstat = new \stdClass(); |
| 36 | + $newstat->TotalPlays = new \stdClass(); |
| 37 | + $newstat->TotalPlays->title = 'Total Plays'; |
| 38 | + |
| 39 | + $newstat->TotalWatchTime = new \stdClass(); |
| 40 | + $newstat->TotalWatchTime->title = 'Total Watchtime'; |
| 41 | + |
| 42 | + if (isset($results[0])) { |
| 43 | + $result = $results[0]; |
| 44 | + $newstat->TotalPlays->value = $result->TotalPlays; |
| 45 | + $newstat->TotalWatchTime->value = $this->secondsToHoursMinutes($result->TotalWatchTime); |
| 46 | + } |
| 47 | + |
| 48 | + $details["visiblestats"][] = $newstat; |
| 49 | + |
| 50 | + return parent::getLiveStats($status, $details); |
| 51 | + } |
| 52 | + |
| 53 | + public function url($endpoint) |
| 54 | + { |
| 55 | + $api_url = parent::normaliseurl($this->config->url) . $endpoint; |
| 56 | + return $api_url; |
| 57 | + } |
| 58 | + |
| 59 | + private function getRequestAttrs() |
| 60 | + { |
| 61 | + $attrs = [ |
| 62 | + "headers" => [ |
| 63 | + |
| 64 | + "accept" => "application/json", |
| 65 | + "x-api-token" => $this->config->x_api_token, |
| 66 | + ], |
| 67 | + ]; |
| 68 | + |
| 69 | + return $attrs; |
| 70 | + } |
| 71 | + |
| 72 | + private function secondsToHoursMinutes($seconds) |
| 73 | + { |
| 74 | + |
| 75 | + // Calculate the hours |
| 76 | + $hours = floor($seconds / 3600); |
| 77 | + |
| 78 | + // Calculate the remaining seconds |
| 79 | + // into minutes |
| 80 | + $minutes = floor(($seconds % 3600) / 60); |
| 81 | + |
| 82 | + // Return the result as an |
| 83 | + // associative array |
| 84 | + return $hours . 'h ' . $minutes . 'min'; |
| 85 | + } |
| 86 | +} |
0 commit comments