Talking Puffin is a full blown desktop Twitter client, but it also includes an independently consumable Twitter API. This provides a Scala implementation of most Twitter API functions, buildable with Maven and includable in other projects.
The first step in using the Twitter API is getting source. You can either download this from http://github.com/dcbriccetti/talking-puffin/ or use git to clone your own repository.
The next step is building the code. Go to the base directory of the project (the one with README.md), and execute mvn install. This will run the scala compiler on both the desktop and Twitter API projects and install them into your Maven repository. Note that you can also do this from the twitter-api subdirectory if you only want to build the Twitter API.
After code has successfully built you can interact with the Twitter API via the scala console. To do this, go to the twitter-api directory and run mvn scala:console. You should see something like the following
mcmac:twitter-api mmcbride$ mvn scala:console
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'scala'.
[INFO] ------------------------------------------------------------------------
[INFO] Building TalkingPuffin Twitter API
[INFO] task-segment: [scala:console]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing scala:console
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [scala:compile {execution: default}]
[INFO] Checking for multiple versions of scala
[INFO] Nothing to compile - all classes are up to date
[INFO] [scala:console]
[INFO] Checking for multiple versions of scala
Welcome to Scala version 2.7.3.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_07).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
In the console you can now import the Twitter API package and create a session.
cala> import org.talkingpuffin.twitter._ import org.talkingpuffin.twitter._ import org.talkingpuffin.twitter._ scala> val sess = TwitterSession(twitterUser,twitterPassword) val sess = TwitterSession(twitterUser,twitterPassword) sess: org.talkingpuffin.twitter.AuthenticatedSession = org.talkingpuffin.twitter.AuthenticatedSession@9fbc913
Once you have a session, you can call twitter methods… this example shows us getting messages from the public timeline
scala> val timeline = sess.getFriendsTimeline("mccv") val timeline = sess.getFriendsTimeline("mccv") timeline: List[org.talkingpuffin.twitter.TwitterStatus] = List(org.talkingpuffin.twitter.TwitterStatus@73ec8c2, org.talkingpuffin.twitter.TwitterStatus@2aee3c45, org.talkingpuffin.twitter.TwitterStatus@7eb6ec07, org.talkingpuffin.twitter.TwitterStatus@1b42008f, org.talkingpuffin.twitter.TwitterStatus@a32ba44, org.talkingpuffin.twitter.TwitterStatus@862cb97, org.talkingpuffin.twitter.T...
The object returned is a List of TwitterStatus objects. The TwitterStatus object is a container for a single tweet, and has fields for most of the elements of the XML returned from the Twitter API. The following example shows a way to iterate over this list and print out the user and text. Note that the user field of the TwitterStatus object is a TwitterUser object, which contains info about a user.
scala> timeline foreach {(x) => println(x.user.name + "--\t" + x.text)} timeline foreach {(x) => println(x.user.name + "--\t" + x.text)} Dave Winer-- Switched at Birth, Women Find New Identity. http://tr.im/lixe Dave Winer-- Yes, there is something super-ironic about a long list of URL shorteners. :-) http://tr.im/li3i Robert Dempsey-- Yes, I'm on a bus in Austin. http://yfrog.com/emz3oj Dave Winer-- The NY Times/Twitter feed (Scripting News). http://tr.im/lixY Dave Winer-- No LOST spoilers!! Twitter API-- Starting something serious on the API? Want to launch your project May 26? Launch at @140tc: http://bit.ly/8jj8x ^DW Dave Briccetti-- Trying to get more RAM on my shit dedicated server, davebsoft.com. Shit because OLM wanted a fortune per month to add dirt-cheap RAM. Michael Arrington-- FriendFeed Enables People/Group Tracking http://tcrn.ch/1t4 by @parislemon rick-- OMG LOST finale in a couple hours Steve Spalding-- A hard lesson to learn is that you can't hand people success. The best you can do is give them the tools. Michael Arrington-- Google's Last MySpace Payment: $75 Million On June 20, 2010 http://tcrn.ch/1t9 by @arrington Alex Payne-- On my way to have a beer with two of the @dropbox guys. Once tipsy, I may request more jiggabytes. Robert Dempsey-- OH: "They all run in the same circles. They fight, they hang out, they have transvestite beauty pageants." Albert Wenger-- On the ground at SFO - meeting up with @joshu for dinner. NewsGang-- Calling all iPhones! Emergency scanner apps on the loose! (from Steven Sande) : Filed under: Software, Odds and .. http://tinyurl.com/qpom96 NewsGang-- Quite Possibly the Coolest Hot Tub You Have Ever Seen | dornob (from dornob.com) http://tinyurl.com/p2r54h Dave Winer-- Red tree: http://tr.im/liMR Dave Winer-- Guerilla Cafe, Berkeley: http://tr.im/liMS Dave Winer-- Tree with red leaves in sunset: http://tr.im/liMT Dave Troy-- There will be a 2010 re-enactment of the 1871 Baltimore parade celebrating the ratification of the 15th amendment (my college thesis). scala>
Note that we can get a bit fancier with this and use for comprehensions to do more or less the same thing. The following sample uses the API to filter the friends timeline, looking for any text with the word “Berkeley”
scala> for(x <- sess.getFriendsTimeline("mccv") if x.text.indexOf("Berkeley") > 0) println(x.text) for(x <- sess.getFriendsTimeline("mccv") if x.text.indexOf("Berkeley") > 0) println(x.text) Guerilla Cafe, Berkeley: http://tr.im/liMS scala>
This should give you a pretty decent start on using the API. There are many more functions available, and you can take a look by generating the scala doc and checking out the API methods. To generate the site, go to your twitter-api directory and run “mvn site”. This should generate a set of HTML pages in target/site. Open index.html, click project reports, click scala docs, and look at the generated documentation for AuthenticatedSession.