Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

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

    public function main() {
        Configure::load('live');
        $instagramconfig = Configure::read("Instagram");        
        $today = date('Y-m-d',time());
        $sql = "UPDATE profiles SET need_refresh = 0 WHERE listed = 0";
        $this->Profile->query($sql);
        $sql = "SELECT count(ir.id) count,ir.from_id FROM instagram_relationships ir JOIN profiles p ON ir.from_id = p.network_id WHERE p.listed = 1 GROUP BY ir.from_id";
        $indexedfollowcounts = $this->Profile->query($sql);
        $sql = "SELECT count(ir.id) count,ir.to_id FROM instagram_relationships ir JOIN profiles p ON ir.to_id = p.network_id WHERE p.listed = 1 GROUP BY ir.to_id";
        $indexedfollowedbycounts = $this->Profile->query($sql);
        $sql = "SELECT id,follows,followed_by,network_id FROM profiles WHERE listed = 1";
        $profilestats = $this->Profile->query($sql);
        $indexedfollowMap = array();
        $indexedfollowedbyMap = array();
        foreach($indexedfollowcounts AS $stat) {
                $indexedfollowMap[$stat['ir']['from_id']] = $stat[0]['count'];
        }
        foreach($indexedfollowedbycounts AS $stat) {
                $indexedfollowedbyMap[$stat['ir']['to_id']] = $stat[0]['count'];
        }
/*
        $this->out("Follow Stats = ".print_r($indexedfollowMap,1));
        $this->out("FollowedBy Stats = ".print_r($indexedfollowedbyMap,1));
        $this->out("Profile Stats = ".print_r($profilestats,1));
*/
        foreach($profilestats AS $stat) {
                if(!isset($indexedfollowMap[$stat['profiles']['network_id']])){
                        $this->markForReferesh($stat['profiles']['id']);
                        continue;
                }
                if(!isset($indexedfollowedbyMap[$stat['profiles']['network_id']])){
                        $this->markForReferesh($stat['profiles']['id']);
                        continue;
                }
                if($stat['profiles']['follows'] != $indexedfollowMap[$stat['profiles']['network_id']]){
                        $this->markForReferesh($stat['profiles']['id']);
                        continue;
                }
                if($stat['profiles']['followed_by'] != $indexedfollowedbyMap[$stat['profiles']['network_id']]){
                        $this->markForReferesh($stat['profiles']['id']);
                        continue;
                }
                if($stat['profiles']['follows'] == $indexedfollowMap[$stat['profiles']['network_id']] && $stat['profiles']['followed_by'] == $indexedfollowedbyMap[$stat['profiles']['network_id']]){
                        $this->unMarkForRefresh($stat['profiles']['id']);
                }
        }
    }

        private function markForReferesh($id) {
                $this->out("Mark $id fro refresh");
                $sql = "UPDATE profiles SET need_refresh = 1 WHERE id = $id";
                $this->Profile->query($sql);
        }
        
        private function unMarkForRefresh($id) {
                $this->out("Unmark $id fro refresh");
                $sql = "UPDATE profiles SET need_refresh = 0 WHERE id = $id";
                $this->Profile->query($sql);
        }
}