Example #1
0
 public static function get_fb_feeds($fb_page_id = '', $page_id = 0, $category_id = 0)
 {
     if (!$fb_page_id) {
         return false;
     }
     $url = self::$api_url . $fb_page_id . '/posts?limit=' . self::$limit . '&access_token=' . self::get_fb_access_token();
     $obj = json_decode(HelperNews::curl($url));
     $batches_like = array();
     $batches_comment = array();
     $items = array();
     $item_ids = array();
     if (!empty($obj->data) && is_array($obj->data)) {
         foreach ($obj->data as $i => $item) {
             if (empty($item->id)) {
                 return false;
             }
             $shares = !empty($item->shares->count) ? $item->shares->count : 0;
             $timestamp = \Carbon\Carbon::now()->toDateTimeString();
             $items[] = array('page_id' => $page_id, 'category_id' => $category_id, 'fb_id' => !empty($item->id) ? $item->id : 0, 'object_id' => !empty($item->object_id) ? $item->object_id : 0, 'name' => !empty($item->name) ? $item->name : '', 'message' => !empty($item->message) ? $item->message : '', 'picture' => !empty($item->picture) ? $item->picture : '', 'fb_link' => !empty($item->link) ? $item->link : '', 'source' => !empty($item->source) ? $item->source : '', 'type' => !empty($item->type) ? $item->type : '', 'share_count' => $shares, 'created_at' => !empty($item->created_time) ? $item->created_time : '', 'updated_at' => $timestamp);
             $item_ids[] = $item->id;
             $batch = new \stdClass();
             $batch->method = 'GET';
             $batch->relative_url = $item->id . '/likes?summary=1';
             $batches_like[] = $batch;
             $batch = new \stdClass();
             $batch->method = 'GET';
             $batch->relative_url = $item->id . '/comments?summary=1';
             $batches_comment[] = $batch;
         }
         // Get facebook like meta information
         $url = self::$api_url . '?batch=' . urlencode(json_encode($batches_like)) . '&access_token=' . self::get_fb_access_token() . '&method=post';
         $objs = json_decode(HelperNews::curl($url));
         if (!empty($objs)) {
             foreach ($objs as $i => $obj) {
                 if ($obj->body) {
                     $body = json_decode($obj->body);
                     $items[$i]['like_count'] = isset($body->summary->total_count) ? $body->summary->total_count : 0;
                 }
             }
         }
         // Get facebook like meta information
         $url = self::$api_url . '?batch=' . urlencode(json_encode($batches_comment)) . '&access_token=' . self::get_fb_access_token() . '&method=post';
         $objs = json_decode(HelperNews::curl($url));
         if (!empty($objs)) {
             foreach ($objs as $i => $obj) {
                 if ($obj->body) {
                     $body = json_decode($obj->body);
                     $items[$i]['comment_count'] = isset($body->summary->total_count) ? $body->summary->total_count : 0;
                 }
             }
         }
         $records = FbFeed::whereIn('fb_id', $item_ids)->get();
         if (count($records)) {
             foreach ($items as $i => $item) {
                 if ($item) {
                     foreach ($records as $j => $record) {
                         if ($item['fb_id'] == $record->fb_id) {
                             if ($item['share_count'] != $record->share_count || $item['like_count'] != $record->like_count || $item['comment_count'] != $record->comment_count) {
                                 $record->share_count = $item['share_count'];
                                 $record->like_count = $item['like_count'];
                                 $record->comment_count = $item['comment_count'];
                                 $record->save();
                             }
                             unset($items[$i]);
                         }
                     }
                 }
             }
         }
         if (count($items)) {
             foreach ($items as $i => $item) {
                 $record = FbFeed::where('fb_id', '=', $item['fb_id'])->first();
                 if (!$record) {
                     FbFeed::insert(array($item));
                 }
             }
         }
     }
     return $items;
 }
