Twitter Feed
This is my first attempt at an action... hopefully things will work out ok.Note: This is mostly re-tooled from the default rss action. I added in some additional features as specified below
Features
Current Features
- Takes twitter rss feed and...
- Strips the "Username: " portion from the feed
- Only posts the description (the Title and description are identical, one is just a link back to the individual twitter post)
- Optionally displays @ replies (providing one-sided conversations)
- Optionally limits results to specified number (helpful for fitting the feed into a portion of your layout)
- Automatically converts twittered links to clickable links.
- Automatically converts @username to a clickable link to www.twitter.com/username
- Automatically converts #hashes to a clickable link to search.twitter.com
Source Code
Current Version: 0.8
- <?php
- /**
- * Cache and display an Twitter RSS feed.
- *
- * Usage:
- * {{twitter http://twitter.com/statuses/user_timeline/[uid]}} or
- * {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30"}} or
- * {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies=true}} or
- * {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies="1" maxitems="5"}}
- *
- * NOTE 1: in Onyx-RSS default is "debugMode" which results in all errors being printed
- * this could be suppressed by turning debug mode off, but then we'd never have a
- * clue about the cause of any error.
- * A better (preliminary) approach seems to be to override the raiseError() method
- * still providing the text of any error message, only within an HTML comment:
- * that way normal display will look clean but you can look at the HTML source to
- * find the cause of any problem.
- * NOTE 2: no solution for timeout problems with non-existing feeds yet...
- * NOTE 3: A major portion of this was taken from the rss.php action.
- *
- * Version 0.8
- *
- * @input string $url mandatory: URL of the feed
- * @input integer $cachetime optional: time in minutes to cache the feed
- * @input boolean $replies optional: True for displaying @username replies. Defaults to false.
- * @input integer $max_items optional: Maximum items to display
- * @output ... tbd @@@
- * @uses Wakka::cleanUrl()
- * @uses Wakka::ReturnSafeHTML()
- * @todo Add comments for readability
- *
- * Changelog
- * 0.8 Added clickable #hashes
- * 0.7 Fixed underscore nick bug not being clickable
- * 0.6 Added clickable @Nicks
- * 0.5 Fixed Nick Bug
- * 0.4 Added comments
- * 0.3 Incorporated replies and maxitems parameters
- * 0.2 Added coding for parsing out username/identifying and click-ifying links
- * 0.1 Copied over from rss action
- */
- $caching = true; // change this to false to disable caching
- $twitter_cache_path = "/tmp"; // set this to a writable directory to store the cache files in
- $lowest_cache_time_allowed = "5"; // set this to the lowest caching time allowed
- // Begin dealing with action variables
- // Maximum items to display
- if (!$twitter_max_items) {
- $max_items = 5; // this is the default number of items to display
- } else {
- $max_items = $twitter_max_items; // set max items to passed variable
- }
- // Are we displaying replies
- if (!$twitter_replies) {
- $replies = 0; // defaults to not showing replies
- } else {
- $replies = $twitter_replies; // set replies to passed variable
- }
- // What cache time are we using
- if (!$twitter_cache_time) {
- $twitter_cache_time = 30; // set this for default cache time
- } elseif ($twitter_cache_time < $lowest_cache_time_allowed) {
- $twitter_cache_time = $lowest_cache_time_allowed;
- }
- $twitter_cache_file = ""; // initial value, no need to ever change
- // End dealing with action variables
- // Action configuration
- $twitter_path = $this->htmlspecialchars_ent($vars['url']);
- if ((!$twitter_path) && $wikka_vars) $twitter_path = $wikka_vars;
- // override - Added by pezhore: not sure what this does...
- {
- $onyx_classpath = $this->GetConfigValue('onyx_path').DIRECTORY_SEPARATOR.'onyx-rss.php';
- /**
- * 3rdParty plugin; implements feed aggregation.
- */
- #include_once('3rdparty'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'onyx-rss'.DIRECTORY_SEPARATOR.'onyx-rss.php');
- include_once $onyx_classpath;
- {
- /**
- * Extension to plugin; implements error handling.
- *
- * @package Actions
- */
- class Wikka_Onyx extends ONYX_RSS
- {
- //private function raiseError($line, $err)
- function raiseError($line, $err)
- {
- if ($this->debugMode)
- {
- echo '<!-- '.$errortext.' -->'."\n";
- }
- }
- }
- }
- }
- {
- if ($caching)
- {
- // Create unique cache file name based on URL
- }
- //Load the RSS Feed: workaround to hide error messages within HTML comments:
- #$twitter =& new Wikka_Onyx();
- $twitter = instantiate('Wikka_Onyx');
- $twitter->setCachePath($twitter_cache_path);
- $twitter->parse($twitter_path, $twitter_cache_file, $twitter_cache_time);
- $meta = $twitter->getData(ONYX_META);
- $nick = ": ";
- //List the feed's items
- $cached_output = "<ul>\n";
- while ($max_items > 0 && ($item = $twitter->getNextItem()))
- {
- $str_desc = $item['description']; // Only using the description for our feed
- $pos = $pos + 2; // Add two for the ':' and ' ' characters
- // Check to see if replies are to be included
- {
- // Replace http:// with valid links
- // Replace @username with valid link
- $str_desc = preg_replace("/@([a-z,A-Z,0-9,_]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $str_desc);
- // Replace #topic with valid link to search.twitter.com
- $str_desc = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=$1\">#$1</a>", $str_desc);
- $cached_output .= "<li>".$str_desc."</li>\n";
- $max_items = $max_items - 1;
- } elseif ($replies == 1) // If replies are wanted, show them
- {
- // Replace http:// with valid links
- // Replace @username with valid link
- $str_desc = preg_replace("/@([a-z,A-Z,0-9,_]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $str_desc);
- // Replace #topic with valid link to search.twitter.com
- $str_desc = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=$1\">#$1</a>", $str_desc);
- $cached_output .= "<li>".$str_desc."</li>\n";
- $max_items = $max_items - 1;
- }
- }
- $cached_output .= "</ul>\n";
- echo $this->ReturnSafeHTML($cached_output);
- }
- else // If this was called improperly, display usage
- {
- echo '<em class="error">Error: Invalid Twitter action syntax. <br /> Proper usage: {{twitter http://twitter.com/statuses/user_timeline/[uid]}} or <br>
- {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30"}} or <br>
- {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies=true}} or <br>
- {{twitter url="http://twitter.com/statuses/user_timeline/[uid]" cachetime="30" replies="1" maxitems="5"}}}}</em>'; # i18n
- }
- ?>
Installation
- Save the code above as twitter.php in your actions folder (for trunk, you have to first create an actions/twitter folder, then save as actions/twitter/twitter.php)
- Use as {{twitter}} according to the usage stated above
Known Bugs
- The coding for dropping "Username: " isn't quite up to par. Change line #133 to $str_desc = substr($str_desc, #); where # is the number of characters from of your twitter name + 2 (for ':' and ' '). Working on the fix.
- Fixed in Version 0.5 and above
Thoughts? Questions? Let me know. I'm on IRC as Pezhore.
CategoryUserContributions