Skip to content

Commit

Permalink
#47: Use " à la printf instead of '
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Loison committed Jan 7, 2023
1 parent 0fc0dc4 commit 4015f92
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 76 deletions.
2 changes: 1 addition & 1 deletion addKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'ignore_errors' => true,
]
];
$content = getJSON('https://www.googleapis.com/youtube/v3/videos?part=snippet&id=mWdFMNQBcjs&key=' . $key, $httpOptions);
$content = getJSON("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=mWdFMNQBcjs&key=$key", $httpOptions);
if ($content['items'][0]['snippet']['title'] === 'A public video') {
file_put_contents(KEYS_FILE, ($keysContent === '' ? '' : "\n") . $key, FILE_APPEND);
echo 'YouTube Data API v3 key added.';
Expand Down
24 changes: 12 additions & 12 deletions channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
$parts = explode(',', $part, count($realOptions));
foreach ($parts as $part) {
if (!in_array($part, $realOptions)) {
die('invalid part ' . $part);
die("invalid part $part");
} else {
$options[$part] = true;
}
Expand All @@ -36,7 +36,7 @@
"header" => 'Cookie: CONSENT=YES+'
]
];
$result = getJSONFromHTML('https://www.youtube.com/c/' . $forUsername . '/about', $opts);
$result = getJSONFromHTML("https://www.youtube.com/c/$forUsername/about", $opts);
$id = $result['header']['c4TabbedHeaderRenderer']['channelId'];
} else if (isset($_GET['id'])) {
$id = $_GET['id'];
Expand All @@ -48,7 +48,7 @@
if (!isHandle($handle)) {
die('invalid handle');
}
$result = getJSONFromHTML('https://www.youtube.com/@' . $handle);
$result = getJSONFromHTML("https://www.youtube.com/@$handle");
$id = $result['responseContext']['serviceTrackingParams'][0]['params'][6]['value'];
}
$continuationToken = '';
Expand Down Expand Up @@ -80,14 +80,14 @@ function getItem($id, $continuationToken)
$httpOptions = [
'http' => $http
];
$result = getJSONFromHTML('https://www.youtube.com/channel/' . $id, $httpOptions);
$result = getJSONFromHTML("https://www.youtube.com/channel/$id", $httpOptions);
$status = $result['alerts'][0]['alertRenderer']['text']['simpleText'];
$item['status'] = $status;
}

