/**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     $instagram = new Instagram(array('apiKey' => config('instagram.id'), 'apiSecret' => config('instagram.secret'), 'apiCallback' => 'http://instagram.app'));
     $account = InstagramAccount::find($this->id);
     $accountMedia = [];
     $user = $instagram->getUser($account['instagram_object_id']);
     if ($user->meta->code == 200) {
         $mediaCount = $user->data->counts->media;
         if ($mediaCount != 0) {
             $media = $instagram->getUserMedia($account['instagram_object_id'], 32);
             while (!is_null($media)) {
                 foreach ($media->data as $data) {
                     if ($data->type == 'video') {
                         continue;
                     }
                     $accountMedia[] = new InstagramMedia(['instagram_object_id' => $data->id, 'photo_url' => $data->link, 'photo_caption' => is_object($data->caption) ? $data->caption->text : null, 'likes_count' => $data->likes->count, 'comments_count' => $data->comments->count]);
                 }
                 $media = $instagram->pagination($media, 32);
             }
         } else {
             $account->status = InstagramAccount::GRABBER_COMPLETED;
             $account->save();
         }
     }
     DB::transaction(function () use($account, $accountMedia) {
         $account->InstagramMedia()->saveMany($accountMedia);
         $account->status = InstagramAccount::GRABBER_COMPLETED;
         $account->save();
     });
 }
示例#2
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $instagram = new InstagramAPI();
     foreach (\App\Supplier::all() as $supplier) {
         $instagram->setAccessToken($supplier->instagramAccount->access_token);
         foreach ($supplier->products()->where('is_active', true)->get() as $product) {
             $comments = $instagram->getMediaComments($product->instagram->id);
             if ($comments->meta->code == 200) {
                 $lastScannedComment = null;
                 for ($i = count($comments->data) - 1; $i >= 0; $i--) {
                     $comment = $comments->data[$i];
                     if ($lastScannedComment == null) {
                         $lastScannedComment = $comment->id;
                     }
                     if ($product->instagram->last_scanned_comment >= $comment->id) {
                         break;
                     }
                     if (strpos(mb_strtolower($comment->text, 'UTF-8'), 'sepete at') !== false) {
                         if ($customer = \App\InstagramAccount::where('instagram_id', $comment->from->id)->first()) {
                             if ($customer->isCustomer()) {
                                 $customer = $customer->instagramable;
                                 \App\WishedProduct::create(['customer_id' => $customer->id, 'product_id' => $product->id, 'count' => 1]);
                             }
                         }
                     }
                 }
                 if ($lastScannedComment != null) {
                     $productInstagram = $product->instagram;
                     $productInstagram->last_scanned_comment = $lastScannedComment;
                     $productInstagram->update();
                 }
             }
         }
     }
 }
 public function listMedia($username, Request $request)
 {
     $page = $request->all();
     $wrap = isset($page['wrap']) ? $page['wrap'] : InstagramMedia::WRAP;
     $account = InstagramAccount::where('username', '=', $username)->with('InstagramMedia')->first();
     if (!is_null($account)) {
         $media = $account->InstagramMedia()->select('id', 'photo_url', 'photo_caption', 'likes_count', 'comments_count')->paginate($wrap)->toArray();
         $data = array_pop($media);
         return json_encode(['pagination' => $media, 'meta' => ['code' => 200], 'data' => $data]);
     } else {
         return json_encode(['meta' => ['error_type' => InstagramAccount::GRABBER_ACCOUNT_MEDIA_LIST_EMPTY, 'code' => 404, 'error_message' => 'Grabber account not exist or media list are empty']]);
     }
 }
 /**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     $instagram = new Instagram(array('apiKey' => config('instagram.id'), 'apiSecret' => config('instagram.secret'), 'apiCallback' => 'http://instagram.app'));
     $account = InstagramAccount::find($this->id);
     $account->status = InstagramAccount::GRABBER_NOT_COMPLETED;
     $account->save();
     $accountMedia = array_combine($account->instagramMedia->lists('instagram_object_id')->toArray(), $account->instagramMedia->all());
     $user = $instagram->getUser($account['instagram_object_id']);
     if ($user->meta->code == 200) {
         $mediaCount = $user->data->counts->media;
         if ($mediaCount != 0) {
             $media = $instagram->getUserMedia($account['instagram_object_id'], 32);
             while (!is_null($media)) {
                 foreach ($media->data as $data) {
                     if ($data->type == 'video') {
                         continue;
                     }
                     if ($this->mode == 'WITH_ADVANCED' && isset($accountMedia[$data->id])) {
                         $accountMedia[$data->id]->instagramMediaLike()->save(new InstagramMediaLike(['count' => $data->likes->count - $accountMedia[$data->id]->likes_count]));
                         $comments = $instagram->getMediaComments($data->id);
                         if (!empty($comments->data)) {
                             foreach ($comments->data as $comment) {
                                 $accountMedia[$data->id]->instagramMediaComment()->updateOrCreate(['instagram_object_id' => $comment->id], ['instagram_object_id' => $comment->id, 'created_at' => Carbon::createFromTimestamp($comment->created_time)]);
                             }
                         }
                     }
                     if (isset($accountMedia[$data->id])) {
                         // update
                         $accountMedia[$data->id]->photo_caption = is_object($data->caption) ? $data->caption->text : null;
                         $accountMedia[$data->id]->likes_count = $data->likes->count;
                         $accountMedia[$data->id]->comments_count = $data->comments->count;
                     } else {
                         // insert
                         $accountMedia[] = new InstagramMedia(['instagram_object_id' => $data->id, 'photo_url' => $data->link, 'photo_caption' => is_object($data->caption) ? $data->caption->text : null, 'likes_count' => $data->likes->count, 'comments_count' => $data->comments->count]);
                     }
                 }
                 $media = $instagram->pagination($media, 32);
             }
         } else {
             $account->status = InstagramAccount::GRABBER_COMPLETED;
             $account->save();
         }
     }
     DB::transaction(function () use($account, $accountMedia) {
         $account->InstagramMedia()->saveMany($accountMedia);
         $account->status = InstagramAccount::GRABBER_COMPLETED;
         $account->save();
     });
 }
示例#5
0
 public function callback(Request $request)
 {
     if (Session::get('instagram_operation')) {
         $instagramOperation = Session::pull('instagram_operation');
         if ($instagramOperation['operation'] == 'login') {
             if ($request->get('code')) {
                 $code = $request->get('code');
                 $instagram = new InstagramAPI();
                 $data = $instagram->getOAuthToken($code);
                 $instagramAccount = InstagramAccount::where(['username' => $data->user->username, 'access_token' => $data->access_token])->first();
                 if ($instagramAccount) {
                     if (Auth::loginUsingId($instagramAccount->instagramable->user->id)) {
                         if ($instagramAccount->instagramable->user->isSupplier()) {
                             return redirect()->action('Dashboard\\SupplierController@show');
                         }
                         if ($instagramAccount->instagramable->user->isCustomer()) {
                             return redirect()->action('Dashboard\\CustomerController@show');
                         }
                     }
                 } else {
                     return redirect()->action('AuthenticationController@showRegister')->withErrors(['messages' => "Kayıtlı Kullanıcı Bulunamadı"]);
                 }
             }
         }
         if ($instagramOperation['operation'] == 'register') {
             if ($instagramOperation['user_type'] == 'supplier') {
                 if (Session::has('user_instagram_info')) {
                     Session::forget('user_instagram_info');
                 }
                 if ($request->get('code')) {
                     $code = $request->get('code');
                     $instagram = new InstagramAPI();
                     $data = $instagram->getOAuthToken($code);
                     Session::put('user_instagram_info', $data);
                     return Redirect::action('Dashboard\\SupplierController@create');
                 }
             }
             if ($instagramOperation['user_type'] == 'customer') {
                 if (Session::has('user_instagram_info')) {
                     Session::forget('user_instagram_info');
                 }
                 if ($request->get('code')) {
                     $code = $request->get('code');
                     $instagram = new InstagramAPI();
                     $data = $instagram->getOAuthToken($code);
                     Session::put('user_instagram_info', $data);
                     return Redirect::action('Dashboard\\CustomerController@create');
                 }
             }
         }
     }
 }