ActiveReporting with Twitter

We all know what is trending these days thanks to our favorite micro blogging service: Twitter. I am sure we all know what Twitter is and how it has changed the evolution of social networking. Though this blog will be centered around Twitter but my focus will be to show you how your favorite reporting tool  i.e. ActiveReports can work with Twitter .
My objective is rather simple, I want to create a simple application through which I can keep track of my tweets or may be someone else’s and have the tweets displayed on a platform where I can also have this information exported as a PDF, Excel, Image ,Word or Text file. This may seem like too much of a task ,but believe me when I say that it is more simple than you would have ever imagined.

Now, let’s go back to the technical tasks at hand and see what is required for us to get the tweets from Twitter and then have it displayed using ActiveReports.

Task 1: Get Tweets from Twitter in a format that Active Reports can use or understands.

Task 2: Bind the Tweets to the Report to have it displayed.

Now that we know the tasks at hand, let’s iterate through each of them one at a time .

Task 1: Get Tweets from Twitter in a format that Active Reports understands

Twitter happens to be an awesome micro blogging service, but most of us are not aware of the awesomeness it has in store for developers who want to make applications using Twitter. Twitter exposes quite a few different API’s to developers so that they can leverage these API’s and make applications that accomplish tasks like posting tweets, reading a tweet etc from their application.

One such that API that will make use of is the REST API. This API enables developers to access basic facets of twitter like timelines, status updates and user information. If you are new at using Restful services in your application then you should probably start from here to know what REST is.

Twitter exposes an endpoint i.e.  GET statuses/user_timeline  via  REST which will returns 20 most Recent tweets of a specified user . This end point returns data in 4 formats i.e JSON ,XML ,RSS and Atom . We have the liberty to choose any format that we like ,but since ActiveReports has inbuilt classes to handle XML datasources ,I have gone for XML since it will get the job done without much effort .
For example if you type the following URL in your browser and hit enter, it should return you the Tweet timeline of Barrack Obama in XML format. BARACKOBAMA

So using this URL we could get any one’s public tweet if we just entered the correct user name 

Now, we know that Twitter has an end point which will give us the tweets in the format that we need , but how exactly do we get data from an  URI ?

.NET is a Pandora’s box of classes and it has one such class called the WebClient Class  which does the task of sending and receiving data from a resource identified by an Uri. So we could use this class and its methods of get data from a specified URI like the one that is given above.

So the code block should look something like this:

 void GetData(string User)  
WebClient TwitterGetData = new WebClient();
TwitterGetData.DownloadStringCompleted += new DownloadStringCompletedEventHandler(TwitterGetData_DownloadStringCompleted);
TwitterGetData.DownloadStringAsync(new Uri("" + User));
void TwitterGetData_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

There are a few things to note in the above code :

  1.  We have used the DownloadStringAsync method to download the user tweets as a string but still the data is XML .
  2. We are waiting for the Download to Complete using the TwitterGetData_DownloadStringCompleted handler and after the download is complete we are passing the resultant data to be set as a datasource for the report.

Task 2: Bind the Tweets to the Report to have it displayed

Now ,that we have the tweets in XML format ,ActiveReports will do the  job of binding this data to report with ease. The Xml datasource only needs the XML document and and XPath pattern to select the data source nodes from the XML data returned by the Twitter Rest API.

 void SetDataSource(string XMLTweetTimeline)  
DataDynamics.ActiveReports.DataSources.XMLDataSource ds = new DataDynamics.ActiveReports.DataSources.XMLDataSource();
ds.ValidateOnParse = true;
ds.FileURL = null;
ds.RecordsetPattern = "//statuses//status";
Report.DataSource = ds;

As you can observe we have used the LoadXML method of the XMLDataSource to load the XML data and used the RecordsetPattern property to set the XPATH expression.Please note that you must bind your report fields to the correct XML fields or your tweets may not see the light of day .You can get information regarding using XML as a datasource for a report from here.

I am going to leave the part of designing the report and running the report to our customers. If you need a sample application to get you through then TwitterReporter application is available for download.

There are a couple of things that I want to discuss before I bring this topic to a close:

  1. The data returned by GET statuses/user_timeline  end point has quite a lot of information including the user_name,user_profilepicture_url ,followers etc.  So for example if you needed to display the Profile picture in the report as well, then you will need to parse the url to get the image and set it on the report.The code that illustrates this is provided below
  2.  XDocument doc = XDocument.Parse(e.Result.ToString());  
    ProfileUrl = doc.Descendants("user").Elements("profile_image_url").First().Value;
    DataDynamics.ActiveReports.Picture profile_Picture = (DataDynamics.ActiveReports.Picture)Report.Sections["Detail1"].Controls["picture2"];
    profile_Picture.Image = LoadImage(ProfileUrl);

  3. Now, since you have already displayed your tweest on a report ,you can now export them to any of the file formats supported by ActiveReports and get your tweets in a desired format like PDF,Excel,Word etc.

3 thoughts on “ActiveReporting with Twitter

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s