Skip to content

Commit

Permalink
Simplify regexs
Browse files Browse the repository at this point in the history
Source: https://www.w3schools.com/php/php_regex.asp *Regular Expression Patterns*

Verified exhaustiveness thanks to:

```bash
grep -rI '0-9' --exclude-dir=.git
```

Also check `a-z` and `A-Z`.
  • Loading branch information
Benjamin-Loison committed Sep 17, 2024
1 parent 2c04dd2 commit db05942
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
32 changes: 16 additions & 16 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function getJSONFromHTML($url, $opts = [], $scriptVariable = '', $prefix = 'var
if(doesPathExist($json, $redirectedToChannelIdPath))
{
$redirectedToChannelId = getValue($json, $redirectedToChannelIdPath);
$url = preg_replace('/[a-zA-Z0-9-_]{24}/', $redirectedToChannelId, $url);
$url = preg_replace('/[\w-_]{24}/', $redirectedToChannelId, $url);
// Does a redirection of redirection for a channel exist?
return getJSONFromHTML($url, $opts, $scriptVariable, $prefix, $forceLanguage, $verifiesChannelRedirection);
}
Expand All @@ -188,48 +188,48 @@ function checkRegex($regex, $str)

function isContinuationToken($continuationToken)
{
return checkRegex('[A-Za-z0-9=\-_]+', $continuationToken);
return checkRegex('[\w=\-_]+', $continuationToken);
}

function isContinuationTokenAndVisitorData($continuationTokenAndVisitorData)
{
return checkRegex('[A-Za-z0-9=_]+,[A-Za-z0-9=\-_]*', $continuationTokenAndVisitorData);
return checkRegex('[\w=_]+,[\w=\-_]*', $continuationTokenAndVisitorData);
}

function isPlaylistId($playlistId)
{
return checkRegex('[a-zA-Z0-9-_]+', $playlistId);
return checkRegex('[\w-_]+', $playlistId);
}

// what's minimal length ?
function isCId($cId)
{
return checkRegex('[a-zA-Z0-9]+', $cId);
return checkRegex('\w+', $cId);
}

function isUsername($username)
{
return checkRegex('[a-zA-Z0-9]+', $username);
return checkRegex('\w+', $username);
}

function isChannelId($channelId)
{
return checkRegex('UC[a-zA-Z0-9-_]{22}', $channelId);
return checkRegex('UC[\w-_]{22}', $channelId);
}

function isVideoId($videoId)
{
return checkRegex('[a-zA-Z0-9-_]{11}', $videoId);
return checkRegex('[\w-_]{11}', $videoId);
}

function isHashtag($hashtag)
{
return true; // checkRegex('[a-zA-Z0-9_]+', $hashtag); // 'é' is a valid hashtag for instance
return true; // checkRegex('[\w_]+', $hashtag); // 'é' is a valid hashtag for instance
}

function isSAPISIDHASH($SAPISIDHASH)
{
return checkRegex('[1-9][0-9]{9}_[a-f0-9]{40}', $SAPISIDHASH);
return checkRegex('[1-9]\d{9}_[a-f\d]{40}', $SAPISIDHASH);
}

function isQuery($q)
Expand All @@ -239,7 +239,7 @@ function isQuery($q)

function isClipId($clipId)
{
return checkRegex('Ug[a-zA-Z0-9-_]{34}', $clipId);
return checkRegex('Ug[\w-_]{34}', $clipId);
}

function isEventType($eventType)
Expand All @@ -254,22 +254,22 @@ function isPositiveInteger($s)

function isYouTubeDataAPIV3Key($youtubeDataAPIV3Key)
{
return checkRegex('AIzaSy[A-D][a-zA-Z0-9-_]{32}', $youtubeDataAPIV3Key);
return checkRegex('AIzaSy[A-D][\w-_]{32}', $youtubeDataAPIV3Key);
}

function isHandle($handle)
{
return checkRegex('@[a-zA-Z0-9-_.]{3,}', $handle);
return checkRegex('@[\w-_.]{3,}', $handle);
}

function isPostId($postId)
{
return (checkRegex('Ug[w-z][a-zA-Z0-9-_]{16}4AaABCQ', $postId) || checkRegex('Ugkx[a-zA-Z0-9-_]{32}', $postId));
return (checkRegex('Ug[w-z][\w-_]{16}4AaABCQ', $postId) || checkRegex('Ugkx[\w-_]{32}', $postId));
}

function isCommentId($commentId)
{
return checkRegex('Ug[w-z][a-zA-Z0-9-_]{16}4AaABAg(|.[a-zA-Z0-9-]{22})', $commentId);
return checkRegex('Ug[w-z][\w-_]{16}4AaABAg(|.[\w-]{22})', $commentId);
}

// Assume `$path !== ''`.
Expand Down Expand Up @@ -313,7 +313,7 @@ function getIntValue($unitCount, $unit = '')
$unitCount = str_replace('K', '*1_000', $unitCount);
$unitCount = str_replace('M', '*1_000_000', $unitCount);
$unitCount = str_replace('B', '*1_000_000_000', $unitCount);
if(checkRegex('[0-9_.*KMB]+', $unitCount)) {
if(checkRegex('[\d_.*KMB]+', $unitCount)) {
$unitCount = eval("return round($unitCount);");
}
return intval($unitCount);
Expand Down
2 changes: 1 addition & 1 deletion noKey/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function myDie($content)
if ($error['errors'][0]['domain'] !== 'youtube.quota') {
$message = $error['message'];
// As there are many different kind of errors other than the quota one, we could just proceed to a test verifying that the expected result is returned, as when adding a key.
if ($message === 'API key expired. Please renew the API key.' or str_ends_with($message, 'has been suspended.') or $message === 'API key not valid. Please pass a valid API key.' or $message === 'API Key not found. Please pass a valid API key.' or str_starts_with($message, 'YouTube Data API v3 has not been used in project ') or str_ends_with($message, 'are blocked.') or checkRegex('The provided API key has an IP address restriction\. The originating IP address of the call \(([0-9a-f:]{4,39}|[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})\) violates this restriction\.', $message)) {
if ($message === 'API key expired. Please renew the API key.' or str_ends_with($message, 'has been suspended.') or $message === 'API key not valid. Please pass a valid API key.' or $message === 'API Key not found. Please pass a valid API key.' or str_starts_with($message, 'YouTube Data API v3 has not been used in project ') or str_ends_with($message, 'are blocked.') or checkRegex('The provided API key has an IP address restriction\. The originating IP address of the call \(([\da-f:]{4,39}|\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})\) violates this restriction\.', $message)) {
// Removes this API key as it won't be useful anymore.
$newKeys = array_merge(array_slice($keys, $keysIndex + 1), array_slice($keys, 0, $keysIndex));
$toWrite = implode("\n", $newKeys);
Expand Down
2 changes: 1 addition & 1 deletion videos.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ function getItem($id)

if ($options['statistics']) {
$json = getJSONFromHTMLForcingLanguage("https://www.youtube.com/watch?v=$id");
preg_match('/like this video along with ([0-9,]+) other people/', $json['contents']['twoColumnWatchNextResults']['results']['results']['contents'][0]['videoPrimaryInfoRenderer']['videoActions']['menuRenderer']['topLevelButtons'][0]['segmentedLikeDislikeButtonViewModel']['likeButtonViewModel']['likeButtonViewModel']['toggleButtonViewModel']['toggleButtonViewModel']['defaultButtonViewModel']['buttonViewModel']['accessibilityText'], $viewCount);
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'),
'likeCount' => getIntValue($viewCount[1]),
Expand Down

0 comments on commit db05942

Please sign in to comment.