<?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 {
    $id = null;
    
    if (strpos($get_url, 'twitter.com') || strpos($get_url, 'x.com')) {
        if($test) {
            var_dump($get_url);
        }
        
        $url_parts = parse_url($get_url);
        preg_match('/^.*\/status\/(\d+).*$/', $url_parts['path'], $output_array);
        $id = $output_array[1];
        
        if($test) {
            var_dump($id);
            //die;
        }
        
        $url = 'https://api.one-api.ir/twitter/v1/tweet/?id=' . $id;
        $headers = 
        [
            "accept: application/json",
            "one-api-token: 947528:64cac7a629424"
        ];
    
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $response = curl_exec($ch);
        if (curl_errno($ch)) 
        {
            curl_close($ch);
            return null;
        }
        curl_close($ch);
        $serviceResultArray = json_decode($response, true);

        $media = null;
        foreach($serviceResultArray['result']['entities']['media']['0']['video_info']['variants'] as $index => $data)
        {
            if($test) {
                var_dump($data['url']);
                //die;
            }
            
            if($data['content_type'] == "video/mp4") {
                $media = $data['url'];
                break;
            }
        }

        $media = $media
            ?? $serviceResultArray['result']['entities']['media']['0']['media_url_https']
            ?? null;
            
        $caption = $serviceResultArray['result']['note_tweet']['text']
            ?? $serviceResultArray['result']['full_text']
            ?? '';
            
        if($test) {
            var_dump($serviceResultArray['result']['full_text']);
            var_dump($serviceResultArray['result']['note_tweet']['text']);
            var_dump($media);
            die;
        }
        $resultnext = [
            'number' => 0,
            0 => [
                'link' => $media,
                'caption' => $caption
            ]
        ];
        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;
}
