This is how I run / update the full list of Repeater IDs on my platform. I would avoid using sleep() as an @aphoe script. Really bad to keep in touch for so long, and what happens if your user has 1MILL followers? Are you going to keep this connection open for a week? lol If you need, run cron or save the redis / memcache command. Rinse and repeat until you get all the followers.
Please note: my code below is a class that runs through the cron command every minute. I am using Laravel 5.1. Therefore, you can probably ignore many of this code, as it is unique to my platform. For example, TwitterOAuth (which gets all oAuths from me on db), TwitterFollowerList is another table, and I check if the record exists, TwitterFollowersDaily is another table in which I store / update the total amount per day for the user and TwitterApi - this is the Abraham\TwitterOAuth . However, you can use any library.
This may give you a good idea of ββwhat you can do the same or even better. I will not explain the whole code because there is a lot going on, but you should be able to navigate through it. Let me know if you have any questions.
public function updateFollowers() { TwitterOAuth::chunk(200, function ($oauths) { foreach ($oauths as $oauth) { $page_id = $oauth->page_id; $follower_list = TwitterFollowerList::where('page_id', $page_id)->first(); if (!$follower_list || $follower_list->updated_at < Carbon::now()->subMinutes(15)) { $next_cursor = isset($follower_list->next_cursor) ? $follower_list->next_cursor : -1; $ids = isset($follower_list->follower_ids) ? $follower_list->follower_ids : []; $twitter = new TwitterApi($oauth->oauth_token, $oauth->oauth_token_secret); $results = $twitter->get("followers/ids", ["user_id" => $page_id, "cursor" => $next_cursor]); if (isset($results->errors)) continue; $ids = $results->ids; if ($results->next_cursor !== 0) { $ticks = 0; do { if ($ticks === 13) { $ticks = 0; break; } $ticks++; $results = $twitter->get("followers/ids", ["user_id" => $page_id, "cursor" => $results->next_cursor]); if (!$results) break; $more_ids = $results->ids; $ids = array_merge($ids, $more_ids); } while ($results->next_cursor > 0); } $stats = [ 'page_id' => $page_id, 'follower_count' => count($ids), 'follower_ids' => $ids, 'next_cursor' => ($results->next_cursor > 0) ? $results->next_cursor : null, 'updated_at' => Carbon::now() ]; TwitterFollowerList::updateOrCreate(['page_id' => $page_id], $stats); TwitterFollowersDaily::updateOrCreate([ 'page_id' => $page_id, 'date' => Carbon::now()->toDateString() ], [ 'page_id' => $page_id, 'date' => Carbon::now()->toDateString(), 'follower_count' => count($ids), ] ); continue; } } }); }
source share