Skip to content

Commit

Permalink
feat: show time in human readable format; adds `seconds_to_human_read…
Browse files Browse the repository at this point in the history
…able();` function
  • Loading branch information
Ahwxorg committed Nov 6, 2024
1 parent f1c7ab2 commit 4d862ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion engines/invidious/video.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
require_once "misc/tools.php";
class VideoSearch extends EngineRequest {
protected $instance_url;
public function get_request_url() {
Expand Down Expand Up @@ -53,6 +54,7 @@ public static function print_results($results, $opts) {
$views = $result["views"] ?? '';
$date = $result["date"] ?? '';
$lengthSeconds = $result["lengthSeconds"] ?? '';
$length = seconds_to_human_readable($lengthSeconds) ?? '';
$thumbnail = "https://i.ytimg.com/vi/" . htmlspecialchars(explode("=", $url)[1]) . "/mqdefault.jpg" ?? '';

echo "<div class=\"text-result-wrapper\">";
Expand All @@ -63,7 +65,7 @@ public static function print_results($results, $opts) {
echo "<br>";
echo "<span>$uploader - $date - $views views</span>";
echo "<br>";
echo "<span>$lengthSeconds seconds</span>";
echo "<span>$length</span>";
echo "</a>";
echo "</div>";
}
Expand Down
28 changes: 28 additions & 0 deletions misc/tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ function get_base_url($url) {
}
}

function seconds_to_human_readable($inputSeconds) {
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;

$days = floor($inputSeconds / $secondsInADay);
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);
$timeParts = [];
$sections = [
'day' => (int)$days,
'hour' => (int)$hours,
'minute' => (int)$minutes,
'second' => (int)$seconds,
];

foreach ($sections as $name => $value){
if ($value > 0){
$timeParts[] = $value. ' '.$name.($value == 1 ? '' : 's');
}
}

return implode(', ', $timeParts);
}

function get_root_domain($url) {
return parse_url($url, PHP_URL_HOST);
Expand Down

0 comments on commit 4d862ce

Please sign in to comment.