if ($options['premieres']) {
$premieres = [];
$result = getJSONFromHTML('https://www.youtube.com/channel/' . $id);
$result = getJSONFromHTML("https://www.youtube.com/channel/$id");
$subItems = $result['contents']['twoColumnBrowseResultsRenderer']['tabs'][0]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['shelfRenderer']['content']['horizontalListRenderer']['items'];
foreach ($subItems as $subItem) {
$subItem = $subItem['gridVideoRenderer'];
Expand All @@ -112,7 +112,7 @@ function getItem($id, $continuationToken)
$httpOptions = [
'http' => $http
];
$result = getJSONFromHTML('https://www.youtube.com/channel/' . $id . '/shorts', $httpOptions);
$result = getJSONFromHTML("https://www.youtube.com/channel/$id/shorts", $httpOptions);
$visitorData = $result['responseContext']['webResponseContextExtensionData']['ytConfigData']['visitorData'];
} else {
$continuationParts = explode(',', $continuationToken);
Expand All @@ -122,7 +122,7 @@ function getItem($id, $continuationToken)
$http = [
'header' => [
'Content-Type: application/json',
'X-Goog-EOM-Visitor-Id: ' . $visitorData
"X-Goog-EOM-Visitor-Id: $visitorData"
],
'method' => 'POST',
'content' => $rawData
Expand Down Expand Up @@ -163,7 +163,7 @@ function getItem($id, $continuationToken)
}
$item['shorts'] = $shorts;
if($reelShelfRendererItems != null && count($reelShelfRendererItems) > 48)
$item['nextPageToken'] = urldecode($reelShelfRendererItems[48]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token'] . ',' . $visitorData);
$item['nextPageToken'] = urldecode($reelShelfRendererItems[48]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token'] . ",$visitorData");
}

if ($options['community']) {
Expand All @@ -176,7 +176,7 @@ function getItem($id, $continuationToken)
'http' => $http
];

$result = getJSONFromHTML('https://www.youtube.com/channel/' . $id . '/community', $httpOptions);
$result = getJSONFromHTML("https://www.youtube.com/channel/$id/community", $httpOptions);
} else {
$rawData = '{"context":{"client":{"clientName":"WEB","clientVersion":"' . MUSIC_VERSION . '"}},"continuation":"' . $continuationToken . '"}';
$http = [
Expand Down Expand Up @@ -226,7 +226,7 @@ function getItem($id, $continuationToken)
'http' => $http
];

$result = getJSONFromHTML('https://www.youtube.com/channel/' . $id . '/channels', $httpOptions);
$result = getJSONFromHTML("https://www.youtube.com/channel/$id/channels", $httpOptions);
$sectionListRenderer = array_slice($result['contents']['twoColumnBrowseResultsRenderer']['tabs'], -3)[0]['tabRenderer']['content']['sectionListRenderer'];
$channels = [];
$channelsItems = $sectionListRenderer['contents'][0]['itemSectionRenderer']['contents'][0]['gridRenderer']['items'];
Expand Down Expand Up @@ -264,7 +264,7 @@ function getItem($id, $continuationToken)
'http' => $http
];

$result = getJSONFromHTML('https://www.youtube.com/channel/' . $id . '/about', $httpOptions);
$result = getJSONFromHTML("https://www.youtube.com/channel/$id/about", $httpOptions);

$resultCommon = array_slice($result['contents']['twoColumnBrowseResultsRenderer']['tabs'], -2)[0]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['channelAboutFullMetadataRenderer'];

Expand Down Expand Up @@ -313,7 +313,7 @@ function getItem($id, $continuationToken)
'http' => $http
];

$result = getJSONFromHTML('https://www.youtube.com/channel/' . $id, $httpOptions);
$result = getJSONFromHTML("https://www.youtube.com/channel/$id", $httpOptions);
$badgeTooltipPath = 'header/c4TabbedHeaderRenderer/badges/0/metadataBadgeRenderer/tooltip';
$item['approval'] = doesPathExist($result, $badgeTooltipPath) ? getValue($result, $badgeTooltipPath) : '';
}
Expand Down
4 changes: 2 additions & 2 deletions commentThreads.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$parts = explode(',', $part, count($realOptions));
foreach ($parts as $part) {
if (!in_array($part, $realOptions)) {
die('invalid part ' . $part);
die("invalid part $part");
} else {
$options[$part] = true;
}
Expand Down Expand Up @@ -58,7 +58,7 @@ function getAPI($videoId, $order, $continuationToken)
];
$result = getJSON('https://www.youtube.com/youtubei/v1/next?key=' . UI_KEY, $opts);
} else {
$result = getJSONFromHTML('https://www.youtube.com/watch?v=' . $videoId);
$result = getJSONFromHTML("https://www.youtube.com/watch?v=$videoId");
$continuationToken = ($order === 'time' ? $result['engagementPanels'][2]['engagementPanelSectionListRenderer']['header']['engagementPanelTitleHeaderRenderer']['menu']['sortFilterSubMenuRenderer']['subMenuItems'][1]['serviceEndpoint'] : end($result['contents']['twoColumnWatchNextResults']['results']['results']['contents'])['itemSectionRenderer']['contents'][0]['continuationItemRenderer']['continuationEndpoint'])['continuationCommand']['token'];
return getAPI($videoId, $order, $continuationToken);
}
Expand Down
14 changes: 7 additions & 7 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function getJSON($url, $opts = [])

function getJSONFromHTMLScriptPrefix($html, $scriptPrefix)
{
$html = explode(';</script>', explode('">' . $scriptPrefix, $html, 3)[1], 2)[0];
$html = explode(';</script>', explode("\">$scriptPrefix", $html, 3)[1], 2)[0];
return json_decode($html, true);
}

Expand All @@ -65,7 +65,7 @@ function getJSONStringFromHTML($html, $scriptVariable = '', $prefix = 'var ')
if ($scriptVariable === '') {
$scriptVariable = 'ytInitialData';
}
return explode(';</script>', explode('">' . $prefix . $scriptVariable . ' = ', $html, 3)[1], 2)[0];
return explode(';</script>', explode("\">$prefix$scriptVariable = ", $html, 3)[1], 2)[0];
}

