Skip to content

Instantly share code, notes, and snippets.

@themattharris
Created February 5, 2012 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themattharris/1741907 to your computer and use it in GitHub Desktop.
Save themattharris/1741907 to your computer and use it in GitHub Desktop.
Processing nvvotecount from gist https://gist.github.com/1741669
<?php
// incase you miss some data. You might need to increment the page number.
// Remember, the max tweets returned by the Twitter API is 3200.
require '../tmhOAuth.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'YOUR_CONSUMER_KEY',
'consumer_secret' => 'YOUR_CONSUMER_SECRET',
'user_token' => 'A_USER_TOKEN',
'user_secret' => 'A_USER_SECRET',
));
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1/statuses/user_timeline'), array(
'user_id' => '475526099',
'since_id' => '166324951975067641',
'count' => '100',
'page' => '1'
));
if ($code != 200) {
echo "Bad connection";
die();
}
$data = $tmhOAuth->response['response'];
$tweets = json_decode($data, true);
$rows = array();
foreach ($tweets as $tweet) {
if (!empty($tweet)) {
// don't accept tweets from anyone other than @NVVoteCount
if ($tweet['user']['id'] != '475526099') return;
$rows[] = $tweet['id_str'] . ',' . $tweet['created_at'] . ',' . $tweet['text'] . PHP_EOL;
}
}
for ($i=count($rows); $i >= 0 ; $i--) {
echo $rows[$i];
}
?>
cat nvvotecount | sort | uniq > DEDUPED
TOTAL=`cat nvvotecount | wc -l`
DEDUPED=`cat DEDUPED | wc -l`
X=`cat DEDUPED | egrep "X " | wc -l`
R=`cat DEDUPED | egrep "R [0-9]+" | wc -l`
C=`cat DEDUPED | egrep "C [0-9]+" | wc -l`
T=`cat DEDUPED | egrep "T [0-9]+" | wc -l`
B=`cat DEDUPED | egrep "B [0-9]+" | wc -l`
K=`cat DEDUPED | egrep "K " | wc -l`
echo "Tweets: TOT:${DEDUPED} X:${X} R:${R} C:${C} T:${T} B:${B} K:${K} of ${TOTAL} sent"
<?php
$data = file_get_contents(dirname(__FILE__) . '/nvvotecount');
$data = explode(PHP_EOL, $data);
$results = array();
$precincts = array();
$reported = array();
foreach ($data as $row) {
$r = explode(',', $row);
if (!isset($r[2])) continue;
$r = $r[2];
switch ($r[0]) {
case 'X': continue; // not used in this code
case 'R': process_row($r); break;
case 'C': process_row($r); break;
case 'T': continue; // not used in this code
case 'B': continue; // not used in this code
case 'K': process_precinct($r); break;
}
}
results();
function process_precinct($row) {
$data = explode(' ', $row);
$code = array_shift($data);
$location = array_shift($data);
$county = array_shift($data);
$precinct = array_shift($data);
$name = array_shift($data);
global $precincts;
global $reported;
$precincts[$county][$precinct] = $name;
$reported[$county][$precinct] = false;
}
function process_row($row) {
$data = explode(' ', $row);
$code = array_shift($data);
$county = array_shift($data);
$_precinct = array_shift($data);
$paginator = array_pop($data);
global $results;
global $reported;
if (!isset($results[$county][$_precinct]))
$results[$county][$_precinct] = array();
$precinct = &$results[$county][$_precinct];
$reported[$county][$_precinct] = true;
for ($i=0; $i < count($data); $i++) {
$candidate = $data[$i];
$count = $data[$i+1];
$precinct[$candidate] = $count;
$i++;
}
}
function results() {
global $results;
global $precincts;
global $reported;
$candidate_counts;
$county_counts;
foreach ($results as $county => $county_data) {
foreach ($county_data as $precinct => $candidates) {
foreach ($candidates as $candidate => $count) {
if (!isset($county_counts[$county][$candidate]))
$county_counts[$county][$candidate] = 0;
if (!isset($candidate_counts[$candidate]))
$candidate_counts[$candidate] = 0;
$county_counts[$county][$candidate] += $count;
$candidate_counts[$candidate] += $count;
}
}
}
$counts = array();
$counts['reported']['total'] = 0;
foreach ($reported as $county => $_precincts) {
$counts[$county] = 0;
foreach ($_precincts as $precinct => $_reported) {
if (!isset($counts['reported'][$county]))
$counts['reported'][$county] = 0;
if ($_reported) {
$counts['reported'][$county]++;
$counts['reported']['total']++;
}
}
}
$counts['exists']['total'] = 0;
foreach ($precincts as $county => $_precincts) {
if (!isset($counts['exists'][$county]))
$counts['exists'][$county] = 0;
$counts['exists']['total'] += count($_precincts);
$counts['exists'][$county] = count($_precincts);
}
$cty = array();
foreach ($county_counts as $county => $values) {
$msg = $county . ' ';
$msg .= @$counts['exists'][$county] . ' IN ' . $counts['reported'][$county] . ' OF ';
foreach ($values as $candidate => $count) {
$msg .= $candidate . ':' . $count . ' ';
}
$cty[$county] = $msg . PHP_EOL;
}
ksort($cty);
foreach ($cty as $msg) echo $msg;
$msg = 'ALL ';
$msg .= $counts['exists']['total'] . ' IN ' . $counts['reported']['total'] . ' OF ';
foreach ($candidate_counts as $candidate => $count) {
$msg .= $candidate . ':' . $count . ' ';
}
echo $msg . PHP_EOL;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment