Skip to content

Commit 51d2ae1

Browse files
lucaslraLucas Araujo
andauthored
feat(Audiobookshelf): enhance stats display and add book count (#868)
* 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 * style: fix constructor formatting in Audiobookshelf class * refactor: rename methods and variables to follow camelCase convention --------- Co-authored-by: Lucas Araujo <[email protected]>
1 parent 67c5d00 commit 51d2ae1

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

Audiobookshelf/Audiobookshelf.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@ 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
9+
public function __construct() {
1510
}
1611

1712
public function test()
1813
{
19-
$test = parent::appTest($this->url('status'));
14+
$test = parent::appTest($this->url('api/me'), $this->getAttrs());
2015
echo $test->status;
2116
}
2217

@@ -25,7 +20,15 @@ public function livestats()
2520
$status = 'inactive';
2621
$res = parent::execute($this->url('api/me/listening-stats'), $this->getAttrs());
2722
$details = json_decode($res->getBody());
28-
$data = ['totalTime' => $this->secondsToHoursMinutes($details->totalTime)];
23+
24+
if ($details->totalTime > 0) {
25+
$status = 'active';
26+
$data = [
27+
'totalTime' => $this->secondsToHoursMinutes($details->totalTime),
28+
'bookCount' => $this->getBookCount()
29+
];
30+
}
31+
2932
return parent::getLiveStats($status, $data);
3033
}
3134

@@ -35,6 +38,22 @@ public function url($endpoint)
3538
return $api_url;
3639
}
3740

41+
private function getBookCount()
42+
{
43+
$res = parent::execute($this->url('api/libraries'), $this->getAttrs());
44+
$libraries = json_decode($res->getBody())->libraries;
45+
46+
$bookCount = 0;
47+
foreach ($libraries as $library) {
48+
$lib_id = $library->id;
49+
$res = parent::execute($this->url('api/libraries/' . $lib_id . '/items'), $this->getAttrs());
50+
$library_details = json_decode($res->getBody());
51+
$bookCount += $library_details->total;
52+
}
53+
54+
return $bookCount;
55+
}
56+
3857
private function getAttrs()
3958
{
4059
return [
@@ -46,12 +65,10 @@ private function getAttrs()
4665
];
4766
}
4867

49-
5068
private function secondsToHoursMinutes($seconds)
5169
{
5270
$hours = floor($seconds / 3600);
5371
$minutes = floor(($seconds % 3600) / 60);
54-
$return_seconds = floor($seconds % 60);
55-
return $hours . 'h ' . $minutes . 'min ' . $return_seconds . 'sec';
72+
return $hours . 'h ' . $minutes . 'm';
5673
}
5774
}

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)