Types of Twitter Applications
Normally, when I'm about to start writing a Twitter application, I already know what I want it to do. Thus, based on the features and functions I have in mind, I already know what platform and category of users I'm targeting. Because we cannot know what you, the reader, have in mind, we will try to set up a basic framework for thinking about the various things you can do with Twitter as we go through this book. Part of Twitter's success is its simplicity and wide-open API. As such, people have developed powerful, sophisticated applications, mashups, and simple widgets that run in other apps or on web pages. However, the approach you will take building a full-on application is different from building a simple mashup or widget.
Building a feature-rich Twitter application takes some planning. Although we will walk you through various examples of how to build apps around specific APIs, we want to bring focus, too. There is an overall approach you should determine before you write line one.
Let's talk about architecture around a simple widget. Suppose our simple widget is going to display the results of a search or the latest tweets from a user. This is the easiest to build. All we have to think about is four steps: make an API call to Twitter, parse the return, format it, and display it. That's it. We diagrammed this simple architecture in Figure 3.2.
Figure 3.2 Example of a simple Twitter API diagram.
All API systems work this way, but what's great about Twitter is that the results are already of value. Quite often, blogging sites (mostly personal) have this type of widget. I have a widget like this on my blog (see Figure 3.3).
Figure 3.3 Screenshot of a Twitter widget on www.perivision.net/word-press.
Because a mashup can be the combination of anything, and that's kind of the point of mashups, we are going to think about our architecture a bit differently. Although technically, a mashup can be just two sources of information or very complex number and relationship of sources, we are going to stick with the spirit of what is considered a mashup by just thinking about mixing two data sources. For example, we can take our Twitter search feed and weather data and display tweets from places that are raining versus tweets from where it's sunny. In this case, we need to store our returns from Twitter somewhere while we get weather data. Then we need to perform some business logic on those returns.
In this case, we need to hold our returns in an array so that when we get the weather data, we can reorganize our data. Because tweets are small, discrete messages, it makes sense to create a multidimensional array object that we can easily explore. So now, we will add one more layer to our diagram. As you can see in Figure 3.4, we are using arrays to store our parsed return so that we can apply some rules (business logic) to create a more valuable dataset.
Figure 3.4 Example of combining two data streams.
I would expect that only a small percentage of readers of this book are intending to build a full-featured Twitter client, but if you are, you want to approach building your application like any other application. Think about your calls to Twitter almost like calls to a database where you provide a set of parameters with your call and get a filtered response that can then be analyzed or applied to a set of rules. It is also well worth your time to set up your Twitter calls in a separate class to deal with errors and changes to the API. You should also set up another class to deal with converting your Twitter calls into multidimensional arrays and/or storing them in a database. The reason for this is that Twitter is still changing. Even during the writing of this book, we had to make adjustments to the book's index as new methods were introduced and other calls were deprecated. By keeping these two processes in standalone classes, you're going to save yourself some headaches down the road. If you are planning on building a full-scale Twitter app, we recommend bookmarking the website for this book and the Twitter API site. Really! It changes and grows that much.
Also, somewhat like a database, you can store information in Twitter. For example, a much-overlooked feature is favorites. This API call allows you to save tweets. This can be quite useful as a means of understanding which tweeters and types of tweets a user tends to favor. New to the API list is lists. This is a list of tweeters a user creates. Again, it's a powerful bit of information that can be quite useful in understanding users' preferences. What is more interesting, though, is using these two API calls as storage devices if your user is under your control—a corporate account, for example. Because the user of that account does not interact with the account personally, you can use these API calls to store tweets and lists that can have greater meaning than originally intended. For example, suppose you have a corporate account for company X. We can store in the favorite list all tweets that match a certain rule, like any tweet that has an unfavorable term in the tweet. Now you have a list of tweets that public relations can examine using other than the application you developed. Also, remember you have access to user bio, location, and other elements. Again, because of how open Twitter is with its API, you can use these fields for anything—for example, including the updating of the Twitter background based on the latest message from the company, or perhaps updating the location field if you're a mobile food van, or changing the profile image based on the time of day or your mood. Instant database functionality ... of sorts! Now this does not mean you should not have a database if you intended on storing anything beyond the simple examples provided here. Also, it is not recommended to abuse this open access by placing unrelated data in these fields. Most applications will follow a simple structure, as illustrated in Figure 3.5.
Figure 3.5 Example of an architecture placing a database between API calls and an application's business logic.
Very common with Twitter applications are the capabilities to save groups, perform more advanced searches, display only new information and some threaded conversations, and so on. Although some of these structured displays can be somewhat complex, the approach you would take as a programmer is not that much different. Many of these structured displays can be achieved without storing information on the server but by using API calls and cookies instead. Consider the following example: Suppose we want to display a column of unread tweets, tweets from our "top 10" friends, three or four saved searches, and your current favorites lists. All of these can be achieved by passing variables within the existing API calls. You will actually work far harder at the UI than the backend coding. For this class of application, we want to set up our code as a series of calls that are more or less self-contained. This will make dealing with the GUI less troublesome as redesign is requested or required.
Collecting statistics from Twitter data provides great promise for research, improved discover and communications. However, this class of Twitter application is a bit harder. TwittFilter, another application created by this author is in this class as shown in Figure 3.6.
Figure 3.6 User Scoring screen of TwittFilter.
This class of Twitter application depends on creating new information through analyzing the return or returns from past Twitter calls, and storing or modifying this information on the server, typically in a database. This, however, is where we find Twitter to be the most interesting; because Twitter has such a large user base, you can gather enough data to infer information that does not have a direct user correlation. Did someone say mashup? For example, a very popular and now API-supported feature called "Trends" in Twitter is nothing more than a constant search across all messages being sent to Twitter, displaying the terms with the highest rate of occurrence. However, because of the large user base and ease of creating tweets, Trends tends to be one of the first places that news breaks.
Because we need to store information as well as grab details for analysis, we need to think about how we structure our program differently. For this class of application, we want to think of Twitter as more of a database source. Setting up our arrays that allow for ease of use within formulas, as well as pulling and pushing into databases, will be a great benefit as our analytics become more and more complex. However, if you are not white listed, you will run into the API call limit quite quickly. It's recommended that if you plan to do statics that require large sample sets or recursive calls, that you explore the streaming API.