Twitter API to RSS
<?php | |
/** | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may | |
* not use this file except in compliance with the License. You may obtain | |
* a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations | |
* under the License. | |
* | |
* All I ask is for you to mention where you got it from. | |
*/ | |
/** | |
* This is taken from my rss reader and I thought I redo it as a standalone | |
* piece of code for others to use. I was thinking about making it a service | |
* but I don't know how to go about getting the ok for more then 150 calls | |
* per hour. | |
*/ | |
/** | |
* Install | |
* Place in a directory called twitter. | |
*/ | |
/** | |
* Usage | |
* 1) /twitter/username - this gives the statuses from the named user. | |
* 2) /twitter/username/favorites - returns the favorites from the named user. | |
* 3) /twitter/username/listname - returns the tweets from a users list. | |
*/ | |
//Configuration | |
//set $only_links = 1 if you want to only include tweets with links. | |
$only_links = 1; | |
//set $title2tweet = 0 if you want use the first link to the title rather then to the tweet. | |
$title2tweet = 0; | |
//set a proxy if needed | |
//$proxy = '10.0.6.251:3128'; | |
$proxy = ''; | |
//modify case of hashtags 1 = lower, 2 = upper | |
$tag_case = 0; | |
//end configuration | |
$uri = strtolower(str_replace('/twitter','',$_SERVER['REQUEST_URI'])); | |
echo '<?xml version="1.0" encoding="UTF-8"?>',PHP_EOL; | |
echo '<rss version="2.0">',PHP_EOL; | |
set_time_limit(120); | |
error_reporting(E_ERROR | E_WARNING | E_PARSE); | |
$parts = explode('/',$uri); | |
if (count($parts) == 3) { | |
switch ($parts[2]=='favorites') { | |
case 'favorites': | |
$url = 'http://api.twitter.com/1/favorites/'.$parts[1].'.json'; | |
$feed_title=$parts[1].'\'s Favorites'; | |
break; | |
default: | |
$url = 'http://api.twitter.com/1/lists/statuses.json?slug='.$parts[2].'&owner_screen_name='.$parts[1].'&include_entities=true&include_rts=true&count=200'; | |
$feed_title=$parts[2].' via '.$parts[1]; | |
break; | |
} | |
} else { | |
$url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.$parts[1].'&include_entities=true&include_rts=true&count=200'; | |
$feed_title=$parts[1].'\'s Tweets'; | |
} | |
//echo $url; | |
$t = get_url($url); | |
$tweets = json_decode($t); | |
echo '<channel>',PHP_EOL; | |
echo ' <title>',$feed_title,'</title>',PHP_EOL; | |
echo ' <link>https://twitter.com/#!',$uri,'</link>',PHP_EOL; | |
echo ' <generator>http://punchingsoup.com/</generator>',PHP_EOL; | |
echo ' <url>'.$url.'</url>',PHP_EOL; | |
foreach ($tweets as $tweet) { | |
$has_link = 0; | |
$retweeted = 0; | |
if (isset($tweet->retweeted_status)) { $retweeted = 1; } | |
if ($retweeted == 0) { | |
$text = clean_string($tweet->text); | |
$text = clean_string($tweet->text); | |
} else { | |
$text = clean_string($tweet->retweeted_status->text); | |
} | |
preg_match('#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',$text,$links); | |
$has_link = sizeof($links); | |
if ($has_link >= $only_links ) { | |
$item_id = $tweet->id_str; | |
echo '<item>',PHP_EOL; | |
// Link | |
$link = 'http://twitter.com/'.$screen_name.'/status/'.$tweet->id_str; | |
if ($title2tweet == 0 && $has_link > 0 ) { | |
echo '<link>',$links[0],'</link>',PHP_EOL; | |
} else { | |
echo '<link>',$link,'</link>',PHP_EOL; | |
} | |
echo '<guid isPermaLink="false">',$link,'</guid>',PHP_EOL; | |
// Author | |
if ($retweeted == 0) { | |
$author_name = $tweet->user->name; | |
$author_link = 'http://twitter.com/'.$tweet->user->screen_name; | |
$screen_name = $tweet->user->screen_name; | |
} else { | |
$author_name = $tweet->retweeted_status->user->name; | |
$author_link = 'http://twitter.com/'.$tweet->retweeted_status->user->screen_name; | |
$screen_name = $tweet->retweeted_status->user->screen_name; | |
} | |
$author_email = ''; | |
// title | |
$title = $text; | |
echo '<title>',$title,'</title>',PHP_EOL; | |
// content | |
$text = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $text); | |
$text = preg_replace('/(@)([0-9a-zA-Z]*)/','<a target="_new" href="http://twitter.com/\2">\1\2</a>',$text); | |
if ($retweeted == 0) { | |
$content = '<div class="tweet_image" style="float:left;padding:10px;"><img src="'.$tweet->user->profile_image_url.'"></div><div class="tweet_content" style="padding:10px;font-size:1.2em;">'; | |
$content .= '<a href="http://twitter.com/'.$tweet->user->screen_name.'" target="_new" style="color:black;text-decoration:none;font-weight:bold;">'.$tweet->user->screen_name.'</a> <span style="color:#bbb;">'.$tweet->user->name.'</span>'; | |
} else { | |
$content = '<div class="tweet_image" style="float:left;padding:10px;"><img src="'.$tweet->retweeted_status->user->profile_image_url.'"></div><div class="tweet_content" style="padding:10px;font-size:1.2em;">'; | |
$content .= '<a href="http://twitter.com/'.$tweet->retweeted_status->user->screen_name.'" target="_new" style="color:black;text-decoration:none;font-weight:bold;">'.$tweet->retweeted_status->user->screen_name.'</a> <span style="color:#bbb;">' . $tweet->retweeted_status->user->name . ' <img src="theme/default/icons/retweet.png"> via ' . $tweet->user->screen_name . '</span>'; | |
} | |
$content .= '<br />'.$text; | |
$content .= '<br /><a href="https://twitter.com/#!/'.$screen_name.'/status/'.$item_id.'" target="_blank">Open on Twitter</a>'; | |
$content .= '</div>'; | |
//favorites | |
//https://api.twitter.com/1/favorites/create/72820884053819394.json | |
//retweet | |
//https://api.twitter.com/1/statuses/retweet/72820884053819394.json | |
//status use %20 rather then + | |
//https://twitter.com/?status= | |
echo '<description><![CDATA[',$content,']]></description>',PHP_EOL; | |
$published = date("D, d M Y H:i:s T",strtotime($tweet->created_at)); | |
echo '<pubDate>',$published,'</pubDate>',PHP_EOL; | |
// categories | |
preg_match_all("/(#\w+)/",$text,$tags); | |
foreach ($tags[0] as $tag) { | |
if (strlen($tag) <= 30) { | |
if ($tag_case == 1) { $tag = strtolower($tag); } | |
if ($tag_case == 2) { $tag = strtoupper($tag); } | |
$tag = preg_replace('/[^A-Za-z0-9$tag_charkeep]/i', '', $tag); | |
$tag = str_replace("'","''",$tag); | |
echo '<category><![CDATA[',$tag,']]></category>',PHP_EOL; | |
} | |
} | |
echo '</item>',PHP_EOL; | |
} | |
} | |
echo '</channel>',PHP_EOL; | |
echo '</rss>',PHP_EOL; | |
function clean_string($c) { | |
$c = utf8_decode($c); | |
$c = str_replace(chr(160),"",$c); | |
return $c; | |
} | |
function get_url($u) { | |
global $proxy; | |
$ch = curl_init(); | |
curl_setopt ($ch, CURLOPT_URL, $u); | |
curl_setopt ($ch, CURLOPT_HEADER, 0); | |
if ($proxy != '') { curl_setopt($ch, CURLOPT_PROXY, $proxy); } | |
curl_setopt ($ch, CURLOPT_USERAGENT, 'punchingsoup'); | |
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); | |
$result = curl_exec ($ch); | |
curl_close ($ch); | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment