From c21362f62f4fc9f99e46c9950f2e31e35faf7fb1 Mon Sep 17 00:00:00 2001 From: Benjamin Loison <12752145+Benjamin-Loison@users.noreply.github.com> Date: Sat, 26 Oct 2024 00:28:06 +0200 Subject: [PATCH] Fix #321: Use `getJSONFromHTML` `forceLanguage` parameter than `getJSONFromHTMLForcingLanguage` --- channels.php | 20 ++++++++++---------- common.php | 19 +++++++++---------- playlists.php | 2 +- videos.php | 10 +++++----- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/channels.php b/channels.php index 2545313..d2359c8 100644 --- a/channels.php +++ b/channels.php @@ -151,14 +151,14 @@ function getItem($id, $order, $continuationToken) $continuationTokenProvided = $continuationToken != ''; if ($options['status']) { - $result = getJSONFromHTMLForcingLanguage("https://www.youtube.com/channel/$id", true); + $result = getJSONFromHTML("https://www.youtube.com/channel/$id", forceLanguage: true, verifiesChannelRedirection: true); $status = $result['alerts'][0]['alertRenderer']['text']['simpleText']; $item['status'] = $status; } if ($options['upcomingEvents']) { $upcomingEvents = []; - $result = getJSONFromHTML("https://www.youtube.com/channel/$id", verifiesChannelRedirection: true); + $result = getJSONFromHTML("https://www.youtube.com/channel/$id", forceLanguage: true, verifiesChannelRedirection: true); $subItems = getTabs($result)[0]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['shelfRenderer']['content']['horizontalListRenderer']['items']; foreach ($subItems as $subItem) { $path = 'gridVideoRenderer/upcomingEventData'; @@ -175,7 +175,7 @@ function getItem($id, $order, $continuationToken) if ($options['shorts']) { if (!$continuationTokenProvided) { - $result = getJSONFromHTMLForcingLanguage("https://www.youtube.com/channel/$id/shorts", true); + $result = getJSONFromHTML("https://www.youtube.com/channel/$id/shorts", forceLanguage: true, verifiesChannelRedirection: true); $visitorData = getVisitorData($result); $tab = getTabByName($result, 'Shorts'); $tabRenderer = $tab['tabRenderer']; @@ -230,7 +230,7 @@ function getItem($id, $order, $continuationToken) if ($options['community']) { if (!$continuationTokenProvided) { - $result = getJSONFromHTMLForcingLanguage("https://www.youtube.com/channel/$id/community", true); + $result = getJSONFromHTML("https://www.youtube.com/channel/$id/community", forceLanguage: true, verifiesChannelRedirection: true); } else { $result = getContinuationJson($continuationToken); } @@ -258,7 +258,7 @@ function getItem($id, $order, $continuationToken) if ($options['channels']) { if (!$continuationTokenProvided) { - $result = getJSONFromHTMLForcingLanguage("https://www.youtube.com/channel/$id/channels", true); + $result = getJSONFromHTML("https://www.youtube.com/channel/$id/channels", forceLanguage: true, verifiesChannelRedirection: true); $tab = getTabByName($result, 'Channels'); $sectionListRenderer = $tab['tabRenderer']['content']['sectionListRenderer']; @@ -323,7 +323,7 @@ function getItem($id, $order, $continuationToken) } if ($options['about']) { - $result = getJSONFromHTMLForcingLanguage("https://www.youtube.com/channel/$id/about", true); + $result = getJSONFromHTML("https://www.youtube.com/channel/$id/about", forceLanguage: true, verifiesChannelRedirection: true); $c4TabbedHeaderRenderer = $result['header']['c4TabbedHeaderRenderer']; $item['countryChannelId'] = $c4TabbedHeaderRenderer['channelId']; @@ -366,7 +366,7 @@ function getItem($id, $order, $continuationToken) } if ($options['approval']) { - $result = getJSONFromHTMLForcingLanguage("https://www.youtube.com/channel/$id", true); + $result = getJSONFromHTML("https://www.youtube.com/channel/$id", forceLanguage: true, verifiesChannelRedirection: true); $item['approval'] = end(explode(', ', $result['header']['pageHeaderRenderer']['content']['pageHeaderViewModel']['title']['dynamicTextViewModel']['rendererContext']['accessibilityContext']['label'])); } @@ -385,7 +385,7 @@ function getItem($id, $order, $continuationToken) if ($options['playlists']) { if (!$continuationTokenProvided) { - $result = getJSONFromHTMLForcingLanguage("https://www.youtube.com/channel/$id/playlists", true); + $result = getJSONFromHTML("https://www.youtube.com/channel/$id/playlists", forceLanguage: true, verifiesChannelRedirection: true); $tab = getTabByName($result, 'Playlists'); if ($tab === null) { @@ -526,7 +526,7 @@ function getItem($id, $order, $continuationToken) if ($options['letsPlay']) { $letsPlay = []; - $result = getJSONFromHTMLForcingLanguage("https://www.youtube.com/channel/$id/letsplay"); + $result = getJSONFromHTML("https://www.youtube.com/channel/$id/letsplay", forceLanguage: true); $gridRendererItems = getTabByName($result, 'Let\'s play')['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['shelfRenderer']['content']['gridRenderer']['items']; foreach($gridRendererItems as $gridRendererItem) { @@ -602,7 +602,7 @@ function getVideos(&$item, $url, $getGridRendererItems, $continuationToken) { $videos = []; if ($continuationToken === '') { - $result = getJSONFromHTMLForcingLanguage($url); + $result = getJSONFromHTML($url, forceLanguage: true); $gridRendererItems = $getGridRendererItems($result); $visitorData = getVisitorData($result); } diff --git a/common.php b/common.php index f0ff085..4d31480 100644 --- a/common.php +++ b/common.php @@ -154,6 +154,15 @@ function getJSONStringFromHTML($html, $scriptVariable = '', $prefix = 'var ') function getJSONFromHTML($url, $opts = [], $scriptVariable = '', $prefix = 'var ', $forceLanguage = false, $verifiesChannelRedirection = false) { + if($forceLanguage) { + $HEADER = 'Accept-Language: en'; + if(!doesPathExist($opts, 'http/header')) { + $opts['http']['header'] = [$HEADER]; + } else { + array_push($opts['http']['header'], $HEADER); + } + } + $html = getRemote($url, $opts); $jsonStr = getJSONStringFromHTML($html, $scriptVariable, $prefix); $json = json_decode($jsonStr, true); @@ -171,16 +180,6 @@ function getJSONFromHTML($url, $opts = [], $scriptVariable = '', $prefix = 'var return $json; } - function getJSONFromHTMLForcingLanguage($url, $verifiesChannelRedirection = false) - { - $opts = [ - 'http' => [ - 'header' => ['Accept-Language: en'] - ] - ]; - return getJSONFromHTML($url, $opts, verifiesChannelRedirection: $verifiesChannelRedirection); - } - function checkRegex($regex, $str) { return preg_match("/^$regex$/", $str) === 1; diff --git a/playlists.php b/playlists.php index 652d0ea..79c022a 100644 --- a/playlists.php +++ b/playlists.php @@ -45,7 +45,7 @@ function getItem($id) { global $options; - $result = getJSONFromHTMLForcingLanguage("https://www.youtube.com/playlist?list=$id"); + $result = getJSONFromHTML("https://www.youtube.com/playlist?list=$id", forceLanguage: true); $item = [ 'kind' => 'youtube#playlist', diff --git a/videos.php b/videos.php index db185ea..7633806 100644 --- a/videos.php +++ b/videos.php @@ -245,7 +245,7 @@ function getItem($id) } if(isset($_GET['clipId'])) { - $json = getJSONFromHTMLForcingLanguage("https://www.youtube.com/clip/$id"); + $json = getJSONFromHTML("https://www.youtube.com/clip/$id", forceLanguage: true); if ($options['id']) { $videoId = $json['currentVideoEndpoint']['watchEndpoint']['videoId']; $item['videoId'] = $videoId; @@ -291,7 +291,7 @@ function getItem($id) } if ($options['mostReplayed']) { - $json = getJSONFromHTMLForcingLanguage("https://www.youtube.com/watch?v=$id"); + $json = getJSONFromHTML("https://www.youtube.com/watch?v=$id", forceLanguage: true); $mutations = $json['frameworkUpdates']['entityBatchUpdate']['mutations']; $commonJsonPath = 'payload/macroMarkersListEntity/markersList'; $jsonPath = "$commonJsonPath/markersDecoration"; @@ -400,7 +400,7 @@ function getItem($id) } if ($options['snippet']) { - $json = getJSONFromHTMLForcingLanguage("https://www.youtube.com/watch?v=$id"); + $json = getJSONFromHTML("https://www.youtube.com/watch?v=$id", forceLanguage: true); $contents = $json['contents']['twoColumnWatchNextResults']['results']['results']['contents']; // Note that `publishedAt` has a day only precision. $publishedAt = strtotime($contents[0]['videoPrimaryInfoRenderer']['dateText']['simpleText']); @@ -413,7 +413,7 @@ function getItem($id) } if ($options['statistics']) { - $json = getJSONFromHTMLForcingLanguage("https://www.youtube.com/watch?v=$id"); + $json = getJSONFromHTML("https://www.youtube.com/watch?v=$id", forceLanguage: true); preg_match('/like this video along with ([\d,]+) other people/', $json['contents']['twoColumnWatchNextResults']['results']['results']['contents'][0]['videoPrimaryInfoRenderer']['videoActions']['menuRenderer']['topLevelButtons'][0]['segmentedLikeDislikeButtonViewModel']['likeButtonViewModel']['likeButtonViewModel']['toggleButtonViewModel']['toggleButtonViewModel']['defaultButtonViewModel']['buttonViewModel']['accessibilityText'], $viewCount); $statistics = [ 'viewCount' => getIntValue($json['playerOverlays']['playerOverlayRenderer']['videoDetails']['playerOverlayVideoDetailsRenderer']['subtitle']['runs'][2]['text'], 'view'), @@ -423,7 +423,7 @@ function getItem($id) } if ($options['activity']) { - $json = getJSONFromHTMLForcingLanguage("https://www.youtube.com/watch?v=$id"); + $json = getJSONFromHTML("https://www.youtube.com/watch?v=$id", forceLanguage: true); $activity = $json['contents']['twoColumnWatchNextResults']['results']['results']['contents'][1]['videoSecondaryInfoRenderer']['metadataRowContainer']['metadataRowContainerRenderer']['rows'][0]['richMetadataRowRenderer']['contents'][0]['richMetadataRenderer']; $name = $activity['title']['simpleText']; $year = $activity['subtitle']['simpleText'];