function getJSONFromHTML($url, $opts = [], $scriptVariable = '', $prefix = 'var ')
Expand All @@ -77,7 +77,7 @@ function getJSONFromHTML($url, $opts = [], $scriptVariable = '', $prefix = 'var

function checkRegex($regex, $str)
{
return preg_match('/^' . $regex . '$/', $str) === 1;
return preg_match("/^$regex$/", $str) === 1;
}

function isContinuationToken($continuationToken)
Expand Down Expand Up @@ -178,12 +178,12 @@ function getValue($json, $path)

function getIntValue($unitCount, $unit)
{
$unitCount = str_replace(' ' . $unit . 's', '', $unitCount);
$unitCount = str_replace(' ' . $unit, '', $unitCount);
$unitCount = str_replace(" {$unit}s", '', $unitCount);
$unitCount = str_replace(" $unit", '', $unitCount);
$unitCount = str_replace('K', '*1000', $unitCount);
$unitCount = str_replace('M', '*1000000', $unitCount);
if(checkRegex('[0-9.*KM]+', $unitCount)) {
$unitCount = eval('return ' . $unitCount . ';');
$unitCount = eval("return $unitCount;");
}
return $unitCount;
}
Expand Down Expand Up @@ -211,7 +211,7 @@ function getCommunityPostFromContent($content)
} else {
$url = $navigationEndpoint['browseEndpoint']['canonicalBaseUrl'];
}
$contentTextItem['url'] = 'https://www.youtube.com' . $url;
$contentTextItem['url'] = "https://www.youtube.com$url";
}
}
array_push($contentText, $contentTextItem);
Expand Down
4 changes: 2 additions & 2 deletions community.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$parts = explode(',', $part, count($realOptions));
foreach ($parts as $part) {
if (!in_array($part, $realOptions)) {
die('invalid part ' . $part);
die("invalid part $part");
} else {
$options[$part] = true;
}
Expand All @@ -36,7 +36,7 @@ function getAPI($postId)
'http' => $http
];

$result = getJSONFromHTML('https://www.youtube.com/post/' . $postId, $options);
$result = getJSONFromHTML("https://www.youtube.com/post/$postId", $options);
$content = $result['contents']['twoColumnBrowseResultsRenderer']['tabs'][0]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0];
$post = getCommunityPostFromContent($content);

Expand Down
2 changes: 1 addition & 1 deletion constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

