Skip to content

Commit 211f934

Browse files
author
Lucas Araujo
committed
feat(Audiobookshelf): enhance stats display and add book count
- Rename "Total Playtime" to "Playtime" for brevity - Add book count to stats display - Simplify time format by removing seconds - Clean up constructor and unused code - Update API endpoint for testing
1 parent ff057fe commit 211f934

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

Audiobookshelf/Audiobookshelf.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,11 @@ class Audiobookshelf extends \App\SupportedApps implements \App\EnhancedApps
66
{
77
public $config;
88

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-
}
9+
public function __construct() {}
1610

1711
public function test()
1812
{
19-
$test = parent::appTest($this->url('status'));
13+
$test = parent::appTest($this->url('api/me'), $this->getAttrs());
2014
echo $test->status;
2115
}
2216

@@ -25,7 +19,15 @@ public function livestats()
2519
$status = 'inactive';
2620
$res = parent::execute($this->url('api/me/listening-stats'), $this->getAttrs());
2721
$details = json_decode($res->getBody());
28-
$data = ['totalTime' => $this->secondsToHoursMinutes($details->totalTime)];
22+
23+
if ($details->totalTime > 0) {
24+
$status = 'active';
25+
$data = [
26+
'totalTime' => $this->secondsToHoursMinutes($details->totalTime),
27+
'bookCount' => $this->get_book_count()
28+
];
29+
}
30+
2931
return parent::getLiveStats($status, $data);
3032
}
3133

@@ -35,6 +37,22 @@ public function url($endpoint)
3537
return $api_url;
3638
}
3739

40+
private function get_book_count()
41+
{
42+
$res = parent::execute($this->url('api/libraries'), $this->getAttrs());
43+
$libraries = json_decode($res->getBody())->libraries;
44+
45+
$book_count = 0;
46+
foreach ($libraries as $library) {
47+
$lib_id = $library->id;
48+
$res = parent::execute($this->url('api/libraries/' . $lib_id . '/items'), $this->getAttrs());
49+
$library_details = json_decode($res->getBody());
50+
$book_count += $library_details->total;
51+
}
52+
53+
return $book_count;
54+
}
55+
3856
private function getAttrs()
3957
{
4058
return [
@@ -46,12 +64,10 @@ private function getAttrs()
4664
];
4765
}
4866

49-
5067
private function secondsToHoursMinutes($seconds)
5168
{
5269
$hours = floor($seconds / 3600);
5370
$minutes = floor(($seconds % 3600) / 60);
54-
$return_seconds = floor($seconds % 60);
55-
return $hours . 'h ' . $minutes . 'min ' . $return_seconds . 'sec';
71+
return $hours . 'h ' . $minutes . 'm';
5672
}
5773
}

Audiobookshelf/livestats.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<ul class="livestats">
22
<li>
3-
<span class="title">Total Playtime</span>
3+
<span class="title">Playtime</span>
44
<strong>{!! $totalTime !!}</strong>
55
</li>
6+
<li>
7+
<span class="title"># Books</span>
8+
<strong>{!! $bookCount !!}</strong>
9+
</li>
610
</ul>

0 commit comments

Comments
 (0)