<?php

error_reporting(E_ALL);
ini_set('display_errors', 0);

set_time_limit(60);
if(isset($_GET["url"]) AND $_GET["host"]=="facebook"){
	include("fb.core.php");
	$url = $_GET["url"];
	$fb = new fb($url);
	if($fb->fbvideoid($url) == null){
		sleep(1);
		echo getError("Invalid Facebook URL");
		exit;
	}
	$fb->get_url();
	if($fb->fb_sd == null){
		echo getError("Download link not found");
		exit;
	}



?>


<?php 
//$fb->id
$tt = $fb->fb_title;
$auth = explode('-',$tt);
$video_title = explode('-', explode('|', $tt)[0])[1];
$video_title=str_replace("'","^",$video_title);
$video_title=str_replace('"',"~",$video_title);
$channel_th = "https://adfree.video/no-image-1280.jpg";
$ht = $fb->fb_url;
$fbsrc = explode('href="', explode('" download', $ht)[0])[1];

$zJson720 = '{"src": "'. $fbsrc.
                        '","thumb": "'.$channel_th.
                        '","title": "'. $video_title.
                        '","author": "'.$auth[0].
                        '","authchannel":"'.$_GET['url'].
                        '","channelthumb": "'.$channel_th.
                        '" ,"btpage": "'.$_GET['url'].
                        '"}';
echo $zJson720;
?>
<?php
}elseif(isset($_GET["url"]) AND $_GET["host"]=="youtube"){
	include( "y.core.php" );
	$url = $_GET["url"];
	$yt  = new YTDownloader( $url );
	if ( $yt->get_id( $url ) == false ) {
		sleep( 1 );
		echo getError("Invalid Youtube URL");
		exit;
	}
	$links = $yt->getDownloadLinks();
	if ( $yt->error == true ) {
		echo getError("Failed to get links, please try again.");
		exit;
	}
?>


<?php


$ytpage = curl_get_file_contents($_GET['url']);

//$author_ch = explode('<meta itemprop="channelId" content="', explode('"><meta itemprop="videoId" content=', $ytpage)[0])[1];
$author_ch = explode('Person"><link itemprop="url" href="http://www.youtube.com/', explode('"><link itemprop="name" content="', $ytpage)[0])[1];

$video_ch = explode('"author":"', explode('","isLowLatencyLiveStream', $ytpage)[0])[1];
    if(strlen($video_ch)>50) {
        $video_ch = explode('"author":"', explode('","isPrivate', $ytpage)[0])[1];
    }
$channel_th = explode(',"width":88,"height":88},{"url":"', explode('","width":176,"height":176}', $ytpage)[0])[1];

                       



                  for ( $i = 0; $i < count( $links ); $i ++ ) {
                  	

                  	if ($links[$i]['quality']=="720p" && $links[$i]['type']=="MP4") {
                  	    
                  	    $ttl = str_replace("'","^",$yt->yt_title);
                  	    $ttl = str_replace('"',"~",$ttl);
                  	    
                  	    $video_ch = str_replace("'","^",$video_ch);
                  	    $video_ch = str_replace('"',"~",$video_ch);
                  	    $video_ch = str_replace("’","^",$video_ch);
                  	    
                  	    
                        
                        $zJson720 = '{"src": "'.
                        $links[$i]["url"].
                        '","thumb": "https://i.ytimg.com/vi/'.$yt->id.'/hqdefault.jpg","title":"'.$ttl.'","author": "'.$video_ch.
                        '","authchannel":"https://youtube.com/channel/'.$author_ch.'/videos","channelthumb": "'.$channel_th.'" ,"btpage": "'.$_GET['url'].
                        '"}'; 
                  	}
                  }
                
            
                for ( $i = 0; $i < count( $links ); $i ++ ) {
                  	
                  	$rez = $links[$i]['quality'];
                  	if ($links[$i]['quality']=="360p" && $links[$i]['type']=="MP4") {
                  	    $ttl = str_replace("'","^",$yt->yt_title);
                  	    $ttl = str_replace('"',"~",$ttl);
                  	    $video_ch = str_replace("'","^",$video_ch);
                  	    $video_ch = str_replace('"',"~",$video_ch);
                        
                        $zJson360 = '{"src": "'.
                        $links[$i]["url"].
                        '","thumb": "https://i.ytimg.com/vi/'.$yt->id.'/hqdefault.jpg","title":"'.$ttl.'","author": "'.$video_ch.
                        '","authchannel":"https://www.youtube.com/channel/'.$author_ch.'/videos","channelthumb": "'.$channel_th.'" ,"btpage": "'.$_GET['url'].
                        '"}'; 
                  	}
                }
            
            if($zJson720) {echo $zJson720; } else {echo $zJson360;}
?>



<?php
}elseif ( isset( $_GET["url"] ) AND $_GET["host"] == "twitter" ) {
include("twitter.core.php");
$url = $_GET["url"];
$twitter = new twitter($url);
$twitter->get_tweet();
if($twitter->error==true){
	die($twitter->msg);
}
?>

<?php


$auth = explode('https://twitter.com/', explode('/status', $url)[0])[1];
$auth_ch = 'https://twitter.com/'.$auth.'/';

				for($i=0;$i<count($twitter->links);$i++){
				if ($twitter->links[$i]["quality"]=="High Definition"){
	                $twitsrc= $twitter->links[$i]["url"];
                  }
                }


$video_title=str_replace("'","^",$twitter->title);
$video_title=str_replace('"',"~",$video_title);

$zJson720 = '{"src": "'. $twitsrc.
                        '","thumb": "'.$twitter->image.
                        '","title": "'. $video_title.
                        '","author": "'.$auth.
                        '","authchannel":"'.$auth_ch.
                        '","channelthumb": "'.$twitter->image.
                        '" ,"btpage": "'.$_GET['url'].
                        '"}';
echo $zJson720;

//$twitter->msg;

?>



<?php
} else{
	sleep(1);
	echo getError("<div id='download-error'>We do not support downloading from this site.</div>");
	exit;
}
function getError($msg){
	return '<div class="alert alert-danger alert-dismissible fade show">'.$msg.'</div>';
}
?> 