Skip to content

Instantly share code, notes, and snippets.

@kopiro
Created May 20, 2012 20:07
Show Gist options
  • Save kopiro/2759381 to your computer and use it in GitHub Desktop.
Save kopiro/2759381 to your computer and use it in GitHub Desktop.
Kopiro Twerremoto
/*
Se siete su Linux:
sudo apt-get install php5 curl
Come installarlo:
sudo curl http://d3macfshcnzosd.cloudfront.net/010762604_prev.mp3 -o /usr/bin/bip.mp3;
sudo curl http://d3macfshcnzosd.cloudfront.net/006202102_prev.mp3 -o /usr/bin/alarm.mp3;
sudo curl https://raw.github.com/gist/2759381 -o /usr/bin/twerr.php;
sudo chmod +x /usr/bin/twerr.php
Come configurarlo:
sudo nano /usr/bin/twerr.php
Quindi inserite le vostre credenziali in USERNAME e PASSWORD:
Come eseguirlo:
twerr.php
Come capire quando il terremoto arriva:
Quando i bip si fanno più ripetuti.
*/
#!/usr/bin/php
<?php
set_time_limit(-1);
define('USERNAME', 'HERE_USERNAME');;
define('PASSWORD', 'HERE_PASSWORD');
define('KEYWORD', '#terremoto');
$key = array('nuova','ancora','altr','seconda');
$count = 0.0;
function is_alert($text)
{
global $key;
foreach ($key as $k => $v)
{
if ( preg_match("/$v/i", $text) )
{
return true;
}
}
return false;
}
function process_tweet($data)
{
$text = $data['text'];
if ( substr($text, 0, 2)=='RT' ) return;
global $count;
$alert = false;
if ( is_alert($text) )
{
$alert = true;
$count = $count + 1;
}
else
{
if ($count-0.2<=0) $count = 0.0;
else $count = $count - 0.2;
}
if ($alert)
{
if ($count>=1.5)
{
system("afplay /usr/bin/bip.mp3");
}
if ($count>=3.0)
{
system("afplay /usr/bin/alarm.mp3");
}
}
echo "$count \t $text \n";
}
while(1)
{
$fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30);
if (!$fp) continue;
$request = "GET /1/statuses/filter.json?track=";
$request .= urlencode(KEYWORD)." HTTP/1.1\r\n";
$request .= "Host: stream.twitter.com\r\n";
$request .= "Authorization: Basic ";
$request .= base64_encode(USERNAME.':'.PASSWORD);
$request .= "\r\n\r\n";
fwrite($fp, $request);
stream_set_blocking($fp, 0);
while(!feof($fp))
{
$read = array($fp);
$write = null;
$except = null;
$res = stream_select($read, $write, $except, 600, 0);
if (!$res) continue;
$json = fgets($fp);
if (!$json) continue;
$data = json_decode($json, true);
if (!$data) continue;
process_tweet($data);
}
}
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment