Created
November 5, 2019 23:44
-
-
Save ChrisHardie/8be703037b451b946218acf3b0d499a3 to your computer and use it in GitHub Desktop.
A PHP script to check for results in the Wayne County, Indiana 2019 Mayoral Election
This file contains hidden or 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 | |
require __DIR__ . '/vendor/autoload.php'; | |
require_once __DIR__ . '/config.php'; | |
$output_messages = array(); | |
$wayneco_results_html = file_get_contents( WAYNECO_RESULTS_URL ); | |
$state_results_html = file_get_contents( STATE_RESULTS_URL ); | |
if ( 100 > strlen( $wayneco_results_html ) ) { | |
$output_messages[] = 'Something wrong with fetching County Clerk results content.'; | |
} | |
if ( 100 > strlen( $state_results_html ) ) { | |
$output_messages[] = 'Something wrong with fetching State results JSON.'; | |
} | |
$wayneco_results_hash = hash( 'md5', $wayneco_results_html ); | |
$state_results_json = json_decode( $state_results_html, true ); | |
$changes_to_write = false; | |
$mayoral_candidate_results = array(); | |
foreach ( $state_results_json['Root']['StatewideSummary']['Race'] as $race ) { | |
if ( STATE_OFFICE_ID === (int) $race['OFFICEID'] ) { | |
$mayoral_candidate_results = array_combine( array_column( $race['Candidates']['Candidate'], 'NAME_ON_BALLOT' ), $race['Candidates']['Candidate'] ); | |
} | |
} | |
if ( file_exists( __DIR__ . '/' . DATA_FILE ) && is_writable( __DIR__ . '/' . DATA_FILE ) ) { | |
$previous_data = json_decode( file_get_contents( __DIR__ . '/' . DATA_FILE ), true ); | |
if ( $previous_data['wayneco_hash'] === $wayneco_results_hash ) { | |
// $output_messages[] = 'County Clerk results page has not changed.'; | |
} else { | |
$output_messages[] = 'County Clerk results page may have changed, visit ' . WAYNECO_RESULTS_URL; | |
$changes_to_write = true; | |
} | |
if ( $previous_data['state_results'] === $mayoral_candidate_results ) { | |
// $output_messages[] = 'State race results data has not changed.'; | |
} else { | |
$message = 'Latest State race results: ' . PHP_EOL; | |
foreach ( $mayoral_candidate_results as $candidate ) { | |
$message .= sprintf( '* %s: %s votes', $candidate['NAME_ON_BALLOT'], $candidate['TOTAL'] ) . PHP_EOL; | |
} | |
$message .= 'View https://indianaenr.blob.core.usgovcloudapi.net/site/index.html for more.' . PHP_EOL; | |
$output_messages[] = $message; | |
$changes_to_write = true; | |
} | |
if ( true === $changes_to_write ) { | |
$new_data = array(); | |
$new_data['wayneco_hash'] = $wayneco_results_hash; | |
$new_data['state_results'] = $mayoral_candidate_results; | |
file_put_contents( __DIR__ . '/' . DATA_FILE, json_encode( $new_data ) ); | |
} | |
} else { | |
$output_messages[] = 'Creating results data file for the first time.'; | |
$data = array(); | |
$data['wayneco_hash'] = $wayneco_results_hash; | |
$data['state_results'] = $mayoral_candidate_results; | |
file_put_contents( __DIR__ . '/' . DATA_FILE, json_encode( $data ) ); | |
} | |
if ( 0 < count( $output_messages ) ) { | |
// Connect to Slack. | |
$slack_settings = [ | |
'username' => 'election-bot', | |
'channel' => '#general', | |
'link_names' => true, | |
'icon' => ':robot_face:', | |
]; | |
$slack_client = new Maknz\Slack\Client( SLACK_WEBHOOK, $slack_settings ); | |
// Send our messages. | |
$slack_client->send( implode( PHP_EOL, $output_messages ) ); | |
} | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment