Exemplo n.º 1
0
 public function collectInfo()
 {
     $cmd_count = 0;
     //  $categories = array('1' => 'check-in', '2' => 'link', '3' => 'status','4'=>'photo', '5' => 'video', '6' => 'comment', '7' => 'others',);
     $categories = array('2' => 'link', '3' => 'status', '5' => 'video', '4' => 'photo');
     $facebook = $_SESSION['facebook'];
     //***************Set Times***********
     $all_friends = $facebook->api('/me/friends');
     $days_threshold = 3;
     ini_set('max_execution_time', 60 * 60);
     $start_time = mktime(0, 0, 0, date("m"), date("d") - $days_threshold, date("Y"));
     $end_time = time();
     $time_step = 4 * 60 * 60;
     //for when FB did not return the posts for a whole period of time and we had to make sub-queries
     //***************Set User Attributes & Save in DB****************
     $user = $facebook->getUser();
     if ($user) {
         $this->fbid = $user;
         $this->start_time = date("Y-m-d H:i:s", $start_time);
         $this->end_time = date("Y-m-d H:i:s", $end_time);
         $this->created_at = new CDbExpression('NOW()');
         if ($this->save()) {
             // Yii::log('success');
             // echo 'success';
         } else {
             print_r($this->getErrors());
         }
     } else {
         // echo 'cannot save user in DB!';
     }
     $cmd_count++;
     $i = 0;
     //****************Save All Friends' Basic information in DB*******************
     for ($f = 0; $f < sizeof($all_friends['data']); $f++) {
         $fbid = $all_friends['data'][$f]['id'];
         $friend = new Friend();
         $friend->fbid = $fbid;
         $friend->name = $all_friends['data'][$f]['name'];
         $friend->user = $this->id;
         if ($friend->save()) {
             //Yii::log('success');
             // echo 'success';
         } else {
             Yii::log($this->getErrors());
             print_r($this->getErrors());
         }
     }
     //*************************************Fetch News Feed (Shown Stories)*********************************
     //******Before, querying the whole time period did not retreive the NewsFeed Posts =>
     // I made it to time-slots using $time_step variable =>
     // but now, it does not work! and querying over the whole period works well! So, I came back to the first method!********
     // for ($ctime = $start_time; $ctime < $end_time; $ctime += $time_step) {
     //   $accesToken = Yii::app()->facebook->getAccessToken();
     //   $cmd = "me/home?fields=id,from,type,link,story,picture,source,name,description,message&limit=500&since=" . $ctime . "&until=" . ($ctime + $time_step);
     $cmd = "me/home?fields=id,from,type,link,story,picture,source,name,description,message,created_time&limit=500&since=" . $start_time . "&until=" . $end_time;
     // $cmd = "me/home?fields=id,from,type,link,story,picture,source,name,description,message&limit=500";
     $me = $facebook->api($cmd);
     echo "cmd: " . $cmd . "<br>";
     echo "size of data: " . sizeof($me['data']) . '<br>';
     $cmd_count++;
     //************Save News Feed Stories at DB************
     $saved_shown_posts_count = 0;
     for ($j = 0; $j < sizeof($me['data']); $j++) {
         $postdata = new PostData();
         $postdata->user = $this->id;
         if (isset($me['data'][$j]['id'])) {
             $postdata->post_id = $me['data'][$j]['id'];
             // echo 'post_id(all): '. $postdata->post_id. '<br>';
         }
         if (isset($me['data'][$i]['from'])) {
             //Save post's authors' attributes
             if (isset($me['data'][$i]['from']['id'])) {
                 $postdata->from_id = $me['data'][$j]['from']['id'];
                 // this is the author's Facebook ID.
             }
             if (isset($me['data'][$j]['from']['name'])) {
                 $postdata->from_name = $me['data'][$j]['from']['name'];
             }
             if (isset($postdata->from_id)) {
                 //   $friend_id = Friend::model()->find('fbid=:fbid AND user=:user', array(':fbid' => $postdata->from_id, ':user' => $this->id));
                 $friend_obj = Friend::model()->find(array('select' => 'id', 'condition' => 'fbid=:fbid AND user=:user', 'params' => array(':fbid' => $postdata->from_id, ':user' => $this->id)));
                 //echo $friend_id['id'] . '<br>';
                 if (isset($friend_obj['id'])) {
                     $postdata->friend = $friend_obj['id'];
                     //this is the autor's Database ID and it needs to be saved!
                 }
             }
         }
         if (isset($me['data'][$j]['description'])) {
             $postdata->description = $me['data'][$j]['description'];
         }
         if (isset($me['data'][$j]['name'])) {
             $postdata->name = $me['data'][$j]['name'];
         }
         if (isset($me['data'][$j]['story'])) {
             $postdata->story = $me['data'][$j]['story'];
         }
         if (isset($me['data'][$j]['link'])) {
             $postdata->link = $me['data'][$j]['link'];
         }
         if (isset($me['data'][$j]['picture'])) {
             $postdata->picture = $me['data'][$j]['picture'];
         }
         if (isset($me['data'][$j]['message'])) {
             $postdata->message = $me['data'][$j]['message'];
         }
         if (isset($me['data'][$j]['source'])) {
             $postdata->source = $me['data'][$j]['source'];
         }
         if (isset($me['data'][$j]['created_time'])) {
             echo "time: " . $me['data'][$j]['created_time'] . '<br>';
             $timestamp = strtotime($me['data'][$j]['created_time']);
             $postdata->created_time = date($timestamp);
         }
         if (isset($me['data'][$j]['type'])) {
             $postdata->type = $me['data'][$j]['type'];
         }
         //*****Save postdata*****
         if ($postdata->save()) {
         } else {
             //Yii::log($postdata->getErrors());
             //echo 'cannot save this post since:<br>';
             //print_r($postdata->getErrors());
             return;
         }
     }
 }