commit 8bd92f25b79950bc27256ec59b5a23f1273c97c0
parent 3a705c8eb55737ad7aecc7ccfc534d898dfe7012
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Tue, 22 Oct 2024 13:09:05 +0200
feat: sort channel list by channel name
Diffstat:
M | index.php | | | 78 | +++++++++++++++++++++++++++++++++++++++++------------------------------------- |
1 file changed, 41 insertions(+), 37 deletions(-)
diff --git a/index.php b/index.php
@@ -23,10 +23,14 @@
<?php
- /* Sort by Time */
- function cmp(array $a, array $b) {
- return $b["time"] - $a["time"];
+ /* Comparing function for sorting */
+ function compare_numeric(array $a, array $b, string $col) {
+ return $b[$col] - $a[$col];
}
+ function compare_string(array $a, array $b, string $col) {
+ return strcmp(strtolower($a[$col]), strtolower($b[$col]));
+ }
+
/* Initilise videos template */
$video_template = array(
@@ -38,12 +42,9 @@
"author" => array()
);
- /* Initilise channels array */
- $channels = array(
- "aid" => array(),
- "author" => array(),
- "error" => array()
- );
+ /* Initilise channels arrays */
+ $channels = array();
+ $channels_list = array();
/* Initilise videos array */
$videos = array();
@@ -52,36 +53,33 @@
// Cookie should have lowest priority (fallback), POST first (intend)
// GET arguments are used for one-time usage (will not set cookie)
if (!is_null($_COOKIE["channels"])) {
- $channels["aid"] = explode(",", $_COOKIE["channels"]);
+ $channels = explode(",", $_COOKIE["channels"]);
}
if (!is_null($_POST["channels"])) {
- $channels["aid"] = urldecode($_POST["channels"]);
- $channels["aid"] = preg_replace(
- '/[^A-Za-z0-9\-\_\,]+/',
- '',
- $channels["aid"]
- );
- $channels["aid"] = explode(",", $channels["aid"]);
+ $channels = urldecode($_POST["channels"]);
+ $channels = preg_replace('/[^A-Za-z0-9\-\_\,]+/', '', $channels);
+ $channels = explode(",", $channels);
}
if (!is_null($_GET["channels"])) {
- $channels["aid"] = explode(",", $_GET["channels"]);
+ $channels = explode(",", $_GET["channels"]);
}
- // Sort channels by alphabet and ensure each stream is unique
- $channels["aid"] = array_unique($channels["aid"]);
+ // Sort channels by alphabet and ensure each channel is unique
+ $channels = array_unique($channels);
/* refresh cookie */
if (is_null($_GET["channels"])) {
header(
"Set-Cookie: " .
- "channels=" . implode(",", $channels["aid"]) . ";" .
+ "channels=" . implode(",", $channels) . ";" .
"Max-Age=" . 31536000 . "; " . /* 60 x 60 x 24 x 365 = 1 year */
"Domain=" . $_SERVER["SERVER_NAME"] . "; " .
"SameSite=Strict;"
);
}
- foreach ($channels["aid"] as $channel) {
+ for ($i = 0; $i < count($channels); $i++) {
+ $channel = $channels[$i];
// Fetch Youtube XML Feed
$channel_xml = file(
@@ -90,9 +88,8 @@
/* Skip to next entry, if channel could not be found */
if ($channel_xml === false) {
- /* Add dummy values to channels array and mark as error */
- array_push($channels["author"], "");
- array_push($channels["error"], true);
+ /* Remove entry from channels list */
+ array_splice($channels, $i, 1);
/* Skip item within loop */
continue;
}
@@ -118,7 +115,9 @@
"&",
$channel_xml["entry"][0]["author"]["name"]
);
- array_push($channels["author"], $author);
+
+ /* Fill channels list array */
+ array_push($channels_list, array("aid" => $channel, "author" => $author));
// Process Entries
foreach ($channel_xml["entry"] as $entry) {
@@ -153,13 +152,17 @@
}
- /* Mark as successful in channels array */
- array_push($channels["error"], false);
-
}
- /* Sort videos array */
- usort($videos, 'cmp');
+ /* Sort videos and channels arrays */
+ function cmp_time(array $a, array $b) {
+ return compare_numeric($a, $b, "time");
+ }
+ function cmp_name(array $a, array $b) {
+ return compare_string($a, $b, "author");
+ }
+ usort($videos, "cmp_time");
+ usort($channels_list, "cmp_name");
?>
@@ -211,19 +214,20 @@
<input
name="channels"
type="text"
- value="<?php echo implode("," . PHP_EOL, $channels["aid"]); ?>"
+ value="<?php echo implode("," . PHP_EOL, $channels); ?>"
>
</details>
<div id="channels_list">
<?php
/* Draw Input fields for each channel ID */
- for ($i = 0; $i < count($channels["aid"]); $i++) {
+ for ($i = 0; $i < count($channels_list); $i++) {
?>
<label for="channel_<?php echo $i; ?>">
<?php
- if ($channels["author"][$i] != "") {
- echo "<a href=\"/?channels=" . $channels["aid"][$i] .
- "\" target=\"_blank\">" . $channels["author"][$i] . "</a>";
+ if ($channels_list[$i]["author"] != "") {
+ echo "<a href=\"/?channels=" . $channels_list[$i]["aid"] .
+ "\" target=\"_blank\">" . $channels_list[$i]["author"] .
+ "</a>";
} else {
echo "<mark class=\"error\">Error</mark>";
}
@@ -233,7 +237,7 @@
name="channel_<?php echo $i; ?>"
class="channels_input"
type="text"
- value="<?php echo $channels["aid"][$i]; ?>"
+ value="<?php echo $channels_list[$i]["aid"]; ?>"
oninput="yt2html_update_channels_list();"
>
<?php