Created
August 25, 2015 13:27
-
-
Save mhauri/66ae26d7ed547fb47016 to your computer and use it in GitHub Desktop.
Send latest CommitStrip Comic to a HipChat Room
This file contains 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 | |
$storageFile = __DIR__ . '/commitstrip.lock'; | |
$url = "http://www.commitstrip.com/en/feed/"; | |
$content = file_get_contents($url); | |
$xml = new SimpleXmlElement($content); | |
$item = $xml->channel->item[0]; | |
$title = (string)$item->title; | |
$link = (string)$item->link; | |
$date = (string)$item->pubDate; | |
$content = (string)$item->children('content', true)->encoded; | |
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $content, $match); | |
if(isset($match[0]) && isset($match[0][0])) { | |
$html = $title . '<br /><a href="' . $link . '"><img src="' . $match[0][0] . '"></a>'; | |
$hash = sha1($html); | |
if(file_get_contents($storageFile) !== $hash) { | |
file_put_contents($storageFile, $hash); | |
$params = array( | |
'from_name' => 'CommitStrip', | |
'color' => 'yellow', | |
'notify' => 'true', | |
'message_format' => "html", | |
'message' => $html | |
); | |
$url = sprintf('http://api.hipchat.com/v2/room/%d/notification?auth_token=%s', YOUR_ROOM_ID, 'YOUR_TOKEN'); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_NOBODY, 0); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
curl_exec($ch); | |
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
if (intval($status) === 204) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment