diff --git a/Audiobookshelf/Audiobookshelf.php b/Audiobookshelf/Audiobookshelf.php index 3994212503..3e75701d71 100644 --- a/Audiobookshelf/Audiobookshelf.php +++ b/Audiobookshelf/Audiobookshelf.php @@ -6,17 +6,12 @@ class Audiobookshelf extends \App\SupportedApps implements \App\EnhancedApps { public $config; - //protected $login_first = true; // Uncomment if api requests need to be authed first - //protected $method = 'POST'; // Uncomment if requests to the API should be set by POST - - public function __construct() - { - //$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set + public function __construct() { } public function test() { - $test = parent::appTest($this->url('status')); + $test = parent::appTest($this->url('api/me'), $this->getAttrs()); echo $test->status; } @@ -25,7 +20,15 @@ public function livestats() $status = 'inactive'; $res = parent::execute($this->url('api/me/listening-stats'), $this->getAttrs()); $details = json_decode($res->getBody()); - $data = ['totalTime' => $this->secondsToHoursMinutes($details->totalTime)]; + + if ($details->totalTime > 0) { + $status = 'active'; + $data = [ + 'totalTime' => $this->secondsToHoursMinutes($details->totalTime), + 'bookCount' => $this->getBookCount() + ]; + } + return parent::getLiveStats($status, $data); } @@ -35,6 +38,22 @@ public function url($endpoint) return $api_url; } + private function getBookCount() + { + $res = parent::execute($this->url('api/libraries'), $this->getAttrs()); + $libraries = json_decode($res->getBody())->libraries; + + $bookCount = 0; + foreach ($libraries as $library) { + $lib_id = $library->id; + $res = parent::execute($this->url('api/libraries/' . $lib_id . '/items'), $this->getAttrs()); + $library_details = json_decode($res->getBody()); + $bookCount += $library_details->total; + } + + return $bookCount; + } + private function getAttrs() { return [ @@ -46,12 +65,10 @@ private function getAttrs() ]; } - private function secondsToHoursMinutes($seconds) { $hours = floor($seconds / 3600); $minutes = floor(($seconds % 3600) / 60); - $return_seconds = floor($seconds % 60); - return $hours . 'h ' . $minutes . 'min ' . $return_seconds . 'sec'; + return $hours . 'h ' . $minutes . 'm'; } } diff --git a/Audiobookshelf/livestats.blade.php b/Audiobookshelf/livestats.blade.php index 8b3ede6dd4..bbfaddbfc5 100644 --- a/Audiobookshelf/livestats.blade.php +++ b/Audiobookshelf/livestats.blade.php @@ -1,6 +1,10 @@