(Posted via Twitter on %d/%m/%Y at %H:%M)";
private $post_title = "";
public function __construct($file=false)
{
if ($file)
$this->cache_file = $file;
else
$this->cache_file = dirname(__FILE__) . '/.twrelaycache';
if (file_exists($this->cache_file))
$this->cache_content = unserialize(file_get_contents($this->cache_file));
}
public function setSkyrockLogin($username, $password)
{
if ($username[0] != '#')
$username = '#' . $username;
$this->username = $username;
$this->password = $password;
}
public function setTwitterRssUrl($url)
{
$this->tw_rss_url = $url;
}
public function setPostDefaults($title, $footer)
{
$this->post_title = $title;
$this->post_footer = $footer;
}
public function processRelay()
{
$tw = $this->extractStatusesFromTwitterRss();
foreach ($tw as $id=>&$status)
{
if (array_key_exists($id, $this->cache_content))
continue;
$footer = str_ireplace('', '', $this->post_footer);
$footer = strftime($footer, $status['date']);
$title = str_ireplace('', '', $this->post_title);
$title = strftime($title, $status['date']);
$content = array(
'title' => new XML_RPC_Value($title, 'string'),
'description' => new XML_RPC_Value($status['content'] . " " . $footer, 'string')
);
$params = array(
new XML_RPC_Value('blabla', 'string'), // blogid
new XML_RPC_Value($this->username, 'string'), // username
new XML_RPC_Value($this->password, 'string'), // password
new XML_RPC_Value($content, 'struct'), // post content
new XML_RPC_Value(true, 'boolean') // publish
);
$message = new XML_RPC_Message('metaWeblog.newPost', $params);
$client = new XML_RPC_Client("/api/xmlrpc.php", 'www.skyrock.com', 80);
$result = $client->send($message);
if (!$result)
throw new Exception("XML_RPC request failed with no apparent reason. Duh!");
elseif ($result->faultCode())
throw new Exception("XML_RPC request failed with error : ".$result->faultCode().' - '.$result->faultString());
else
{
$this->cache_content[$id] = true;
}
}
file_put_contents($this->cache_file, serialize($this->cache_content));
return true;
}
private function extractStatusesFromTwitterRss()
{
if (empty($this->tw_rss_url))
throw new Exception("Twitter RSS url is empty.");
$statuses = array();
$file = file_get_contents($this->tw_rss_url);
preg_match_all('!- .*
!sU', $file, $items, PREG_SET_ORDER);
unset($file);
foreach ($items as &$item)
{
$content = $link = $date = $id = false;
preg_match_all('!<([^>]+)>([^<]+)\\1>!', $item[0], $tags, PREG_SET_ORDER);
foreach ($tags as &$tag)
{
$tagName = strtolower($tag[1]);
if ($tagName == 'description')
{
$content = preg_replace('!^[^:]+:\s*!', '', $tag[2]);
}
elseif ($tagName == 'pubdate')
{
$date = strtotime($tag[2]);
}
elseif ($tagName == 'link')
{
$link = $tag[2];
$id = preg_replace('!^.*/([0-9]+)$!', '\\1', $tag[2]);
}
}
if ($content && $link && $date && $id)
{
$statuses[$id] = array(
'content' => $content,
'link' => $link,
'date' => $date,
);
}
}
unset($items, $tags);
return $statuses;
}
}
?>