Skip to content

Commit

Permalink
Fix #57: Add ability to retrieve channel CHANNELS tab content, solv…
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Loison committed Oct 26, 2022
1 parent 6ea736d commit 5340e9e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
47 changes: 46 additions & 1 deletion channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

include_once 'common.php';

$realOptions = ['snippet', 'premieres', 'community', 'about'];
$realOptions = ['snippet', 'premieres', 'community', 'channels', 'about'];

// really necessary ?
foreach ($realOptions as $realOption) {
Expand Down Expand Up @@ -183,6 +183,51 @@ function getItem($id, $continuationToken)
$item['nextPageToken'] = str_replace('%3D', '=', $contents[10]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token']);
}

if ($options['channels']) {
$http = [
'header' => ['Accept-Language: en']
];

$options = [
'http' => $http
];

$result = getJSONFromHTML('https://www.youtube.com/channel/' . $id . '/channels', $options);
$sectionListRenderer = array_slice($result['contents']['twoColumnBrowseResultsRenderer']['tabs'], -3)[0]['tabRenderer']['content']['sectionListRenderer'];
$channels = [];
$channelsItems = $sectionListRenderer['contents'][0]['itemSectionRenderer']['contents'][0]['gridRenderer']['items'];
foreach($channelsItems as $channelItem) {
$gridChannelRenderer = $channelItem['gridChannelRenderer'];
$thumbnails = [];
foreach($gridChannelRenderer['thumbnail']['thumbnails'] as $thumbnail) {
$thumbnail['url'] = 'https://' . substr($thumbnail['url'], 2);
array_push($thumbnails, $thumbnail);
}
$subscriberCount = $gridChannelRenderer['subscriberCountText']['simpleText'];
$subscriberCount = str_replace(' subscribers', '', $subscriberCount);
// Have observed this case for the channel: https://www.youtube.com/channel/UCbOoDorgVGd-4vZdIrU4C1A
$subscriberCount = str_replace(' subscriber', '', $subscriberCount);
$subscriberCount = str_replace('K', '*1000', $subscriberCount);
$subscriberCount = str_replace('M', '*1000000', $subscriberCount);
if(checkRegex('[0-9.*KM]+', $subscriberCount)) {

This comment has been minimized.

Copy link
@Benjamin-Loison

Benjamin-Loison Oct 26, 2022

Author Owner

Could be more precise.

$subscriberCount = eval('return ' . $subscriberCount . ';');
}
$channel = [
'channelId' => $gridChannelRenderer['channelId'],
'title' => $gridChannelRenderer['title']['simpleText'],
'thumbnails' => $thumbnails,
'videoCount' => intval(str_replace(',', '', $gridChannelRenderer['videoCountText']['runs'][0]['text'])),
'subscriberCount' => $subscriberCount
];
array_push($channels, $channel);
}
$channels = [
'paratext' => $sectionListRenderer['subMenu']['channelSubMenuRenderer']['contentTypeSubMenuItems'][0]['title'],
'channels' => $channels
];
$item['channels'] = $channels;
}

if ($options['about']) {
$http = [
'header' => ['Accept-Language: en']
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function feature($feature)
}

// don't know if already written but making a table may be nice
$features = [['channels/list', 'snippet,premieres,community,about&forUsername=USERNAME&id=CHANNEL_ID'], // could use ',' instead of '&' to describe that `forUsername` and `id` have the same aim
$features = [['channels/list', 'snippet,premieres,community,channels,about&forUsername=USERNAME&id=CHANNEL_ID'], // could use ',' instead of '&' to describe that `forUsername` and `id` have the same aim
def
['commentThreads/list', 'snippet,replies&videoId=VIDEO_ID(&pageToken=PAGE_TOKEN)'],
['playlists/list', 'statistics&id=PLAYLIST_ID'],
Expand Down

0 comments on commit 5340e9e

Please sign in to comment.