public
Last active

Twitter API to RSS

  • Download Gist
.htaccess
ApacheConf
1 2 3
RewriteEngine on
RewriteBase /twitter
RewriteRule ^(.*)$ twitter2rss.php [NC]
twitter2rss.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
<?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;
}
 
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.