<?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, '/pin/')) {
        if($test) {
            var_dump($get_url);
        }
        
        $url_parts = parse_url($get_url);
        preg_match('/^\/?pin\/(.+--)?(\d+)\/?.*$/', $url_parts['path'], $output_array);
        $id = $output_array[2];
        
        if($test) {
            var_dump($id);
            //die;
        }
    }
    elseif (strpos($get_url, 'pin.it')) 
    {
        $ch = curl_init($get_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($ch);
        $expandedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        curl_close($ch);
        preg_match('/pin\/(\d+)\//', $expandedUrl, $matches);
        if (isset($matches[1])) $id = $matches[1];
    }
    
    if($id) 
    {
        $url = 'https://api.one-api.ir/pinterest/v1/pin/?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 = $serviceResultArray['result']['videos']['video_list']['V_720P']['url'] 
            ?? $serviceResultArray['result']['story_pin_data']['pages'][0]['blocks'][0]['video']['video_list']['V_EXP3']['url']
            ?? $serviceResultArray['result']['images']['orig']['url'] 
            ?? null;
            
        if($test) {
            var_dump($serviceResultArray['result']['videos']['video_list']['V_720P']['url'] );
            var_dump($serviceResultArray['result']['story_pin_data']['pages'][0]['blocks'][0]['video']['video_list']['V_EXP3']['url'] );
            var_dump($serviceResultArray['result']['images']['orig']['url']  );
            //die;
            
            var_dump($media);
        }
        $resultnext = [
            'number' => 0,
            0 => [
                'link' => $media,
                'caption' => $serviceResultArray['result']['title'] . "\r\n" . $serviceResultArray['result']['closeup_unified_description']
            ]
        ];
        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;
}
