Skip to content

Instantly share code, notes, and snippets.

@michvaldes001
Last active May 4, 2016 16:03
Show Gist options
  • Save michvaldes001/4ff6dfb910271bc4d87ad0e27ab11906 to your computer and use it in GitHub Desktop.
Save michvaldes001/4ff6dfb910271bc4d87ad0e27ab11906 to your computer and use it in GitHub Desktop.
This is the script I use on my server to graph media negativity towards each candidate. It runs daily as a cron job.
<?php
require_once('store_data.php');
class CANDIDATE_DATA_GRAPH
{
private function scan_news_data($first_name, $last_name)
{
$candidate_yahoo_data = file_get_contents('https://news.search.yahoo.com/search?fr=uh3_news_vert_gs&type=2button&p=' . $first_name . '%20' . $last_name . '%20');
$candidate_google_data = file_get_contents('https://www.google.com/search?hl=en&gl=us&tbm=nws&authuser=0&q' . $first_name . '+' . $last_name);
$candidate_cnn_data = file_get_contents('http://www.cnn.com/search/?text=');
$candidate_nbc_data = file_get_contents('http://www.nbcnews.com/search/' . $first_name . '%20' . $last_name);
$candidate_nytimes_data = file_get_contents('http://query.nytimes.com/search/sitesearch/?action=click&contentCollection&region=TopBar&WT.nav=searchWidget&module=SearchSubmit&pgtype=Homepage#/' . $first_name . ' ' . $last_name);
$consolidated_data = $candidate_yahoo_data . $candidate_google_data . $candidate_cnn_data . $candidate_nbc_data . $candidate_nytimes_data;
return $consolidated_data;
}
private function negativity($negative_candidate_data)
{
$total_negative_counter = 0;
$negative_adjectives = array('aggressive','aloof','arrogant','belligerent','big-headed','bitchy','boastful','bone-idle','boring','bossy','callous','cantankerous','careless','changeable','clinging','compulsive','cowardly','cruel','cunning','cynical',
'deceitful','detached','dishonest','dogmatic','domineering','finicky','flirtatious','foolish','foolhardy','fussy','greedy','grumpy','gullible','harsh',
'impatient','impolite','impulsive','inconsiderate','inconsistent','indecisive','indiscreet','inflexible','interfering','intolerant','irresponsible',
'jealous lazy','Machiavellian','materialistic','mean','miserly','moody','narrowminded','nasty','naughty','nervous','obsessive obstinate', 'overcritical','overemotional','parsimonious','patronizing','perverse','pessimistic','pompous','possessive','pusillanimous','quarrelsome','quick-tempered','resentful','rude','ruthless','sarcastic','secretive','selfish','self-centred','self-indulgent','silly','sneaky','stingy', 'stubborn','stupid','superficial','tactless','timid','touchy','thoughtless','truculent','unkind','unpredictable','unreliable', 'untidy',
'untrustworthy','vague','vain','vengeful','vulgar','weak-willed');
foreach ($negative_adjectives as &$word)
{
$word_results = substr_count(($negative_candidate_data), $word);
if ($word_results != 0)
{
$total_negative_counter = $total_negative_counter + $word_results;
}
}
return $total_negative_counter;
}
private function positivity($positive_candidate_data)
{
$total_positive_counter = 0;
$positive_adjectives = array('adaptable','adventurous','affable','affectionate','agreeable','ambitious','amiable','amicable',
'amusing','brave','bright','broad-minded','calm','careful','charming','communicative','compassionate','conscientious','considerate',
'convivial','courageous','courteous','creative','decisive','determined','diligent','diplomatic','discreet','dynamic','easygoing','emotional',
'energetic','enthusiastic','exuberant','fair-minded','faithful','fearless','forceful','frank','friendly','funny','generous','gentle','good',
'gregarious','hard-working','helpful','honest','humorous','imaginative','impartial','independent','intellectual','intelligent','intuitive',
'inventive','kind','loving','loyal','modest','neat','nice','optimistic','passionate','patient','persistent','pioneering','philosophical',
'placid','plucky','polite','powerful','practical','pro-active','quick-witted','quiet','rational','reliable','reserved','resourceful','romantic','self-confident','self-disciplined','sensible','sensitive','sincere','sociable','straightforward','sympathetic','thoughtful',
'tidy','tough','unassuming','understanding','versatile','warmhearted','willing','witty');
foreach ($positive_adjectives as &$word)
{
$word_results = substr_count(($positive_candidate_data) , $word);
if ($word_results != 0)
{
$total_positive_counter = $total_positive_counter + $word_results;
}
}
return $total_positive_counter;
}
public function pass_data_to_chart()
{
$trump_data = $this -> scan_news_data('Donald', 'Trump');
$sanders_data = $this -> scan_news_data('Bernie','Sanders');
$clinton_data = $this -> scan_news_data('Hillary','Clinton');
$trump_negativity = $this -> negativity($trump_data);
$sanders_negativity = $this -> negativity($sanders_data);
$clinton_negativity = $this -> negativity($clinton_data);
$trump_positivity = $this -> positivity($trump_data);
$sanders_positivity = $this -> positivity($sanders_data);
$clinton_positivity = $this -> positivity($clinton_data);
$trump_negative_percent = round((($trump_negativity/$trump_positivity) * 100), 2);
$sanders_negative_percent = round((($sanders_negativity/$sanders_positivity) * 100), 2);
$clinton_negative_percent = round((($clinton_negativity/$clinton_positivity) * 100), 2);
$store_data = new DATA_STORE();
$store_data -> serialize('grapher_files/election/date.txt', date('M/d'), 30);
$store_data -> serialize('grapher_files/election/trump.txt', $trump_negative_percent, 30);
$store_data -> serialize('grapher_files/election/sanders.txt', $sanders_negative_percent, 30);
$store_data -> serialize('grapher_files/election/clinton.txt', $clinton_negative_percent, 30);
$store_data -> graph('line_graph', 'trump', 'grapher_files/election/', 'grapher_files/election/date.txt' , 'grapher_files/election/trump.txt');
$store_data -> graph('line_graph', 'sanders', 'grapher_files/election/', 'grapher_files/election/date.txt' , 'grapher_files/election/sanders.txt');
$store_data -> graph('line_graph', 'clinton', 'grapher_files/election/', 'grapher_files/election/date.txt' , 'grapher_files/election/clinton.txt');
}
}
$data = new CANDIDATE_DATA_GRAPH();
$data -> pass_data_to_chart();
?>
<?php
//This is required to serialize arrays and store them as text files.
//The graph generating library (grapher.php) is the same I uploaded on to here.
require_once('grapher.php');
class DATA_STORE
{
public function serialize($path_to_file, $data, $max_data_points)
{
if (!file_exists($path_to_file))
{
$data_array = array();
array_push($data_array, $data);
$file_create = fopen($path_to_file ,'w');
$serialized_array = serialize($data_array);
file_put_contents($path_to_file, $serialized_array);
fclose($file_create);
}
else
{
$recover_serialized_array = file_get_contents($path_to_file);
$recovered_array = unserialize($recover_serialized_array);
$recovered_array = $this -> truncate_array($recovered_array, $max_data_points);
array_push($recovered_array, $data);
$serialized_array = serialize($recovered_array);
file_put_contents($path_to_file, $serialized_array);
}
}
public function graph($graph_type, $image_name, $image_directory, $label_file, $data_file)
{
$recover_serialized_array_label = file_get_contents($label_file);
$recovered_array_label = unserialize($recover_serialized_array_label);
$recover_serialized_array_data = file_get_contents($data_file);
$recovered_array_data = unserialize($recover_serialized_array_data);
$graph_data_array = array('value' => $recovered_array_data, 'label' => $recovered_array_label);
$image_render = new GRAPH_CREATE();
$image_render -> gather_data($graph_type, 600,1000, $graph_data_array, 0, 0,$image_name, array(10, 200, 28), 1, 20, $image_directory, 0);
}
private function truncate_array($array_to_truncate, $max_data_points)
{
if (count($array_to_truncate) == $max_data_points)
{
array_shift($array_to_truncate);
}
return $array_to_truncate;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment