Skip to content

Commit

Permalink
Fix #321: Use getJSONFromHTML forceLanguage parameter than `getJS…
Browse files Browse the repository at this point in the history
…ONFromHTMLForcingLanguage`
  • Loading branch information
Benjamin-Loison committed Oct 25, 2024
1 parent 261b4b0 commit c21362f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
20 changes: 10 additions & 10 deletions channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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'];
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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']));
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
Expand Down
19 changes: 9 additions & 10 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion playlists.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 5 additions & 5 deletions videos.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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']);
Expand All @@ -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'),
Expand All @@ -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'];
Expand Down

0 comments on commit c21362f

Please sign in to comment.