BackTweets API AS3 Class

For a current project collaborator Bruce Drummond and I needed to search Twitter for urls. We attempted to parse the bit.ly truncated urls by expanding them, but that proved inefficient. Since each user truncates urls differently, we could not simply truncate the url and then search for that bit.ly. We also contacted bit.ly to determine if a list of short urls could be returned for a long url, but after a condescending response, we decided to look elsewhere.

After some searching on the web, we found BackTweets. This is an extension of BackType that stores data for various social media. BackTweets proved most effective at handling url searches. We could pass in the url that we were searching for and BackTweets would return a list of tweets. It is also possible to pass in something shorter, like nytimes.com and it will return tweets that have that inside the url. For example, http://www.nytimes.com/2009/11/13/world/asia/13eikenberry.html?_r=1&hp, could be returned when providing nytimes.com as a query term.

Earlier in this project we wrote a class in AS3 for Flash, and thought that someone may benefit from it.

Download Source

It is a simple wrapper that you can pass in arguments and get an array of tweet objects returned.
The constructor takes one argument, your BackTweetAPI Key

var _bt:BackTweet = new BackTweet("Your-BackTweet-API_Key");

Now that we have a BackTweet ready, we can use the search function to search for something. Don’t forget to add a listener so that we know when it is finished loading;

_bt.addEventListener(Event.COMPLETE, _onBtLoaded, false, 0, true);
_bt.search( "Your-Search-Term" );

The search function is defined as

search($q:String, $itemsPerPage:uint = 20, $page:uint = 0, $since_id:uint = 0);

The $itemsPerPage, $page, and $since_id optional arguments do the following
$itemsPerPage defines the number of tweets returned (from 10 – 100);
The $page argument determines the offset of the tweets returned.
The $since_id can be specified to search tweets after a certain id.

For example:

var _bt:BackTweet;
_bt = new BackTweet("Your-BackTweet-API_Key");
_bt.addEventListener(Event.COMPLETE, _onBtLoaded, false, 0, true);
_bt.search( "Your-Search-Term" );

Now we must define the function that is called when the tweets are loaded

private function _onBtLoaded($e:Event):void {
	trace(bt.toString()+'\n');
	var tweets:Array = bt.tweets;
	bt.traceTweets();
}

The BackTweet class overrides the toString function to print the BackTweet information.
The traceTweets function inside the BackTweet class is a convenience function that traces out all of the tweets to the output panel.
And calling bt.tweets returns an array of Tweet objects that contains the following

public var tweetId:Number;
public var userId:Number;
public var user:String;
public var profileImage:String;
public var createdTime:String;
public var tweetText:String;

All of these vars are public, so once the tweets are loaded, you can access the vars in the tweets array,
for example, to access the user who created each tweet:

for (var i:int = 0; i < tweets.length; i++) {
	trace(tweets[i].user);
}

So here is all the code so far:

var _bt:BackTweet = new BackTweet("Your-BackTweet-API_Key");
_bt.addEventListener(Event.COMPLETE, _onBtLoaded, false, 0, true);
_bt.search( "Your-Search-Term" );
 
private function _onBtLoaded($e:Event):void {
	trace(_bt.toString()+'\n');
	var tweets:Array = _bt.tweets;
	_bt.traceTweets();
 
	for (var i:int = 0; i < tweets.length; i++) {
		trace(tweets[i].user);
	}
}

However, loading xml files from other domains can cause problems. So, we have to use a php file that loads the xml for us. Check this blog post for more information on loading external xml files. The example contains this php file.

_bt.setXMLLoadFile( "loadXML.php" );

Download Source

This entry was posted in AS3, Flash and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted August 7, 2010 at 9:52 pm | Permalink

    thanks for sharing

5 Trackbacks

  1. [...] BackTweets API AS3 Class nickhardeman.com/blog/?p=276 [...]

  2. [...] First Tweet: 8 hours ago somerandomdude Highly Influential PJ Onori BackTweets API AS3 Class http://nickhardeman.com/blog/?p=276 retweet [...]

  3. By BackTweets API AS3 Class — Some Random Dude on November 27, 2009 at 12:40 pm

    [...] BackTweets API AS3 Class [...]

  4. [...] a service of BackType. I wrote a AS3 class to wrap the BackTweets API, more information in this blog post. The tweets are arranged around in the center based on the time difference from the article posting [...]

  5. By Alex Gordon on April 8, 2010 at 12:13 pm

    Не только тебя…

    Since each user truncates urls differently, we could not simply truncate the url and then search for that bit.ly. We also contacted bit.ly to determine […….

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*