commit 3ececa373dd8ebdbb582f7f42cd6e0ce7203c996
parent 662aa3c36a67d9e80df387840bd384c71f677e9c
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Sat, 7 Dec 2024 11:59:02 +0100
feat: strip unnecessary parameters from profile image URL
Diffstat:
M | index.php | | | 42 | +++++++++++++++++++++++++----------------- |
1 file changed, 25 insertions(+), 17 deletions(-)
diff --git a/index.php b/index.php
@@ -21,28 +21,36 @@ function ytdl_status($video_id) {
}
function get_yt_profilepic($channel_id) {
+ // set fallback to NULL
$profile_pic_link = null;
- if (empty($profile_pic_link)) {
- $html = explode(
- PHP_EOL,
- file_get_contents("https://www.youtube.com/channel/" . $channel_id)
+ // fetch HTML for given channel ID
+ $html = explode(
+ PHP_EOL,
+ file_get_contents("https://www.youtube.com/channel/" . $channel_id)
+ );
+ // If HTML could be fetched, extract lines with probable profile picture
+ if (!empty($html)) {
+ $profile_pic_lines = preg_grep(
+ '/yt3\.googleusercontent\.com\//',
+ $html
);
- if (!empty($html)) {
- $profile_pic_lines = preg_grep(
- '/yt3\.googleusercontent\.com\//',
- $html
- );
- $profile_pic_links = preg_replace(
- '/^.*\"(https:\/\/yt3\.googleusercontent\.com\/[^\"]+).*$/',
- '${1}',
- $profile_pic_lines
+ $profile_pic_links = preg_replace(
+ '/^.*\"(https:\/\/yt3\.googleusercontent\.com\/[^\"]+).*$/',
+ '${1}',
+ $profile_pic_lines
+ );
+ // If probably pictures were found, get the first one and strip
+ // unnecessary parameters from the link
+ if (count($profile_pic_links) > 0) {
+ $key = array_keys($profile_pic_links)[0];
+ $profile_pic_link = preg_replace(
+ '/=.*$/',
+ ''
+ $profile_pic_links[$key]
);
- if (count($profile_pic_links) > 0) {
- $key = array_keys($profile_pic_links)[0];
- $profile_pic_link = $profile_pic_links[$key];
- }
}
}
+ // return the profile picture that was found (or null otherwise)
return $profile_pic_link;
}