Currently Being Moderated
Fareez Ahamed

Search Twitter from Java

Posted by Fareez Ahamed in Java Development on May 6, 2012 11:10:30 AM

Twitter is the social network used very widely for promoting products, films, etc. It is loved by companies especially for marketing. Twitter API owes the biggest part in Twitter’s success. The API is providing you the ability to do every possible operation that you can do in the website through your programs. For example, you find twitter tweets in many other sites (even in this blog) and this is achieved using Twitter API. Using this API you can do a lot of innovative things similar to the viral marketing done by Warner Bros for The Dart Knight Rises.

 

Here I would like to give you an intro to the Twitter Search API. It is simplest of all Twitter API. It doesn’t need any authentication. If you want to search all the recent tweets that contain the word ‘darkknight’, you can do it using your own program and print the results! Here I’m using Java to implement it. Now we will see about Twitter Search API.

 

 

This is the URL you are going to access to search the tweets. You have to pass a query string and the server will return you the result tweets in JSON format. Then you have to parse the returned JSON data and produce the result.

 

For example,

 

The above URL will return the tweets containing the word ‘darkknight’. Just enter the URL in your browser and see the result in view source. Similar to q there are many other query parameters available. You can get the detailed documentation of the API at https://dev.twitter.com/docs/api/1/get/search.

 

To parse the JSON data, we need a JSON parser. I used the following library for this purpose https://github.com/douglascrockford/JSON-java.

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;

import org.json.JSONArray;

import org.json.JSONObject;

 

/**

*

* @author Fareez Ahamed

* @email kn_fareez@ymail.com

*/

public class Main {

 

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) throws Exception{

        BufferedReader in = new BufferedReader(

                new InputStreamReader(System.in));

        String urlstr = "http://search.twitter.com/search.json?q=";

        StringBuffer buff = new StringBuffer();

 

        System.out.print("Search for : ");

        urlstr += in.readLine();

 

        URL url = new URL(urlstr);

        BufferedReader br = new BufferedReader(

                                    new InputStreamReader(

                                    url.openConnection().getInputStream()));

        int c;

        while((c=br.read())!=-1)

        {

            buff.append((char)c);

        }

        br.close();

 

        JSONObject js = new JSONObject(buff.toString());

        JSONArray tweets = js.getJSONArray("results");

        JSONObject tweet;

        for(int i=0;i<tweets.length();i++) {

            tweet = tweets.getJSONObject(i);

            System.out.println((i+1)+") "+tweet.getString("from_user")+" at "+tweet.getString("created_at"));

            System.out.println(tweets.getJSONObject(i).getString("text")+"\n");

        }

    }

}

 

The above program gets a string and displays the recent tweets which contain the string along with the username and tweeted time.

 

Now, in the Twitter Search API documentation, carefully go through the sample JSON data shown to understand which fields are used for what purpose. The response from the Search API is always a single JSON object. Every response JSON object contains a field named ‘results’ which is the array of tweets. Each object in the array represents a single tweet and contains all the information regarding the tweet. In the above program, I access only three fields for a simple implementation. They are, from_user which gives the username of the user who tweeted, created_at which gives the time and text which is the tweet text.

 

Output

Search for : darkknight
1) _malam at Sat, 05 May 2012 14:52:23 +0000
@__darkknight hi

2) Mauxa at Sat, 05 May 2012 14:48:13 +0000
@DarkKnight_IT Batman - The Dark Knight Rises, record di visite per il trailer inglese http://t.co/7OlV8iNl

3) Mauxa at Sat, 05 May 2012 14:47:40 +0000
@DarkKnight_IT Batman - The Dark Knight Rises, Anne Hathaway è un’ambigua Catwoman http://t.co/1kYDQf6o

4) mike_mc07 at Sat, 05 May 2012 14:47:03 +0000
#TheAvengers is the most EPIC movie since #DarkKnight

5) JulieHicks21 at Sat, 05 May 2012 14:46:57 +0000
Every time I see @mvp86hinesward running the ball on the #darkknight preview I kind of wanna cry

 

Hope you would love to try it.

 

Fareez Ahamed

URL: http://freeze-cse.blogspot.in/2012/05/search-twitter-from-your-program.html

Comments

Actions

Filter Blog

By author:
By date:
By tag: