Parent Directory
|
Revision Log
Improved lasttime handling
<?php // $Id: ohloh2twitter.php,v 1.6 2009/04/13 08:35:01 stronk7 Exp $
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Ohloh2Twitter simple gateway //
// http://http://cvs.moodle.org/contrib/tools/ohloh2twitter //
// //
// Copyright (C) 2008-3008 Eloy Lafuente (stronk7) //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
require_once (dirname(__FILE__) . '/config.php');
$quiet = isset($argv[1]) && strstr($argv[1], 'quiet') ? true : false; /// Only update datetime stamps, don't push items
if ($quiet) {
echo "quiet mode: only datetime stamps will be updated. Items won't be pushed from Ohloh to Twitter\n";
} else {
echo "normal mode: items will be pushed from Ohloh to Twitter\n";
}
$ohloh_request = 'http://www.ohloh.net/accounts/' . $o2t->ohloh_account . '/messages.xml?api_key=' . $o2t->ohloh_api_key;
if (!$ohlohxml = curl_request($ohloh_request)) {
process_error('inaccessible_ohloh_request');
}
if (!$ohlohobj = simplexml_load_string($ohlohxml)) {
process_error('wrong_ohloh_xml');
}
if ($ohlohobj->status != 'success' || $ohlohobj->items_returned == 0) {
process_error('empty_ohloh_xml');
}
$lasttime = 0;
if (file_exists(dirname(__FILE__) . '/ohloh2twitter.last')) {
$lasttime = file_get_contents(dirname(__FILE__) . '/ohloh2twitter.last');
}
$newmessages = array();
foreach ($ohlohobj->result->message as $message) {
if ($message->account != $o2t->ohloh_account) {
process_error('wrong_account');
}
if ((string)$message->created_at > $lasttime) {
$newmessages[] = $message;
$lasttime = (string)$message->created_at;
}
}
if ($quiet) {
if (file_put_contents(dirname(__FILE__) . '/ohloh2twitter.last', $lasttime) === false) {
process_error('write_last_error');
}
} else if ($newmessages) {
$newmessages = array_reverse($newmessages);
foreach ($newmessages as $newmessage) {
echo "sending message " . mb_substr($newmessage->body, 0, 20, 'UTF-8') . "...\n";
if (mb_strlen($newmessage->body, 'UTF-8') > 140) { /// 140cc limit in Twitter messages
$newmessage->body = mb_substr($newmessage->body, 0, 136, 'UTF-8') . '...';
}
if (!$twitterxml = curl_request('http://twitter.com/statuses/update.xml',
array('source' => 'ohloh2twitter', 'status' => $newmessage->body),
$o2t->twitter_account,
$o2t->twitter_password)) {
process_error('inaccessible_twitter_request');
}
if (!$twitterobj = simplexml_load_string($twitterxml)) {
process_error('wrong_twitter_xml');
}
if (!empty($twitterobj->error)) {
process_error('twitter error: ' . $twitterobj->error);
}
$lasttime = (string)$newmessage->created_at;
if (file_put_contents(dirname(__FILE__) . '/ohloh2twitter.last', $lasttime) === false) {
process_error('write_last_error');
}
}
}
function curl_request($url, $postfields = array(), $user = null, $pass = null) {
global $o2t;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'ohloh2twitter 0.9');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); /// Unset the Expect header. Shouldn't be here in HTTP/1.1
///curl_setopt($ch, CURLOPT_VERBOSE, '1');
if (!empty($o2t->proxy_array) && $o2t->proxy_array['proxy_protocol'] != '') {
curl_setopt($ch, CURLOPT_PROXY, $o2t->proxy_array['proxy_host'].":".$o2t->proxy_array['proxy_port']);
if (!empty($o2t->proxy_array['proxy_user']) && !empty($o2t->proxy_array['proxy_pass'])) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $o2t->proxy_array['proxy_user'].":".$o2t->proxy_array['proxy_pass']);
}
}
if ($postfields) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
}
if ($user && $pass) {
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
}
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = '';
}
curl_close($ch);
return $result;
}
function process_error($message) {
echo $message . "\n";
die;
}
?>
| Moodle CVS Admin | ViewVC Help |
| Powered by ViewVC 1.0.7 |