From 5c39bb548916dddfe0e88ab00e7b81b8f25a2886 Mon Sep 17 00:00:00 2001 From: Chris Gmyr Date: Thu, 18 Oct 2018 16:46:51 -0400 Subject: [PATCH 1/3] extract statuses method --- .../Console/Commands/UpdateTwitterList.php | 87 ++++++++++--------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php b/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php index d728f76..fef0709 100644 --- a/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php +++ b/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php @@ -47,46 +47,7 @@ public function handle() { $new_users = []; - $finished = false; - $raw_statuses = []; - $conditions = [ - 'user_id' => $this->user_id, - 'include_rts' => true, - 'exclude_replies' => false, - 'count' => 3200 - ]; - $cutoff_date = strtotime("1 month ago"); - $max_id = 0; - - echo "Going through the timeline...\n"; - - while (!$finished) { - $data = $this->connection->get( - "statuses/user_timeline", - $conditions - ); - - // Loop through and grab the oldest ID - foreach ($data as $status) { - if (strtotime($status->created_at) >= $cutoff_date) { - $raw_statuses[] = $status; - $max_id = $status->id; - } else { - break; - $finished = true; - } - } - $tweet_count = count($raw_statuses); - echo "{$tweet_count} tweets\n"; - - if (isset($conditions['max_id'])) { - if ($conditions['max_id'] == $max_id) { - $finished = true; - } - } - - $conditions['max_id'] = $max_id; - } + $raw_statuses = $this->getStatusesFromTimeline(); // First, eliminate any tweets that are older than a month echo "Grabbing interaction tweets...\n"; @@ -239,4 +200,50 @@ public function handle() echo "Added " . count($users_to_add) . " to the cycle list\n"; } } + + private function getStatusesFromTimeline() + { + $finished = false; + $raw_statuses = []; + $conditions = [ + 'user_id' => $this->user_id, + 'include_rts' => true, + 'exclude_replies' => false, + 'count' => 3200 + ]; + $cutoff_date = strtotime("1 month ago"); + $max_id = 0; + + echo "Going through the timeline...\n"; + + while (!$finished) { + $data = $this->connection->get( + "statuses/user_timeline", + $conditions + ); + + // Loop through and grab the oldest ID + foreach ($data as $status) { + if (strtotime($status->created_at) >= $cutoff_date) { + $raw_statuses[] = $status; + $max_id = $status->id; + } else { + break; + $finished = true; + } + } + $tweet_count = count($raw_statuses); + echo "{$tweet_count} tweets\n"; + + if (isset($conditions['max_id'])) { + if ($conditions['max_id'] == $max_id) { + $finished = true; + } + } + + $conditions['max_id'] = $max_id; + } + + return $raw_statuses; + } } From 7324e6b1c00aec14f30791d39fa73671f3a11e1d Mon Sep 17 00:00:00 2001 From: Chris Gmyr Date: Thu, 18 Oct 2018 16:47:37 -0400 Subject: [PATCH 2/3] make cutoff date global --- .../app/Console/Commands/UpdateTwitterList.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php b/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php index fef0709..b391899 100644 --- a/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php +++ b/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php @@ -23,6 +23,7 @@ class UpdateTwitterList extends Command protected $user_id; protected $connection; protected $list_id; + protected $cutoff_date; /** * Create a new command instance. @@ -36,6 +37,7 @@ public function __construct() $this->user_id = config('services.twitter.user'); $this->connection = app('Twitter'); $this->list_id = config('services.twitter.list'); + $this->cutoff_date = strtotime("1 month ago"); } /** @@ -99,7 +101,7 @@ public function handle() // Loop through and grab the oldest ID foreach ($data as $favorite) { - if (strtotime($favorite->created_at) >= $cutoff_date) { + if (strtotime($favorite->created_at) >= $this->cutoff_date) { $favorites[] = $favorite; $max_id = $favorite->id; } else { @@ -211,7 +213,6 @@ private function getStatusesFromTimeline() 'exclude_replies' => false, 'count' => 3200 ]; - $cutoff_date = strtotime("1 month ago"); $max_id = 0; echo "Going through the timeline...\n"; @@ -224,7 +225,7 @@ private function getStatusesFromTimeline() // Loop through and grab the oldest ID foreach ($data as $status) { - if (strtotime($status->created_at) >= $cutoff_date) { + if (strtotime($status->created_at) >= $this->cutoff_date) { $raw_statuses[] = $status; $max_id = $status->id; } else { From 15450242c7d8445c87e5612808608a0e0e36706b Mon Sep 17 00:00:00 2001 From: Chris Gmyr Date: Thu, 18 Oct 2018 16:50:12 -0400 Subject: [PATCH 3/3] cleanup getStatusesFromTimeline a little --- .../app/Console/Commands/UpdateTwitterList.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php b/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php index b391899..5f708c4 100644 --- a/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php +++ b/20181018/twitter/laravel-base/app/Console/Commands/UpdateTwitterList.php @@ -205,7 +205,8 @@ public function handle() private function getStatusesFromTimeline() { - $finished = false; + $this->info('Going through the timeline...'); + $raw_statuses = []; $conditions = [ 'user_id' => $this->user_id, @@ -215,8 +216,7 @@ private function getStatusesFromTimeline() ]; $max_id = 0; - echo "Going through the timeline...\n"; - + $finished = false; while (!$finished) { $data = $this->connection->get( "statuses/user_timeline", @@ -233,8 +233,9 @@ private function getStatusesFromTimeline() $finished = true; } } + $tweet_count = count($raw_statuses); - echo "{$tweet_count} tweets\n"; + $this->info("{$tweet_count} tweets"); if (isset($conditions['max_id'])) { if ($conditions['max_id'] == $max_id) {