Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions Audiobookshelf/Audiobookshelf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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 [
Expand All @@ -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';
}
}
6 changes: 5 additions & 1 deletion Audiobookshelf/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<ul class="livestats">
<li>
<span class="title">Total Playtime</span>
<span class="title">Playtime</span>
<strong>{!! $totalTime !!}</strong>
</li>
<li>
<span class="title"># Books</span>
<strong>{!! $bookCount !!}</strong>
</li>
</ul>