Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

<?php
class InstaShell extends AppShell {
    public $uses = array('InstagramUser','Profile','InstagramRelationship');

    public function main() {
        Configure::load('live');
        $instagramconfig = Configure::read("Instagram");        
        $today = date('Y-m-d',time());
        $options = array('conditions'=>array('last_crawled !='=>$today,'network'=>'instagram','need_refresh' => 1),'fields'=>array('id','network_id','access_token'),'recursive'=>-1);
        $users = $this->Profile->find('all',$options);
        if(!empty($users)) {
                foreach($users AS $user) {
                        $sql = "DELETE FROM instagram_relationships WHERE from_id = ".$user['Profile']['network_id'];
                        $this->Profile->query($sql);
                        $sql = "DELETE FROM instagram_relationships WHERE to_id = ".$user['Profile']['network_id'];
                        $this->Profile->query($sql);
                        $url = $instagramconfig['apiurl']."v1/users/".$user['Profile']['network_id']."/follows?access_token=".$user['Profile']['access_token'];
                        $this->out("Follows url ".$url);
                        $this->crawlInstagram($url,$user['Profile']['id'], $user['Profile']['network_id'],$user['Profile']['access_token']);
                        $url = $instagramconfig['apiurl']."v1/users/".$user['Profile']['network_id']."/followed-by?access_token=".$user['Profile']['access_token'];
                        $this->out("Followed By url ".$url);
                        $this->crawlFollowers($url,$user['Profile']['id'], $user['Profile']['network_id'],$user['Profile']['access_token']);
                        $sql = "UPDATE profiles SET last_crawled = CURDATE() WHERE id = ".$user['Profile']['id'];
                        $this->Profile->query($sql);
                }
        }
    }

        private function crawlInstagram($url,$id,$instagramId,$access_token){
                $today = date('Y-m-d',time());
                $apiresult = $this->Profile->make_request($url,null);
                if(!empty($apiresult['data'])){         
                        $sql = "INSERT IGNORE INTO instagram_relationships(`from_id`, `to_id`) VALUES";
                        foreach($apiresult['data'] AS $profile) {
                                $profiledata[] = $profile;
                                $sql .= "($instagramId, ".$profile['id']."),";
                        }
                        try{
                                /*
                                $this->InstagramUser->create();
                                $this->InstagramUser->save($profile);
                                $data = array('from_id' => $instagramId, 'to_id' => $profile['id']);
                                $this->InstagramRelationship->create();
                                $this->InstagramRelationship->save($data);
                                */
                                $this->InstagramUser->create();
                                $this->InstagramUser->saveAll($profiledata);
                                $sql = substr($sql,0,-1);
                                $this->log("crawlInstagram [$instagramId] ".sizeof($profiledata),'instafollowed-'.$today.'-'.$instagramId);
                                $this->log("crawlInstagram [$instagramId] ".$sql,'instafollowed-'.$today.'-'.$instagramId);
                                $this->Profile->query($sql);
                                sleep(1);
                                //$this->InstagramRelationship->create();
                                //$this->InstagramRelationship->saveAll($relationshipdata);
                        }catch(Exception $ex){
                                $this->out($ex->getMessage());
                        }
                }
                if(isset($apiresult['pagination']['next_url']) && !empty($apiresult['pagination']['next_url'])){                
                        $this->out("Now crawling ".$apiresult['pagination']['next_url']);
                        $this->crawlInstagram($apiresult['pagination']['next_url'],$id,$instagramId,$access_token);
                }
        }
        
        private function crawlFollowers($url,$id,$instagramId,$access_token){
                $today = date('Y-m-d',time());
                $apiresult = $this->Profile->make_request($url,null);
                if(!empty($apiresult['data'])){         
                        $sql = "INSERT IGNORE INTO instagram_relationships(`to_id`, `from_id`) VALUES";
                        foreach($apiresult['data'] AS $profile) {
                                $profiledata[] = $profile;
                                $sql .= "($instagramId, ".$profile['id']."),";
                        }
                        try{
                        /*
                                $this->InstagramUser->create();
                                $this->InstagramUser->save($profile);
                                $data = array('to_id' => $instagramId, 'from_id' => $profile['id']);
                                $this->InstagramRelationship->create();
                                $this->InstagramRelationship->save($data);
                        */
                                $this->InstagramUser->create();
                                $this->InstagramUser->saveAll($profiledata);
                                $sql = substr($sql,0,-1);
                                $this->log("crawlFollowers [$instagramId] ".sizeof($profiledata),'instafollowers-'.$today.'-'.$instagramId);
                                $this->log("crawlFollowers [$instagramId] ".$sql,'instafollowers-'.$today.'-'.$instagramId);
                                $this->Profile->query($sql);
                //              sleep(1);
                                //$this->InstagramRelationship->create();
                                //$this->InstagramRelationship->saveAll($relationshipdata);
                        }catch(Exception $ex){
                                $this->out($ex->getMessage());
                        }
                }
                if(isset($apiresult['pagination']['next_url']) && !empty($apiresult['pagination']['next_url'])){                
                        $this->out("Now crawling ".$apiresult['pagination']['next_url']);
                        $this->crawlFollowers($apiresult['pagination']['next_url'],$id,$instagramId,$access_token);
                }
        }
}