<?php
error_reporting(0);
header('Content-Type: application/json;');
$test = $_GET['test'] == 1;

$get_url = $_GET['url'];
if ($get_url == null) {
    $print_false_xd = array();
    $print_false_xd['res']['text'] = "لطفا لینک پست خودرا بگذارید مانند نمونه instadw.php?url=https://www.instagram.com/p/CroXqJMI104/?igshid=YmMyMTA2M2Y=";
    echo json_encode(['ok' => false, 'channel' => '@xdrobotch', 'writer' => 'XD TEAM',  'Results' => $print_false_xd], 448);
    die;
}

try {
    if($test) {
        //var_dump($get_url);
    }
        
    $ch = curl_init("https://one-api.ir/youtube/");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ['token' => '947528:64cac7a629424', 'action' => 'getvideoid', 'link' => $get_url]);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $end_url = curl_exec($ch);
    curl_close($ch);
    
    $serviceResultArray = json_decode($end_url,true);
    $id = $serviceResultArray['result'];
    
    //-------------------------
    
    if($test) {
        //var_dump($serviceResultArray);
        //var_dump($id);
    }
    
    $ch = curl_init("https://one-api.ir/youtube/");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ['token' => '947528:64cac7a629424', 'action' => 'fullvideo', 'id' => $id]);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $end_url = curl_exec($ch);
    curl_close($ch);
    
    $serviceResultArray = json_decode($end_url,true);
    if($test) {
        //var_dump($serviceResultArray);
        // var_dump($serviceResultArray['result']['fulltitle']);
        // var_dump($serviceResultArray['result']['description']);
        // var_dump($serviceResultArray['result']['formats'][0]['url']);
        // die;
    }

    $links = [];
    foreach($serviceResultArray['result']['formats'] as $format)
    {
        $links [] = [
            'text' => $format['resolution'] . ' (' . $format['format_note'] . ') ' . $format['ext'],
            'callback_data' => 'download: ' . $format['url']
        ];
    }
    
    $resultnext = [
            'number' => 0,
            0 => [
                'caption' => $serviceResultArray['result']['fulltitle'] . '\r\n' . $serviceResultArray['result']['description'],
                'links' => $links,
            ]
        ];
    if($test) {
        var_dump($resultnext);
        die;
    }
    
    echo json_encode(['ok' => true, 'channel' => '@xdrobotch', 'writer' => 'XD TEAM',  'Results' => $resultnext], 448);
    
} catch (Exception $ex) {
    $print_false_xd = array();
    $print_false_xd['res']['text'] = "خطا در خواندن اطلاعات ، لینک ورودی را چک کنید یا مجددا امتحان کنید";
    echo json_encode(['ok' => false, 'channel' => '@xdrobotch', 'writer' => 'XD TEAM',  'Results' => $print_false_xd], 448);
    exit;
}
