Created
August 12, 2022 06:50
-
-
Save richardkeep/0e63549d3533ab6622e2e914c0c6a3f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Support\Facades\Http; | |
use Illuminate\Support\Facades\Cache; | |
use Illuminate\Console\Command; | |
use Abraham\TwitterOAuth\TwitterOAuth; | |
class Tweet extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'tweet'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Tweet provisional presidential results'; | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
$connection = new TwitterOAuth( | |
env('TWITTER_API_KEY'), | |
env('TWITTER_API_SECRET'), | |
env('ACCESS_TOKEN'), | |
env('ACCESS_TOKEN_SECRET') | |
); | |
$res = Http::accept('application/json') | |
->get(config('services.ntv.endpoint')); | |
$postComparison = false; | |
if (Cache::has('lastNtvName1') || Cache::get('lastName1')) { | |
$postComparison = true; | |
$lastNtvName1 = Cache::get('lastNtvName1'); | |
$lastNtvVotes1 = Cache::get('lastNtvVotes1'); | |
$lastNtvName2 = Cache::get('lastNtvName2'); | |
$lastNtvVotes2 = Cache::get('lastNtvVotes2'); | |
$lastName1 = Cache::get('lastName1'); | |
$lastVotes1 = Cache::get('lastVotes1'); | |
$lastName2 = Cache::get('lastName2'); | |
$lastVotes2 = Cache::get('lastVotes2'); | |
} | |
$ntvdata = $res->json()['national']['candidates']; | |
$ntvTotalVotes = number_format($res->json()['national']['total_votes_cast']); | |
$ntvname1 = $ntvdata[0]['FIRST_NAME'] == "WILLIAM" ? 'RUTO' : $ntvdata[0]['FIRST_NAME']; | |
$ntvvotes1 = $ntvdata[0]['CANDIDATE_VOTES']; | |
$ntvreadableVotes1 = number_format($ntvvotes1); | |
$ntvpercent1 = round($ntvdata[0]['NATIONAL_PERCENTAGE'], 2); | |
$ntvname2 = $ntvdata[1]['FIRST_NAME'] == "WILLIAM" ? 'RUTO' : $ntvdata[1]['FIRST_NAME']; | |
$ntvvotes2 = $ntvdata[1]['CANDIDATE_VOTES']; | |
$ntvreadableVotes2 = number_format($ntvvotes2); | |
$ntvpercent2 = round($ntvdata[1]['NATIONAL_PERCENTAGE'], 2); | |
$ntvreadableDiff = number_format($ntvvotes1 - $ntvvotes2); | |
if ($postComparison) { | |
if ($ntvname1 == $lastNtvName1) { | |
$compareTweetNtvPerson1 = $ntvname1 . " gained " . number_format($ntvvotes1 - $lastNtvVotes1). " votes"; | |
} else { | |
$compareTweetNtvPerson1 = $ntvname1 . " gained " . number_format($ntvvotes1 - $lastNtvVotes2). " votes"; | |
} | |
if ($ntvname2 == $lastNtvName2) { | |
$compareTweetNtvPerson2 = $ntvname2 . " gained " . number_format($ntvvotes2 - $lastNtvVotes2). " votes"; | |
} else { | |
$compareTweetNtvPerson2 = $ntvname2 . " gained " . number_format($ntvvotes2 - $lastNtvVotes1). " votes"; | |
} | |
} | |
Cache::forever('lastNtvName1', $ntvname1); | |
Cache::forever('lastNtvVotes1', $ntvvotes1); | |
Cache::forever('lastNtvName2', $ntvname2); | |
Cache::forever('lastNtvVotes2', $ntvvotes2); | |
$res = Http::accept('application/json') | |
->withHeaders([ | |
'api-key' => config('services.citizen.api_key') | |
]) | |
->get(config('services.citizen.endpoint')); | |
$citizenTotalVotes = number_format($res->collect()->sum('total_votes')); | |
$name1 = $res->json()[0]['candidate_popular_name']; | |
$votes1 = $res->json()[0]['total_votes']; | |
$readableVotes1 = number_format($votes1); | |
$percent1 = $res->json()[0]['percentage']; | |
$name2 = $res->json()[1]['candidate_popular_name']; | |
$votes2 = $res->json()[1]['total_votes']; | |
$readableVotes2 = number_format($votes2); | |
$percent2 = $res->json()[1]['percentage']; | |
$readableDiff = number_format($votes1 - $votes2); | |
if ($postComparison) { | |
if ($name1 == $lastName1) { | |
$compareTweetCitizenPerson1 = $name1 . " gained " . number_format($votes1 - $lastVotes1) . " votes"; | |
} else { | |
$compareTweetCitizenPerson1 = $name1 . " gained " . number_format($votes1 - $lastVotes2). " votes"; | |
} | |
if ($name2 == $lastName2) { | |
$compareTweetCitizenPerson2 = $name2 . " gained " . number_format($votes2 - $lastVotes2). " votes"; | |
} else { | |
$compareTweetCitizenPerson2 = $name2 . " gained " . number_format($votes2 - $lastVotes1). " votes"; | |
} | |
} | |
Cache::forever('lastName1', $name1); | |
Cache::forever('lastVotes1', $votes1); | |
Cache::forever('lastName2', $name2); | |
Cache::forever('lastVotes2', $votes2); | |
$citizenTweet = "Citizen | |
{$name1} - {$readableVotes1} ({$percent1}%) | |
{$name2} - {$readableVotes2} ({$percent2}%) | |
Diff: {$readableDiff} | |
Total: {$citizenTotalVotes} | |
"; | |
$ntvTweet = "NTV | |
{$ntvname1} - {$ntvreadableVotes1} ({$ntvpercent1}%) | |
{$ntvname2} - {$ntvreadableVotes2} ({$ntvpercent2}%) | |
Diff: {$ntvreadableDiff} | |
Total: {$ntvTotalVotes} | |
"; | |
$tweet = $citizenTweet."\n\n".$ntvTweet."\n\n #KenyaElections2022"; | |
$status = $connection->post("statuses/update", [ | |
"status" => $tweet, | |
]); | |
if ($postComparison) { | |
$compareTweet = "Trend (Last 20 mins) | |
Citizen | |
{$compareTweetCitizenPerson1} | |
{$compareTweetCitizenPerson2} | |
NTV | |
{$compareTweetNtvPerson1} | |
{$compareTweetNtvPerson2} | |
"; | |
if (\property_exists($status, 'id')) { | |
$connection->post("statuses/update", [ | |
"status" => $compareTweet. "\n #KenyaElections2022", | |
"in_reply_to_status_id" => $status->id, | |
]); | |
} | |
} else { | |
info('no comparisons'); | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment