Skip to content

Instantly share code, notes, and snippets.

@riaf
Created February 2, 2012 04:31
Show Gist options
  • Save riaf/1721490 to your computer and use it in GitHub Desktop.
Save riaf/1721490 to your computer and use it in GitHub Desktop.
Facebook の API に何かあったときに IRC で通知すると便利じゃないかと思いまして ref: http://qiita.com/items/1976
<?php
require_once 'Net/Socket/Tiarra.php';
$api_uri = 'http://www.facebook.com/feeds/api_status.php';
$db = __DIR__ . '/log.sqlite';
$pdo = new PDO("sqlite:$db");
$tiarra_socket = 'riaf';
$irc_channel = '#nequal@freenode';
$last_row = $pdo->query('SELECT * FROM log ORDER BY created_at DESC')->fetch(PDO::FETCH_ASSOC);
if ($last_row) {
$last = array(
'current' => json_decode($last_row['current'], true),
'push' => json_decode($last_row['push'], true),
'created_at' => new DateTime($last_row['created_at']),
);
} else {
$last = null;
}
$now = new DateTime;
$response = json_decode(file_get_contents($api_uri), true);
try {
if (!isset($response['current'])) {
throw new Exception('`current` is not found');
}
if (!isset($response['push'])) {
throw new Exception('`push` is not found');
}
if ($last) {
if ($last['current']['health'] != $response['current']['health']) {
$tiarra = new Net_Socket_Tiarra($tiarra_socket);
$tiarra->message($irc_channel, sprintf('10Facebook Status: %s (%s)', $response['current']['subject'], $response['current']['health']));
}
}
if (is_null($last) || $last['current']['health'] != $response['current']['health'] || $last['push']['id'] != $response['push']['id']) {
$insert = $pdo->prepare('INSERT INTO `log` (current, push, created_at) VALUES (:current, :push, :created_at);');
$insert->execute(array(
'current' => json_encode($response['current']),
'push' => json_encode($response['push']),
'created_at' => $now->format('Y-m-d H:i:s'),
));
}
} catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
pear channel-discover openpear.org
pear install Net_Socket_Tiarra
CREATE TABLE IF NOT EXISTS log (
current TEXT,
push TEXT,
created_at DATETIME
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment