2020-10-15 16:55:16 +00:00
< ? php
class UsaElection extends VirtualDeviceManager
{
private $api_uri = 'https://ft-ig-content-prod.s3.eu-west-1.amazonaws.com/v2/Financial-Times/ig-rcp-polls-backend/2020-presidential/latest/presidential-races.json' ; // Your redirect uri
private $virtual_device_name = " Election " ;
private $subdevice_type = " election " ;
2020-10-22 14:42:49 +00:00
function make ()
2020-10-15 16:55:16 +00:00
{
2020-10-22 14:42:49 +00:00
try {
if ( DeviceManager :: registeret ( $this -> virtual_device_name )) {
$deviceId = DeviceManager :: getDeviceByToken ( $this -> virtual_device_name )[ 'device_id' ];
$dataItems = [ 'Trump' , 'Biden' , 'Unknown' ];
foreach ( $dataItems as $dataItem ) {
if ( ! $subDevice = SubDeviceManager :: getSubDeviceByMaster ( $deviceId , strtolower ( $dataItem ))) {
SubDeviceManager :: create ( $deviceId , strtolower ( $dataItem ), '% ' . $dataItem );
sleep ( 1 );
}
2020-10-15 16:55:16 +00:00
}
2020-10-22 14:42:49 +00:00
if ( ! $this -> fetchEnabled ( $deviceId , $subDevice [ 'subdevice_id' ])) die ();
2020-10-15 16:55:16 +00:00
2020-10-22 14:42:49 +00:00
$finalUrl = $this -> api_uri ;
$json = json_decode ( Utilities :: CallAPI ( 'GET' , $finalUrl ), true );
2020-10-15 16:55:16 +00:00
2020-10-22 14:42:49 +00:00
$voteSpectrum = [
'republican' => [
'solid' => 0 ,
'leaning' => 0 ,
],
'democrat' => [
'solid' => 0 ,
'leaning' => 0 ,
],
'tossup' => 0
];
2020-10-15 16:55:16 +00:00
2020-10-22 14:42:49 +00:00
foreach ( $json as $state ) {
if ( $state [ 'raceCategory' ] != 'tossup' ) {
$raceCategory = explode ( '-' , $state [ 'raceCategory' ]);
$voteSpectrum [ $raceCategory [ 0 ]][ $raceCategory [ 1 ]] = $voteSpectrum [ $raceCategory [ 0 ]][ $raceCategory [ 1 ]] + $state [ 'raceDelegates' ];
} else {
$voteSpectrum [ 'tossup' ] = $voteSpectrum [ 'tossup' ] + $state [ 'raceDelegates' ];
}
2020-10-15 16:55:16 +00:00
}
2020-10-22 14:42:49 +00:00
$Trump = $voteSpectrum [ 'republican' ][ 'solid' ] + $voteSpectrum [ 'republican' ][ 'leaning' ];
$Biden = $voteSpectrum [ 'democrat' ][ 'solid' ] + $voteSpectrum [ 'democrat' ][ 'leaning' ];
$Unknown = $voteSpectrum [ 'tossup' ];
2020-10-15 16:55:16 +00:00
2020-10-22 14:42:49 +00:00
$OnePercent = ( $Trump + $Biden + $Unknown ) / 100 ;
2020-10-15 16:55:16 +00:00
2020-10-22 14:42:49 +00:00
foreach ( $dataItems as $Category ) {
RecordManager :: create ( $deviceId , strtolower ( $Category ), round (( $$Category / $OnePercent )));
}
} else {
DeviceManager :: create ( $this -> virtual_device_name , $this -> virtual_device_name );
DeviceManager :: approved ( $this -> virtual_device_name );
2020-10-15 16:55:16 +00:00
}
2020-10-22 14:42:49 +00:00
return 'sucessful' ;
} catch ( Exception $e ) {
return 'exception: ' . $e -> getMessage ();
2020-10-15 16:55:16 +00:00
}
}
}