commit 74e3601a716758b98a2e25f35f3cd9ed1fd2feb4
parent b2b324571557e265ef27a65b3b57439499af6f8e
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Sat, 1 Mar 2025 16:32:38 +0100
feat: show stream descriptions
Diffstat:
3 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/assets/js/twitch.js b/assets/js/twitch.js
@@ -44,6 +44,9 @@ async function tw2html_toggle_hidden(stream) {
tw2html_update_preview(stream);
}
+ // Fetch description text
+ var desc_txt = await tw2html_fetch_description(stream);
+
} else {
// Hide section
@@ -54,13 +57,32 @@ async function tw2html_toggle_hidden(stream) {
x.remove();
});
+ // Reset description text
+ var desc_txt = "";
+
}
+ // Insert description text into description field
+ section.querySelector("#description_" + stream).innerText = desc_txt;
+
// Mark Loading indicator as done
tw2html_loading_indicator(stream, status);
}
+async function tw2html_fetch_description(stream) {
+
+ // description response
+ const desc_resp = await fetch ("/lib/fetch_description.php?stream=" + stream);
+
+ // extract text of response
+ var desc_txt = desc_resp.text();
+
+ // return result
+ return(desc_txt);
+
+}
+
function tw2html_update_preview(stream) {
// Construct current time
@@ -217,7 +239,6 @@ function tw2html_loading_indicator(stream, status) {
}
-
function tw2html_reload() {
// Debug output
diff --git a/index.php b/index.php
@@ -133,6 +133,9 @@
<?php echo $stream; ?>
</h2>
+ <!-- Description line -->
+ <p id="description_<?php echo $stream;?>"></p>
+
<!-- player Container -->
<div class="player_container hidden"></div>
diff --git a/lib/fetch_description.php b/lib/fetch_description.php
@@ -0,0 +1,32 @@
+<?php
+
+/* Build URL to fetch the description from */
+$url = "https://www.twitch.tv/" . $_GET["stream"];
+
+/* Fetch HTML meta data (up until body starts) */
+$handle = fopen($url, "r");
+if ($handle) {
+ $html = stream_get_line($handle, 0, "<body>");
+ fclose($handle);
+} else {
+ $html = "";
+}
+
+/* Fetch description line */
+$desc_raw = preg_replace(
+ '/^.*(<meta[^>]+og:description[^>]+>).*/',
+ '\1',
+ $html
+);
+
+/* Extract description string */
+$desc = preg_replace(
+ '/^.*content="([^"]+)".*$/',
+ '\1',
+ $desc_raw
+);
+
+/* return the extracted description line */
+echo $desc;
+
+?>