/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $socialMediaAccounts = SocialMediaAccount::where('account_type', 'twitter')->where('auto_follow', 1)->get()->all(); foreach ($socialMediaAccounts as $socialMediaAccount) { $errorMessage = ""; $errorCount = 0; echo "<h2>{$socialMediaAccount->screen_name}</h2>"; /* if ($socialMediaAccount->id == 4) { echo "<h3>SKIPPED!</h3>"; continue; } */ $connection = new TwitterOAuth($socialMediaAccount->consumer_key, $socialMediaAccount->consumer_secret, $socialMediaAccount->access_token, $socialMediaAccount->access_token_secret); $targetUsers = TargetUser::where('social_media_account_id', $socialMediaAccount->id)->where('to_follow', 1)->take(100)->get(); $i = 1; if (is_null($targetUsers)) { echo "No Target Users in DB."; continue; } else { foreach ($targetUsers as $targetUser) { $follow = $connection->post("https://api.twitter.com/1.1/friendships/create.json?user_id={$targetUser->account_id}&follow=true"); if (isset($follow->errors)) { $errorObject = $follow->errors; $error = $errorObject[0]->code; $errorCount++; $errorMessage .= "<h2>Error {$errorCount}</h2>"; $errorMessage .= "Friendship creator to needs to refresh. " . $errorObject[0]->message; echo "<div class='errorMessage'>{$errorMessage}</div>"; $breakableError = "You are unable to follow more people at this time."; $blockedError = 'You have been blocked from following this account at the request of the user.'; if ($errorObject[0]->message == $blockedError) { $oldTargetUser = TargetUser::find($targetUser->id)->delete(); echo " - Target User deleted from DB."; } elseif (strpos($errorObject[0]->message, $breakableError) !== false) { break; } continue; } else { echo "<br>{$i}: {$targetUser->screen_name} followed!"; Friend::create(['account_id' => $targetUser->account_id, 'screen_name' => $targetUser->screen_name ? $targetUser->screen_name : '', 'whitelisted' => $targetUser->whitelist ? true : false, 'social_media_account_id' => $socialMediaAccount->id]); $oldTargetUser = TargetUser::find($targetUser->id)->delete(); echo " - Target User deleted from DB."; } $i++; } } $i--; // Accurate amount of friendships if ($i == 0) { $message = "{$i} friendships created for {$socialMediaAccount->screen_name}! Please check to see if you have a user scheduled."; } else { $message = "{$i} friendships created for {$socialMediaAccount->screen_name}!"; } Helper::email_user($message, $socialMediaAccount->user_id); if ($errorCount > 0) { Helper::email_admin($errorMessage, $errorCount, "FollowController", $socialMediaAccount->screen_name); } } }
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $socialMediaAccounts = SocialMediaAccount::where('account_type', 'twitter')->where('auto_unfollow', 1)->get()->all(); foreach ($socialMediaAccounts as $socialMediaAccount) { $errorMessage = ""; $errorCount = 0; $i = 1; if ($socialMediaAccount->id != 10) { continue; } echo "<h2>{$socialMediaAccount->screen_name}</h2>"; $connection = new TwitterOAuth($socialMediaAccount->consumer_key, $socialMediaAccount->consumer_secret, $socialMediaAccount->access_token, $socialMediaAccount->access_token_secret); $friends = Friend::where('social_media_account_id', $socialMediaAccount->id)->where('whitelisted', 0)->select('account_id')->take(100)->get()->toArray(); // ->where('created_at', '<=', Carbon::today('America/Denver')->subweek()) $oldFriends_ids = array(); foreach ($friends as $account_id) { $oldFriends_ids[] = $account_id['account_id']; } if (is_null($oldFriends_ids)) { echo "No friends in DB."; } else { foreach ($oldFriends_ids as $oldFriend) { $destroyFriendship = $connection->post("https://api.twitter.com/1.1/friendships/destroy.json?user_id={$oldFriend}"); if (isset($destroyFriendship->errors)) { $errorObject = $destroyFriendship->errors; $error = $errorObject[0]->code; $errorCode = $errorObject[0]->code; $errorCount++; $errorMessage .= "<h2>Error {$errorCount}</h2>"; $errorMessage .= $errorObject[0]->message; echo "<div class='errorMessage'>{$errorMessage}</div>"; if (261 == $errorCode) { Helper::email_admin("Need to reset Twitter Auth Tokens", 1, "UnfollowController", $socialMediaAccount->screen_name); break; } elseif (34 == $errorCode) { break; } else { continue; } } else { echo "<br>{$i}: {$oldFriend} - friendship destroyed!"; $friend = Friend::where('social_media_account_id', $socialMediaAccount->id)->where('account_id', $oldFriend)->get()->first(); $friend->unfollowed = 1; $friend->unfollowed_timestamp = Carbon::now('America/Denver'); $friend->save(); echo " - Unfollow flagged in DB."; } $i++; } } $i--; // Accurate amount of friendships $message = "{$i} friendships destroyed for {$socialMediaAccount->screen_name}."; Helper::email_user($message, $socialMediaAccount->user_id); if ($errorCount > 0) { Helper::email_admin($errorMessage, $errorCount, "UnfollowController", $socialMediaAccount->screen_name); } } }
public function filter() { $socialMediaAccounts = SocialMediaAccount::where('account_type', 'twitter')->get()->all(); $crunchAccounts = SocialMediaAccount::where('account_type', 'crunch')->get()->all(); // Calculate how many accounts to grab from the API to filter $count = count($crunchAccounts) / count($socialMediaAccounts) * 180; foreach ($socialMediaAccounts as $socialMediaAccount) { $errorCount = 0; $errorMessage = ""; $connection = new TwitterOAuth($socialMediaAccount->consumer_key, $socialMediaAccount->consumer_secret, $socialMediaAccount->access_token, $socialMediaAccount->access_token_secret); $myScreenName = $socialMediaAccount->screen_name; echo "<h1>{$myScreenName}</h1>"; // GET CURRENT DB Followers $oldFollowers = Follower::where('social_media_account_id', $socialMediaAccount->id)->select('account_id')->get()->all(); $oldFollowers_ids = array(); foreach ($oldFollowers as $account_id) { $oldFollowers_ids[] = $account_id['account_id']; } // GET CURRENT DB FRIENDS $oldFriends = Friend::where('social_media_account_id', $socialMediaAccount->id)->select('account_id')->get()->all(); $oldFriends_ids = array(); foreach ($oldFriends as $account_id) { $oldFriends_ids[] = $account_id['account_id']; } // GET ALL TARGET USERS AND PROCESS INTO ARRAY $targetUsers = TargetUser::where('social_media_account_id', $socialMediaAccount->id)->select('account_id')->get()->all(); $targetUsers_ids = array(); foreach ($targetUsers as $id) { $targetUsers_ids[] = $id['account_id']; } /** * * * GET MODEL ACCOUNTS'S FOLLOWERS, FILTER IF ALREADY FOLLOWING OR FRIEND, ADD TO TARGET USERS TABLE * * */ // GET MODEL ACCOUNT $modelAccount = ModelAccount::where('social_media_account_id', $socialMediaAccount->id)->where('api_cursor', '!=', 0)->where('sort_order', 1)->get()->first(); if (!is_null($modelAccount)) { $cursor = (int) $modelAccount->api_cursor; echo "<h2>@" . $modelAccount->screen_name . "'s ONLINE FOLLOWERS</h2><br>"; $searchFollowersAPI = "https://api.twitter.com/1.1/followers/ids.json?cursor={$cursor}&screen_name={$modelAccount->screen_name}&count={$count}"; $followers = $connection->get("{$searchFollowersAPI}"); if (isset($followers->errors)) { $errorCount++; $errorObject = $followers->errors; $error = $errorObject[0]->code; $errorMessage .= "<h2>Error {$errorCount}</h2>"; $errorMessage .= "Model account follower lookup to needs to refresh. " . $errorObject[0]->message; echo "<div class='errorMessage'>{$errorMessage}</div>"; continue; } else { $modelFollowers = $followers->ids; foreach ($modelFollowers as $id) { $modelFollowers_ids[] = $id; } if (isset($modelFollowers_ids)) { $filterFollowers = array_diff($modelFollowers_ids, $oldFollowers_ids); $filterFriends = array_diff($filterFollowers, $oldFriends_ids); $filterTargets = array_diff($filterFriends, $targetUsers_ids); foreach ($filterTargets as $id) { $newTarget = TargetUser::create(['account_id' => $id, 'social_media_account_id' => $socialMediaAccount->id]); } } if (count($modelFollowers) > 0) { $modelAccount->api_cursor = $followers->next_cursor; $modelAccount->save(); if ($modelAccount->api_cursor == 0) { $errorCount++; $errorMessage .= "<h2>Error {$errorCount}</h2>"; $errorMessage .= "Model Account API cursor equals 0.<br>"; $errorMessage .= "{$i} were added to target_users table.<br>"; $data = "You reached the end of {$modelAccount->screen_name}. Please double check to see if you have any other users scheduled so Scavenger can keep working on your account!"; Helper::email_user($data, $socialMediaAccount->user_id); } } echo "<br><br><strong>Next Cursor: </strong>{$followers->next_cursor}"; } } else { echo "<h1>Add a model account that hasn't been finished!</h1>"; } } echo '<hr>'; $now = Carbon::now('America/Denver'); echo "<br>{$now}"; if ($errorCount > 0) { Helper::email_admin($errorMessage, $errorCount, "ModelAccountController", $socialMediaAccount->screen_name); } }