User Tools

Site Tools


java:miningthesocialweb

Mining the social web, 2nd edition in Java

Introduction

I'm reading the book “Mining the social web, 2nd edition”. Look how cute it is:

The book gives a deep insight of how to exploit data from social websites, and it's a topic I am quite interested. However, the recipes are in python, but I want to make my own recipes in java. This page is for register my achievements (if any) and notes, because there are tons of link that might be interested or worth reading.

Twitter

The first chapter of the book deals with Twitter. To make my own java excursion into the topic, I've read some things.

Pick the libraries

Ive selected those libraries for my access to Twitter with java:

Why this libraries?? The process I've followed is to watch some projects in GitHub I was interested in and follow the projects: if there were messages, add one point. Then I've looked to the readme page of the project, and I was pleased to to read in the case of Scribe that this library has plenty of examples of a vast collection of social networks, so I thought it was nice to have half of the work done.

You may also need one library for the service Yahoo Geoplanet because it's needed to locate the trending topics by location id.

My first code

It is very hard to program with Twitter4J or even Scribe because there is no documentation available, so most of the time you will be trying to guess what's the object/method you need to instantiate to go to the next step.

Because I was so lost in the beginning, I've decided to follow simple goals at a time, mimicking the different steps of the book. Here are my steps:

Basic Object

I've created a basic to hold all my examples:

public class Examples {
 
	private ConfigurationBuilder cb;
	private TwitterFactory tf;
	private Twitter twitter;
 
	public Examples()
	{
		// creating of the Twitter object
        cb = new ConfigurationBuilder();
 
        cb.setDebugEnabled(true)
            .setOAuthConsumerKey("HERE-GOES-YOUR-CONSUMER-KEY")
        .setOAuthConsumerSecret("HERE-GOES-YOUR-CONSUMER-SECRET")
        .setOAuthAccessToken("HERE-GOES-YOUR-ACCESS-TOKEN")
        .setOAuthAccessTokenSecret("HERE-GOES-YOUR-ACCESS-TOKEN-SECRET");
 
        tf = new TwitterFactory(cb.build());
        twitter = tf.getInstance();
 
	}
 
	public Twitter getConfiguredTwitter()
  {
      return twitter;
  } // sendDirectMessage
 
	public void getHomeTimeLine() throws TwitterException
	{
		  List<Status> statuses = twitter.getHomeTimeline();
		  for( Status status : statuses )
			  System.out.println( status.getUser().getName() + ": " + status.getText() );
	}
 
}

This example displays the trending topics of all the world. The WOEID1) of the whole world is 1.

/**
 * Example of page 16, bullet 1.3.3
 */
public void exploringTrendingTopics() throws TwitterException
{
    Trends trends = twitter.trends().getPlaceTrends( 1 );
 
    for( Trend trend : trends.getTrends() )
    {
          System.out.println( trend.toString() );
        System.out.println( "Trend: " + trend.getName() );
        System.out.println( "  Url: " + trend.getURL() );
    }
} // exploringTrendingTopics

It's possible that not all the trending topics can be consulted. For instance, I've tried with other locations (cities in Spain) and it doesn't work. It does work searching for the trending topics of Spain, for instance. Would work searching for little countries like Andorra or Cittá del Vaticano?

However, you should register in Yahoo! Geoplanet to get the ID for Spain or other country.

/**
 * Example of page 16, bullet 1.3.3
 */
public void exploringTrendingTopics() throws TwitterException, GeoPlanetException
{
 
    GeoPlanet g = new GeoPlanet("PUT-HERE-YOUR-PROJECT-CONSUMER-KEY");
    PlaceCollection places = g.getPlaces("Spain");
 
    // take only the first ocurrence,
    // who is the country
    Place spain = places.get(0);
 
    if( spain == null )
        return;
 
    Trends trends = twitter.trends().getPlaceTrends( (int) spain.getWoeId() );
 
    for( Trend trend : trends.getTrends() )
    {
          System.out.println( trend.toString() );
        System.out.println( "Trend: " + trend.getName() );
        System.out.println( "  Url: " + trend.getURL() );
    }
} // exploringTrendingTopics

Oauth libraries in java

Learning path for Twitter

Apart from the book, which I encourage to read even you are not keen on python, these are my readings:

Introduction to OAuth in plain english

If you are like me, probably OAuth is a technology that doesn't seem clear to you. I encourage you to navigate and read about this topic. Moreover, this is the base of many “Sign in with Twitter”, “Sign in with facebook” and the like.

Facebook

http://restfb.com/

RestFB is a simple and flexible Facebook Graph API and Old REST API client written in Java. It is open source software released under the terms of the MIT License.

1)
Where On Earth ID, Yahoo Service, http://developer.yahoo.com/geo/geoplanet/
java/miningthesocialweb.txt · Last modified: 2014/12/24 10:42 (external edit)