Last active
December 21, 2015 21:02
-
-
Save maple-nishiyama/800b83eb4e3e2a886329 to your computer and use it in GitHub Desktop.
Twitter のタイムラインの監視をしてデスクトップに通知する
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'; | |
define('CONSUMER_KEY', ''); | |
define('CONSUMER_SECRET', ''); | |
define('ACCESS_TOKEN', ''); | |
define('ACCESS_TOKEN_SECRET', ''); | |
date_default_timezone_set('Asia/Tokyo'); | |
ini_set('xdebug.var_display_max_depth', -1); | |
ini_set('xdebug.var_display_max_data', -1); | |
ini_set('xdebug.var_display_max_children', -1); | |
function main() { | |
$lastReadId = 0; | |
$to = new TwistOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET); | |
//main_loop($to, $lastReadId); | |
streaming($to); | |
} | |
function main_loop($to, $lastReadId) { | |
while (true) { | |
try { | |
$statuses = getStatuses($to); | |
$unreads = filterUnread($lastReadId, $statuses); | |
usort($unreads, function($a, $b) { | |
return $a->id - $b->id; | |
}); | |
if (!empty($unreads)) { | |
// echo date('Y-m-d H:i:s') . ":新着 " . count($unreads) . "件\n"; | |
$last = $unreads[count($unreads) - 1]; | |
$lastReadId = $last->id; | |
showStatuses($unreads); | |
} else { | |
// echo date('Y-m-d H:i:s') . ":未読はありません\n"; | |
} | |
} catch (TwistException $e) { | |
echo $e->getMessage() . "\n"; | |
} | |
sleep(30); | |
} | |
} | |
function streaming($to) { | |
$to->streaming('user', function($status) use($to) { | |
if (empty($status->friends)) { | |
showStatus($status); | |
notifyOnDesktop($status); | |
} | |
}); | |
} | |
function getStatuses($to) { | |
$statuses = $to->get('lists/statuses', array('list_id' => '175659571')); | |
return $statuses; | |
} | |
function filterUnread($lastReadId, $statuses) { | |
return array_filter($statuses, function ($status) use ($lastReadId) { | |
return $status->id > $lastReadId; | |
}); | |
} | |
function showStatuses($statuses) { | |
foreach ($statuses as $status) { | |
showStatus($status); | |
notifyOnDesktop($status); | |
} | |
} | |
function showStatus($st) { | |
if (empty($st)) { | |
return; | |
} | |
$time = date("Y-m-d H:i:s ", strtotime($st->created_at)); | |
$text = $st->text; | |
$isRetweet = !empty($st->retweeted_status); | |
$user = $isRetweet ? $st->retweeted_status->user->name : $st->user->name; | |
$retweetedBy = $isRetweet ? $st->user->name : null; | |
echo "=================================\n"; | |
echo "$time\n"; | |
echo "$text\n"; | |
echo "by $user"; | |
echo $isRetweet ? " retweeted by $retweetedBy\n" : "\n"; | |
echo "\n"; | |
} | |
function notifyOnDesktop($st) { | |
if (empty($st)) { | |
return; | |
} | |
$text = $st->text; | |
$isRetweet = !empty($st->retweeted_status); | |
$user = $isRetweet ? $st->retweeted_status->user->name : $st->user->name; | |
$retweetedBy = $isRetweet ? $st->user->name : null; | |
$icon = $st->user->profile_image_url; | |
$statusId = $st->id_str; | |
$screenName = $st->user->name; | |
$url = "https://twitter.com/$screenName/status/$statusId"; | |
$title = escapeshellarg($user); | |
$subtitle = escapeshellarg($retweetedBy); | |
$message = escapeshellarg($text); | |
$cmd = "terminal-notifier -title $title -subtitle $subtitle -message $message -appIcon $icon -open $url"; | |
//$cmd = "osascript -e 'display notification \"$message\" with title \"$title\" subtitle \"$subtitle\"'"; | |
exec($cmd); | |
} | |
function sanitize($string) { | |
$search = [ | |
'#', '&', ';', '`', '|', | |
'*', '?', '~', '<', '>', | |
'^', '(', ')', '[', ']', | |
'{', '}', '$', '\\', '"', | |
]; | |
$replace = [ | |
'#', '&', ';', '`', '|', | |
'*', '?', '〜', '<', '>', | |
'^', '(', ')', '[', ']', | |
'{', '}', '$', '\', "”", | |
]; | |
return str_replace($search, $replace, $string); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment