Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

<?php
require_once '../Vendor/simple_html_dom.php';

class MSPShell extends AppShell {
    public $uses = array('Offer');

        var $dealIds = array();

    public function main() {
        $url = "http://www.mysmartprice.com/deals/index.php?viewall=true&parameter=hot";
        $rawhtml = $this->fetchHtmlViaProxy($url);
        $this->parseHtml($rawhtml);
        //$this->out(print_r($rawhtml,1));
        $this->fetchMore($this->dealIds);
    }

        private function parseHtml($html) {
                $dom = new simple_html_dom();
                $dom->load($html,true,false);
                foreach($dom->find('a.sectionitem') AS $a) {
                        $data = array();
                        $property = 'data-dealid';
                        $data['fid'] = $a->$property;
                        $data['offerUrl'] = $a->href;
                        $data['thumbnail'] = $a->find('img',0)->src;
                        //$data['endTime'] = @$a->find('div.expirytoday',0)->src;
                        $data['offerName'] = $a->find('div.item-title',0)->plaintext;
                        $data['oldprice'] = trim(substr($a->find('div.item-price',0)->find('span.old',0)->plaintext,3));
                        $data['newprice'] = trim(substr($a->find('div.item-price',0)->find('span.new',0)->plaintext,3));
                        $data['offerDescription'] = $a->find('div.item-shipping',0)->plaintext;
                        $data['store'] = $a->find('div.item-store',0)->plaintext;
                        $this->saveOffer($data);
                } 
        }

        private function saveOffer($offer){             
                $this->out(print_r($offer,1));
                $moreurl = "http://www.mysmartprice.com/deals/more_deals.php";
                $this->dealIds[] = $offer['fid'];
                //$this->Offer->create();
                //$this->Offer->save($offer);
        }

        private function fetchHtmlViaProxy($url,$fields = null) {
                $proxy = '190.112.203.13:1212';

                $options = array( 
//                  CURLOPT_PROXY          => $proxy,
                    CURLOPT_HTTPPROXYTUNNEL => 0,
                    CURLOPT_REFERER        => "http://www.google.com",
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1", 
                    CURLOPT_CONNECTTIMEOUT => 20,
                    CURLOPT_TIMEOUT        => 20,
                    CURLOPT_MAXREDIRS      => 10,
                    CURLOPT_HEADER         => true,

                ); 

                $ch = curl_init( $url ); 
                if(!empty($fields)) {
                        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
                        rtrim($fields_string, '&');
                        //set the url, number of POST vars, POST data
                        curl_setopt($ch,CURLOPT_POST, count($fields));
                        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
                }
                curl_setopt_array($ch,$options);
                $content = curl_exec( $ch ); 
                return $content;
        }
}