[localhost:~]$ cat * > /dev/ragfield

Thursday, April 16, 2009

Twittering with Mathematica

A couple weeks ago James Rohal wrote about "Updating Twitter from Mathematica." His technique is interesting, but it has a number of drawbacks. It requires special PHP code to run on his server. The user name and password appear to be hardcoded. The only functionality it provides is updating the status.
Twitter has a REST API which can be called directly from a number of languages, including Mathematica. Several months ago I started, but didn't finish, a Mathematica package to wrap the Twitter API. After I read James' post I took the opportunity to clean it up and fill in the missing pieces.
Let's take a look a few of the things this Twitter package can do. Begin by loading the package in the usual manner.
<<Twitter`
TwitterSession
Next we'll open a session. This will bring up a login dialog.
session = TwitterSessionOpen[]
TwitterSession[<ragfield>]
TwitterSessionAuthorizedQ[session]
True
TwitterSessionScreenName[session]
ragfield
TwitterSessionClose[session]
It's also possible to specify the user name and/or login programatically to avoid the manual step. However, doing so will require a hardcoded password.
session = TwitterSessionOpen["User"->"ragfield"]
TwitterSession[<ragfield>]
TwitterUser
The next object is a user. The session is associated with the user who logged in.
user = TwitterSessionUser[session]
TwitterUser[<ragfield>]
We can query the user for several attributes.
TwitterUserID[user]
12920162
TwitterUserName[user]
Rob Raguet-Schofield
TwitterUserScreenName[user]
ragfield
TwitterUserLocation[user]
Urbana, Illinois
TwitterUserHomePage[user]
http://rob.ragfield.com
TwitterUserDescription[user]
My name is Rob. I write software, ride bicycles, and occasionally write software while riding bicycles.
TwitterUserProfilePage[user]
http://twitter.com/ragfield
TwitterUserInfo[user]
Name Rob Raguet-Schofield
Location Urbana, Illinois
Web http://rob.ragfield.com
Bio My name is Rob. I write software, ride bicycles, and occasionally write software while riding bicycles.
And all this information can be displayed graphically with hyperlinks, tooltips, etc.
TwitterUserUI[user]
2009-04-16-TwitteringWithMathematica1
TwitterStatus
The final, and perhaps most important, object is the status. A status can be accessed directly by its unique ID, or it can be retrieved from a timeline (see below).
status = TwitterStatus[671394002]
TwitterStatus[<ragfield: shopping for underpants>]
We can also query the status message for several attributes.
TwitterStatusID[status]
671394002
TwitterStatusUser[status]
TwitterUser[<ragfield>]
TwitterStatusDate[status]
Sat 2 Feb 2008 19:02:02
TwitterStatusText[status]
shopping for underpants
TwitterStatusPage[status]
http://twitter.com/ragfield/statuses/671394002
TwitterStatusSource[status]
TwitterStatusUI[status]
2009-04-16-TwitteringWithMathematica2
Setting the status
We can set our status.
status = TwitterSetStatus[session, "Tweeting from Mathematica"]
TwitterStatus[<ragfield: Tweeting from Mathematica>]
TwitterStatusUI[status]
2009-04-16-TwitteringWithMathematica9
We can also delete any of our tweets if we decided we don't like them.
status = TwitterSetStatus[session, "I am dumb"]
TwitterStatus[<ragfield: I am dumb>]
id = TwitterStatusID[status]
1537789527
TwitterDeleteStatus[session, status]
TwitterStatus[<ragfield: I am dumb>]
TwitterStatus[id]
FetchURL::"conopen": "\!\(\*StyleBox[\"\\\"The connection to URL \\\"\", \"MT\"]\)\!\(\*StyleBox[\!\(\"http://twitter.com/statuses/show/1537789527.xml\"\), \"MT\"]\)\!\(\*StyleBox[\"\\\" cannot be opened. If the URL is correct, you might need to configure your firewall program, or you might need to set a proxy in the Internet connectivity tab of the Preferences dialog (or by calling SetInternetProxy). For HTTPS connections, you might need to inspect the authenticity of the server's SSL certificate and choose to accept it.\\\"\", \"MT\"]\)"
$Failed
The tweet has been deleted, so any attempt to download it again fails.
Timelines
A list of status messages can be retrieved from a specific user.
Take[TwitterUserTimeline[user], 4]
{TwitterStatus[<ragfield: Tweeting from Mathematica>], TwitterStatus[<ragfield: testing...>], TwitterStatus[<ragfield: 4 mile recovery/photo walk>], TwitterStatus[<ragfield: Marge, if anyone asks, you require 24 hour nursing care, Lisa's a clergyman, Maggie is seven people, and Bart was wounded in Vietnam>]}
Column[TwitterStatusUI/@%]
2009-04-16-TwitteringWithMathematica3
And more...
Take[TwitterUserTimeline[user, "Page"->2], 4]
{TwitterStatus[<ragfield: off to run the marathon. wish me luck.>], TwitterStatus[<ragfield: @esmithrunner good luck to you and your family as well>], TwitterStatus[<ragfield: It's just another race>], TwitterStatus[<ragfield: Little Miss C (kindergarten) after successfully adding 95+3, "I probably know just about all maths.">]}
Column[TwitterStatusUI/@%]
2009-04-16-TwitteringWithMathematica4
And even other users' timelines...
Take[TwitterUserTimeline["lancearmstrong"], 3]
{TwitterStatus[<lancearmstrong: The Armstrong's in the snow! Just went swimming at the aspen rec center.>], TwitterStatus[<lancearmstrong: http://twitpic.com/3ffo8>], TwitterStatus[<lancearmstrong: This is AMAZING!!!! http://tinyurl.com/d7d6ex>]}
Column[TwitterStatusUI/@%]
2009-04-16-TwitteringWithMathematica5
And the friends timeline...
Take[TwitterFriendsTimeline[session], 3]
{TwitterStatus[<cabel: Enjoying the absurdity of Remi Gaillard's real-life Pac-Man. http://tinyurl.com/dm9mor>], TwitterStatus[<danielpunkass: I suggest opt-in per tweet. I get a message from @twitshirt: "Lucky you! Somebody wants to make you famous!" Per-tweet licensing agreement.>], TwitterStatus[<danielpunkass: Feel bad for @twitshirt guys, but I agree it's a smarmy premise ©-wise. Friend in IRC suggested opt-in + @messages inviting opt-in. Solved.>]}
Column[TwitterStatusUI/@%]
2009-04-16-TwitteringWithMathematica6
And any replies to the user...
Take[TwitterReplies[session], 3]
{TwitterStatus[<spoonshake: @ragfield Ah, priceless Simpsons quotes.>], TwitterStatus[<gutzville: @ragfield I would just be happy if they put the curtis road exit in that has been open for more than a year>], TwitterStatus[<spoonshake: @ragfield Priceless headline.>]}
Column[TwitterStatusUI/@%]
2009-04-16-TwitteringWithMathematica7
And the public timeline...
Take[TwitterPublicTimeline[], 3]
{TwitterStatus[<lastp1acechamps: @mchowes yah my friend seriously lives right across the street from the newbury store and he couldn't get up for me.>], TwitterStatus[<exult: Some things alarm me more than other things>], TwitterStatus[<HambyDavid: Phone is dying. Need car charger>]}
Column[TwitterStatusUI/@ %]
2009-04-16-TwitteringWithMathematica8
Friends and Followers
Finally we can get a list of our friends and followers.
Take[friends = TwitterFriends[session], 5]
{TwitterUser[<bmeyaard>], TwitterUser[<quickKarl2>], TwitterUser[<_Becca_Knight>], TwitterUser[<bikefriday>], TwitterUser[<aimeekandrac>]}
Take[followers = TwitterFollowers[session], 5]
{TwitterUser[<bmeyaard>], TwitterUser[<quickKarl2>], TwitterUser[<NaturalComedy>], TwitterUser[<akw279>], TwitterUser[<Modells_Coupons>]}
And since this is Mathematica, we can do interesting data explorations.
friendIDs = TwitterUserID/@friends;
followerIDs = TwitterUserID/@followers;
union = Intersection[friendIDs, followerIDs];
55% of the people I'm following also follow me.
N[Length[union] / Length[friendIDs]]
0.55`
42% of the people who follow me are people whom I also follow.
N[Length[union] / Length[followerIDs]]
0.41509433962264153`
TwitterSessionClose[session]
Downloads
Download HTTP.m (required by Twitter.m).

Update: I have written a very similar entry for my company's blog.

1 comment:

James Rohal said...

This is great! I was wondering how to use the POST method in Mathematica. Thanks for creating such a useful package.