Integrating Mathematica with Twitter

A World of Possibilities

George E. Danner
Business Laboratory

Acknowledgements

Alan Savoy wrote most of the underlying code you see here

Tripp Lybrand assisted with the testing

Todd Gayley provided invaluable advice and guidance

Some Background

One of Mathematica’s biggest strengths is its ability to integrate with a surprisingly wide range of applications.

When you integrate Mathematica with something else, it often turns the collection into an entirely new tool.

Two years ago we spoke on the subject of integrating Mathematica with Google technologies.

Social Networks

We all know about social networks.   Most of us use them for fun.

But what would happen if we started to put intelligence onto social networks?

Could we turn social networks into machines that did useful work?

The first steps is to make Mathematica a “peer” on a social network.  This is what we set out to do.

That means that a model could not only READ messages in its group of followers but also RESPOND in meaningful ways.

Our Inspiration

We were inspired by a Wolfram Blog entry some time ago.

The code did not work (because of changes in the Twitter API)

It was more - or - less read only code

The Solution

Architecture

The Solution: AtModel

A synthetic Twitter account "follows" a number of people.

The Solution: AtModel

Someone from the following community tweets a special command.

The Solution: AtModel

The AtModel responds by tweeting the results of the command - based computation.

The Solution: Key Functions

Getting ready to use the model.

PrepareTwitter[]:=Block[{},

InstallJava[];

(*The twitter4j library is in the notebook directory*)
AddToClassPath[NotebookDirectory[]];

(*Creates a global reference to the twitter system*)
LoadJavaClass["twitter4j.TwitterFactory"];
twitter=twitter4j`TwitterFactory`getSingleton[];

];

The Solution: Key Functions

Collect the tweets on ATModel' s timeline, produce the wordcloud, then tweet a URL to the result.

TweetCloud[text_String]:=Block[{wordCloud,imagefile,statusUpdate},

(*Export the word cloud image to the file system*)
wordCloud=StringToWordCloud[text];
Export[FileNameJoin[{NotebookDirectory[],"wordcloud.jpg"}],wordCloud];

(*Create a Java reference to the exported file*)imagefile=JavaNew["java.io.File",FileNameJoin[{NotebookDirectory[],"wordcloud.jpg"}]];

(*Create a new Twitter status update using the library and attach the file*)statusUpdate=JavaNew["twitter4j.StatusUpdate","Timeline Wordcloud #atmodel"];
statusUpdate@setMedia[imagefile];

(*Send the tweet to Twitter*)
twitter@updateStatus[statusUpdate]
];

The Challenges

The old code from the blog entry did not work (due to changes in the Twitter API -  requiring open authentication as example)

We originally tried to write the code for use in a CDF, meaning no outside library.Import alone no longer worked, and OAuth was hard to debug in native MMa (we didn' t know Wolfram had provided an undocumented function already).We dropped the CDF requirement and looked for a library.None were found for Mathematica, supporting the new API, one mostly working Java library was found : twitter4j

There is a usage limit on the normal Twitter API, cutting into the ability to poll

For the streaming interface, the Twitter certificate is intermittently incompatible with the Java runtime in MMa.This is why the trigger wasn' t streamed.

A Live Demonstration

Let’s try this out right now.

Conclusions

We were once again amazed at what kinds of systems you could stiitch to Mathematica

Wolfram could have made this a little easier by ...

Intelligent devices on social networks have myriad applications :

Crowd selling

Remote telemetry