define('DOMAIN_NAME', $_SERVER['SERVER_NAME']);
$protocol = (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on')) ? 'https' : 'http';
define('WEBSITE_URL', $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
define('WEBSITE_URL', "$protocol://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
define('SUB_VERSION_STR', '.9999099');
define('KEYS_FILE', '/var/www/ytPrivate/keys.txt');

Expand Down
35 changes: 20 additions & 15 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function url($url, $name = '')
if ($name === '') {
$name = $url;
}
return '<a href="' . $url . '">' . $name . '</a>';
return "<a href=\"$url\">$name</a>";
}

function yt()
Expand All @@ -19,20 +19,20 @@ function yt()

function feature($feature)
{
$suburl = $feature[0];
$suburl = "$feature[0]/list";
$webpage = explode('/', $suburl, 2)[0];
$url = $feature[1];
$name = ucfirst(str_replace('/', ': ', $suburl));
echo 'Based on <a href="https://developers.google.com/youtube/v3/docs/' . $suburl . '">' . $name . '</a>: ' . url(WEBSITE_URL . $webpage . '?part=' . $url) . '<br/>';
echo "Based on <a href=\"https://developers.google.com/youtube/v3/docs/$suburl\">$name</a>: " . url(WEBSITE_URL . "$webpage?part=$url") . '<br/>';
}

$features = [['channels/list', 'status,premieres,shorts,community,channels,about,approval,playlists&forUsername=USERNAME&id=CHANNEL_ID&handle=HANDLE'],
['community/list', 'snippet&id=POST_ID'],
['commentThreads/list', 'snippet,replies&videoId=VIDEO_ID&order=relevance,time(&pageToken=PAGE_TOKEN)'],
['playlists/list', 'snippet,statistics&id=PLAYLIST_ID'],
['playlistItems/list', 'snippet&playlistId=PLAYLIST_ID(&pageToken=PAGE_TOKEN)'],
['search/list', 'id,snippet&q=QUERY&channelId=CHANNEL_ID&eventType=upcoming&hashTag=HASH_TAG&type=video&order=viewCount,relevance(&pageToken=PAGE_TOKEN)'],
['videos/list', 'id,status,contentDetails,music,short,impressions,musics,isPaidPromotion,isPremium,isMemberOnly,mostReplayed,qualities,chapters,isOriginal,isRestricted&id=VIDEO_ID&clipId=CLIP_ID&SAPISIDHASH=YOUR_SAPISIDHASH']];
$features = [['channels', 'status,premieres,shorts,community,channels,about,approval,playlists&forUsername=USERNAME&id=CHANNEL_ID&handle=HANDLE'],
['community', 'snippet&id=POST_ID'],
['commentThreads', 'snippet,replies&videoId=VIDEO_ID&order=relevance,time(&pageToken=PAGE_TOKEN)'],
['playlists', 'snippet,statistics&id=PLAYLIST_ID'],
['playlistItems', 'snippet&playlistId=PLAYLIST_ID(&pageToken=PAGE_TOKEN)'],
['search', 'id,snippet&q=QUERY&channelId=CHANNEL_ID&eventType=upcoming&hashTag=HASH_TAG&type=video&order=viewCount,relevance(&pageToken=PAGE_TOKEN)'],
['videos', 'id,status,contentDetails,music,short,impressions,musics,isPaidPromotion,isPremium,isMemberOnly,mostReplayed,qualities,chapters,isOriginal,isRestricted&id=VIDEO_ID&clipId=CLIP_ID&SAPISIDHASH=YOUR_SAPISIDHASH']];

?>

Expand All @@ -46,15 +46,20 @@ function feature($feature)
}

echo '<br/>';
echo url(WEBSITE_URL . 'lives' . '?part=' . 'donations&id=VIDEO_ID') . '<br/>';
echo url(WEBSITE_URL . 'liveChats' . '?part=' . 'snippet,participants&id=VIDEO_ID&time=TIME_MS') . '<br/>';

$features = [['lives', 'donations&id=VIDEO_ID'],
['liveChats', 'snippet,participants&id=VIDEO_ID&time=TIME_MS']];

foreach ($features as $feature) {
echo url(WEBSITE_URL . "$feature[0]?part=$feature[1]<br/>");
}

?>

<h2>Make <?php yt(); ?> request WITHOUT ANY KEY:</h2>

To make <strong>ANY <?php yt(); ?> request WITHOUT ANY KEY/USING YOUR QUOTA</strong>, you can use: <?php $noKey = 'noKey'/*used to be yt*/; echo url(WEBSITE_URL . $noKey . '/YOUR_REQUEST'); ?><br/>
For instance you can use: <?php $example = 'videos?part=snippet&id=VIDEO_ID'; echo url(WEBSITE_URL . $noKey . '/' . $example); ?> instead of <?php echo url('https://www.googleapis.com/youtube/v3/' . $example); ?><br/>
To make <strong>ANY <?php yt(); ?> request WITHOUT ANY KEY/USING YOUR QUOTA</strong>, you can use: <?php $noKey = 'noKey'/*used to be yt*/; echo url(WEBSITE_URL . "$noKey/YOUR_REQUEST"); ?><br/>
For instance you can use: <?php $example = 'videos?part=snippet&id=VIDEO_ID'; echo url(WEBSITE_URL . "$noKey/$example"); ?> instead of <?php echo url("https://www.googleapis.com/youtube/v3/$example"); ?><br/>
I may add in the future limitation per IP etc if the quota need to be better shared among the persons using this API.<br/>
<?php

Expand Down Expand Up @@ -96,7 +101,7 @@ function share() {

$hash = file_get_contents('.git/refs/heads/main');
if ($hash !== false) {
echo '<br/><br/>This instance uses version: <a href="https://github.com/Benjamin-Loison/YouTube-operational-API/commit/' . $hash . '">' . $hash . '</a>';
echo "<br/><br/>This instance uses version: <a href=\"https://github.com/Benjamin-Loison/YouTube-operational-API/commit/$hash\">$hash</a>";
}

?>
2 changes: 1 addition & 1 deletion keys.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- could add here stuff to add/remove key cf https://github.com/Benjamin-Loison/YouTube-operational-API/issues/18 -->
<!-- could also allow to share keys which have their daily quota spent -->
All keys have all worked at least once and some keys may have extended quota (more than 10 000).
All keys have all worked at least once and some keys may have extended quota (more than 10,000).
6 changes: 3 additions & 3 deletions liveChats.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$parts = explode(',', $part, count($realOptions));
foreach ($parts as $part) {
if (!in_array($part, $realOptions)) {
die('invalid part ' . $part);
die("invalid part $part");
} else {
$options[$part] = true;
}
Expand Down Expand Up @@ -43,7 +43,7 @@ function getItem($id)
{
global $options;

$result = getJSONFromHTML('https://www.youtube.com/watch?v=' . $id);
$result = getJSONFromHTML("https://www.youtube.com/watch?v=$id");
$continuation = $result['contents']['twoColumnWatchNextResults']['conversationBar']['liveChatRenderer']['continuations'][0]['reloadContinuationData']['continuation'];

$rawData = '{"context":{"client":{"clientName":"WEB","clientVersion":"' . MUSIC_VERSION . '"}},"continuation":"' . $continuation . '","currentPlayerState":{"playerOffsetMs":"' . $_GET['time'] . '"}}';
Expand Down Expand Up @@ -83,7 +83,7 @@ function getItem($id)
]
];

$result = getJSONFromHTML('https://www.youtube.com/live_chat?continuation=' . $continuation, $opts, 'window["ytInitialData"]', '');
$result = getJSONFromHTML("https://www.youtube.com/live_chat?continuation=$continuation", $opts, 'window["ytInitialData"]', '');
$participants = array_slice($result['continuationContents']['liveChatContinuation']['actions'], 1);
$item['participants'] = $participants;
}
Expand Down
6 changes: 3 additions & 3 deletions lives.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$parts = explode(',', $part, count($realOptions));
foreach ($parts as $part) {
if (!in_array($part, $realOptions)) {
die('invalid part ' . $part);
die("invalid part $part");
} else {
$options[$part] = true;
}
Expand All @@ -39,15 +39,15 @@ function getItem($id)
{
global $options;

$result = getJSONFromHTML('https://www.youtube.com/watch?v=' . $id);
$result = getJSONFromHTML("https://www.youtube.com/watch?v=$id");
$continuation = $result['contents']['twoColumnWatchNextResults']['conversationBar']['liveChatRenderer']['continuations'][0]['reloadContinuationData']['continuation'];

$opts = [
"http" => [
"user_agent" => USER_AGENT,
]
];
$html = getJSONFromHTML('https://www.youtube.com/live_chat?continuation=' . $continuation, $opts, 'window["ytInitialData"]', '');
$html = getJSONFromHTML("https://www.youtube.com/live_chat?continuation=$continuation", $opts, 'window["ytInitialData"]', '');

$item = [
'kind' => 'youtube#video',
Expand Down
4 changes: 2 additions & 2 deletions playlistItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function getAPI($playlistId, $continuationToken)
$http['header'] = 'Content-Type: application/json';
$http['content'] = $rawData;
} else {
$url = 'https://www.youtube.com/playlist?list=' . $playlistId;
$url = "https://www.youtube.com/playlist?list=$playlistId";
$http['header'] = ['Cookie: CONSENT=YES+', 'Accept-Language: en'];
}

Expand Down Expand Up @@ -90,7 +90,7 @@ function getAPI($playlistId, $continuationToken)
break;
}
}
$publishedAt = time() - eval('return ' . $publishedAtStr . ';');
$publishedAt = time() - eval("return $publishedAtStr;");
// the time is not perfectly accurate this way
$answerItem = [
'kind' => 'youtube#playlistItem',
Expand Down
Loading

1 comment on commit 4015f92

@Benjamin-Loison
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified that I haven't forgot any usage of ", by using:

grep -r --exclude-dir=tools/ ' \. ' | grep -vE '_VERSION|UI_KEY|WEBSITE_URL|USER_AGENT|"' | grep ' \. '

Please sign in to comment.