diff options
author | Jesús <heckyel@hyperbola.info> | 2019-05-23 16:41:39 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2019-05-23 16:41:39 -0500 |
commit | ace3b9c7be4b0b5f035867d40fda0d0c86352518 (patch) | |
tree | 605b583da045554158488bbce38b663273bfbb0f | |
parent | 55cd4077377b533a262cecdf54b2c40e3be46e84 (diff) | |
download | ytlibre-ace3b9c7be4b0b5f035867d40fda0d0c86352518.tar.lz ytlibre-ace3b9c7be4b0b5f035867d40fda0d0c86352518.tar.xz ytlibre-ace3b9c7be4b0b5f035867d40fda0d0c86352518.zip |
update regex and organize functions
-rw-r--r-- | tools/init.php | 49 | ||||
-rw-r--r-- | tools/processor.php | 33 |
2 files changed, 57 insertions, 25 deletions
diff --git a/tools/init.php b/tools/init.php index 2627811..162350c 100644 --- a/tools/init.php +++ b/tools/init.php @@ -1,12 +1,37 @@ <?php -function get($url) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $data = curl_exec($ch); - curl_close($ch); - return $data; +function url_exists( $baselink = NULL ) { + if( empty( $baselink ) ){ + return false; + } + + $ch = curl_init( $baselink ); + + // Set a timeout + curl_setopt( $ch, CURLOPT_TIMEOUT, 5 ); + curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 ); + + // Set NOBODY to true to make a HEAD-type request + curl_setopt( $ch, CURLOPT_NOBODY, true ); + // Allow follow redirects + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); + // Receive the response as string, not output + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + + $data = curl_exec( $ch ); + + // Get the response code + $httpcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); + // close connection + curl_close( $ch ); + + // Accept only answer 200 (Ok), 301 (permanent redirect) or 302 (temporary redirect) + $accepted_response = array( 200, 301, 302 ); + if( in_array( $httpcode, $accepted_response ) ) { + return true; + } else { + return false; + } } function secToDuration($seconds){ @@ -14,3 +39,13 @@ function secToDuration($seconds){ $seconds = ($seconds - ($minutes * 60)); return $minutes . ' minutes ' . $seconds . ' seconds'; } + +function bytes($a) { + $unim = array("","K","M","G","T","P"); + $c = 0; + while ($a>=1000) { + $c++; + $a = $a/1000; + } + return number_format($a,($c ? 2 : 0),",",".")." ".$unim[$c]; +} diff --git a/tools/processor.php b/tools/processor.php index 89a37f6..6c42399 100644 --- a/tools/processor.php +++ b/tools/processor.php @@ -7,17 +7,25 @@ if (empty($_GET['link'])){ $baselink = htmlspecialchars($_GET['link']); -$yturl = 'youtu.be/'; +$urlexists = url_exists( $baselink ); +if ($urlexists == true) { + + // Regex - filter URL id is match[5] + // Testing into → https://regexr.com/4ej96 + $rx = '/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be|invidio\.us|invidiou\.sh|invidious\.snopyta\.org))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/'; + preg_match($rx, $baselink, $match); + + // Testing if id → 11 characters + if (strlen($match[5]) < 12) { + $video_id = $match[5]; + } else { + header('Location: index.php'); + } -if (strpos($baselink,$yturl) !== false){ - $link = preg_replace('~^https?://youtu\.be/([a-z\d]+)$~i', 'https://www.youtube.com/watch?v=$1', $baselink); } else { - $link = $baselink; + header('Location: index.php'); } -parse_str($link, $urlData); -$video_id = array_values($urlData)[0]; - $video_string = file_get_contents("https://invidio.us/api/v1/videos/{$video_id}"); $video_info = json_decode($video_string); @@ -30,17 +38,6 @@ $videoChannel = $video_info->author; // Begin_ViewCount $extract_video_view = $video_info->viewCount; - -function bytes($a) { - $unim = array("","K","M","G","T","P"); - $c = 0; - while ($a>=1000) { - $c++; - $a = $a/1000; - } - return number_format($a,($c ? 2 : 0),",",".")." ".$unim[$c]; -} - $videoViews = bytes($extract_video_view); // End_ViewCount |