<?php

class YahooService{

    private $site = '';
    private $parser = '';

    public function __construct($site, $parser)
    {
        $this->site = $site;
        $this->parser = $parser;
    }

    public function get()
    {
        $yahooData = [
            'yahoo_index_count' => 0,
            'yahoo_backlink_count' => 0,
            'yahoo_image_count' => 0
        ];

        //$dom = file_get_contents('http://www.burakvanli.com/yahoo.php?type=index&url=' . $this->site);
        $dom = file_get_contents_curl('https://search.yahoo.com/search;_ylt=A0LEVxJIGQxVtqMAA4tXNyoA;_ylc=X1MDMjc2NjY3OQRfcgMyBGZyA3NmcARncHJpZANYMDhPa2pzaVRCQ01UTUJMSkxFM0tBBG5fcnNsdAMwBG5fc3VnZwMzBG9yaWdpbgNzZWFyY2gueWFob28uY29tBHBvcwMwBHBxc3RyAwRwcXN0cmwDBHFzdHJsAzIxBHF1ZXJ5A3NpdGU6d3d3LmZhY2Vib29rLmNvbQR0X3N0bXADMTQyNjg1NjI3OQ--?p=site:' . $this->site . '&fr2=sb-top-search&fr=sfp');

        if(! $dom){
            return $yahooData;
        }

        $html = $this->parser->str_get_html($dom);

        if($html->find('.compPagination', 0)){
            $result = trim(str_replace(['results', ',', '.'], ['', '', ''], $html->find('.compPagination', 0)->find('span', 0)->plaintext));
            if(is_numeric($result)){
                $yahooData['yahoo_index_count'] = $result;
            }
        }

        $dom = file_get_contents_curl('https://search.yahoo.com/search;_ylt=A0LEVxJIGQxVtqMAA4tXNyoA;_ylc=X1MDMjc2NjY3OQRfcgMyBGZyA3NmcARncHJpZANYMDhPa2pzaVRCQ01UTUJMSkxFM0tBBG5fcnNsdAMwBG5fc3VnZwMzBG9yaWdpbgNzZWFyY2gueWFob28uY29tBHBvcwMwBHBxc3RyAwRwcXN0cmwDBHFzdHJsAzIxBHF1ZXJ5A3NpdGU6d3d3LmZhY2Vib29rLmNvbQR0X3N0bXADMTQyNjg1NjI3OQ--?p=link%3A' . $this->site . '&fr2=sb-top-search&fr=sfp');
        //$dom = file_get_contents('http://www.burakvanli.com/yahoo.php?type=backlink&url=' . $this->site);

        if(! $dom){
            return $yahooData;
        }

        $html = $this->parser->str_get_html($dom);

        if($html->find('.compPagination', 0)){
            $result = trim(str_replace(['results', ',', '.'], ['', '', ''], $html->find('.compPagination', 0)->find('span', 0)->plaintext));
            if(is_numeric($result)){
                $yahooData['yahoo_backlink_count'] = $result;
            }
        }

        $dom = file_get_contents_curl('https://images.search.yahoo.com/search/images;_ylt=AwrB8pCaIgxV4HIASn2JzbkF;_ylu=X3oDMTBsZ29xY3ZzBHNlYwNzZWFyY2gEc2xrA2J1dHRvbg--;_ylc=X1MDOTYwNjI4NTcEX3IDMgRiY2sDNWV0MHAzOWEwZWZiNCUyNmIlM0QzJTI2cyUzRGlkBGZyA3NmcARncHJpZANaMUcwa0tTLlJLQ1FxLmNPQmNzaHhBBG10ZXN0aWQDbnVsbARuX3N1Z2cDMARvcmlnaW4DaW1hZ2VzLnNlYXJjaC55YWhvby5jb20EcG9zAzAEcHFzdHIDBHBxc3RybAMEcXN0cmwDMzEEcXVlcnkDc2l0ZTp3d3cuYmFyaXNpY2lua29zdXlvcnVtLmNvbQR0X3N0bXADMTQyNjg1ODY1NwR2dGVzdGlkA0lNR0My?gprid=Z1G0kKS.RKCQq.cOBcshxA&pvid=YFW.ujY5LjFXdBkaVAc9ZAtWNzguMQAAAABq_lG0&p=site%3A' . $this->site . '&fr=sfp&fr2=sb-top-images.search.yahoo.com&ei=UTF-8&n=60&x=wrt&y=Search');
        //$dom = file_get_contents('http://www.burakvanli.com/yahoo.php?type=images&url=' . $this->site);

        if(! $dom){
            return $yahooData;
        }

        preg_match('/,\"total\":([0-9]+)/mis', $dom, $matched);

        if(count($matched) >= 2){
            $result = trim($matched[1]);
            if(is_numeric($result)){
                $yahooData['yahoo_image_count'] = $result;
            }
        }
        return $yahooData;
    }


}