Example #2
0
 public function getSpamCommentPage()
 {
     $record = FbPage::whereRaw('status = ? AND crawl_fb = ?', array(true, false))->first();
     if (!$record) {
         FbPage::where('crawl_fb', '=', true)->update(['crawl_fb' => false]);
         $record = FbPage::whereRaw('status = ? AND crawl_fb = ?', array(true, false))->first();
     }
     if (!empty($record->fb_id)) {
         // Get post of current page
         //$records = FbFeed::whereRaw('status = ? AND page_id = ? AND per_day = CURDATE() AND comment_count > ? AND type = ? AND spam IS NULL', [true, $record->id, LIMIT_COMMENT_COUNT, 'link', null])
         $feeds = FbFeed::whereRaw('status = ? AND page_id = ? AND DATE(created_at) = CURDATE() AND comment_count > ? AND type = ? AND spam IS NULL', [true, $record->id, LIMIT_COMMENT_COUNT, 'link'])->select(DB::raw('id, fb_id, fb_link, name, message, picture, page_id, category_id, like_count, comment_count, share_count, type, source, object_id, created_at, DATE(created_at) AS per_day'))->orderBy('per_day', 'DESC')->orderBy('comment_count', 'DESC')->take(10)->get();
         if (count($feeds) >= 4) {
             // Get hot news
             //$feeds = FbFeed::getHotFeeds($record->id, 2);
             /*if (count($feeds)) {
                                 foreach ( $records as $record ) {
                                     if ($record->fb_id != ) {
                                         HelperNews::postCommentToFB($record->fb_id, $record->link, $record->name);
                                     }
             
                                 }
                             }*/
             $last = count($feeds) - 1;
             if ($last >= 1) {
                 foreach ($feeds as $i => $feed) {
                     if ($last - $i != $i) {
                         $feed->spam = HelperNews::postCommentToFB($feed->fb_id, $feeds[$last - $i]->fb_link, $feeds[$last - $i]->name);
                         $feed->save();
                         $record->crawl_fb = true;
                         $record->save();
                     }
                 }
             }
         }
     }
     return Response::json(array('status' => 'success', 'data' => $feeds), 200);
 }
Example #3
0
 public function getFeeds()
 {
     $data = get();
     $default = array('category_ids' => '', 'page_id' => 0, 'page' => 0, 'clear_cache' => 0, 'order' => 'new');
     extract(array_merge($default, $data));
     $arr_cats = array();
     if (is_string($category_ids)) {
         $arr_cats_old = explode(',', $category_ids);
         if (!is_array($arr_cats_old) && empty($arr_cats_old)) {
             return Response::json(array('status' => 'error', 'data' => 'Error'), 500);
         }
         foreach ($arr_cats_old as $i => $item) {
             $item = intval($item);
             if (!in_array($item, $arr_cats) && !empty($item)) {
                 $arr_cats[] = $item;
             }
         }
     }
     $limit = DLN_LIMIT * 10;
     $index = (int) ($page / 10);
     $pos = (int) ($page % 10);
     $skip = $index * $limit;
     sort($arr_cats);
     $category_path = implode('_', $arr_cats);
     switch ($order) {
         case 'share':
             $order_by = 'share_count';
             break;
         case 'comment':
             $order_by = 'comment_count';
             break;
         case 'like':
             $order_by = 'like_count';
             break;
         case 'new':
         default:
             $order_by = 'created_at';
             break;
     }
     $cache_id = md5("{$order_by}_{$category_path}_{$page_id}_{$index}");
     if (!empty($clear_cache)) {
         Cache::forget($cache_id);
     }
     if (!Cache::has($cache_id)) {
         if ($page_id) {
             $_cond = 'status = ? AND page_id = ?';
             $_cona = array(true, $page_id);
         } else {
             $_cond = 'status = ? AND like_count > ?';
             $_cona = array(true, ALLOW_LIKE);
         }
         if (!empty($arr_cats)) {
             $records = FbFeed::whereRaw($_cond, $_cona)->whereIn('category_id', $arr_cats)->select(DB::raw('id, fb_id, name, message, picture, page_id, category_id, like_count, comment_count, share_count, type, source, object_id, shared, created_at, DATE(created_at) AS per_day'))->orderBy('per_day', 'DESC')->orderBy($order_by, 'DESC')->skip($skip)->take($limit)->get()->toArray();
         } else {
             $records = FbFeed::whereRaw($_cond, $_cona)->select(DB::raw('id, fb_id, name, message, picture, page_id, category_id, like_count, comment_count, share_count, type, source, object_id, shared, created_at, DATE(created_at) AS per_day'))->orderBy('per_day', 'DESC')->orderBy($order_by, 'DESC')->skip($skip)->take($limit)->get()->toArray();
         }
         Cache::put($cache_id, json_encode($records), DLN_CACHE_MINUTE);
     } else {
         $records = json_decode(Cache::get($cache_id));
     }
     $records = array_slice($records, $pos * DLN_LIMIT, DLN_LIMIT);
     return Response::json(array('status' => 'success', 'data' => $records), 200);
 }