User Tools

Site Tools


java:miningthesocialweb

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java:miningthesocialweb [2014/01/18 18:58] rlunarojava:miningthesocialweb [2022/12/02 22:02] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Mining the social web, 2nd edition ======+====== Mining the social web, 2nd edition in Java ======
  
 ===== Introduction ===== ===== Introduction =====
Line 17: Line 17:
 Ive selected those libraries for my access to Twitter with java: Ive selected those libraries for my access to Twitter with java:
  
-   * [[https://www.github.com/twitter/hbc|Official Twitter Library for Java]]+ 
 +   * [[http://twitter4j.org/]]
    * [[https://github.com/fernandezpablo85/scribe-java|Scribe]]    * [[https://github.com/fernandezpablo85/scribe-java|Scribe]]
 +   * http://www.winterwell.com/software/jgeoplanet/
  
 **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.  **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. 
  
-In the case of the twitter library for java, I've checked also the downloads pageBecause it was the most downloaded of all, I've decided to use that+You may also need one library for the service [[http://developer.yahoo.com/geo/geoplanet/|Yahoo Geoplanet]] because it's needed to locate the trending topics by location id.
  
-==== Create the project ====+==== My first code ====
  
-If you have experience with [[http://maven.apache.org|Maven]] you will be pleased to read that the only thing you have to do is to add the dependency to your project and that's all. But it was not my case, so I decided to use a dummy project to download the dependencies from Maven and then use it in a normal eclipse project+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'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: 
  
-AQUI ME QUEDO+=== Basic Object ===
  
 +I've created a basic to hold all my examples: 
 +
 +<code java>
 +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() );
 + }
 +
 +}
 +</code>
 +
 +
 +=== Get world trending topics ===
 +
 +This example displays the trending topics of all the world. The WOEID((Where On Earth ID, Yahoo Service, http://developer.yahoo.com/geo/geoplanet/)) of the whole world is 1. 
 +
 +
 +<code java>
 +/**
 + * 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
 +</code>
 +
 +=== Get trending topics by country ===
 +
 +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 [[http://developer.yahoo.com/geo/geoplanet/|Yahoo! Geoplanet]] to get the ID for Spain or other country.
 +
 +<code java>
 +/**
 + * 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
 +</code>
  
 ===== Oauth in Java: links to investigate ===== ===== Oauth in Java: links to investigate =====
Line 42: Line 141:
  
 [[https://github.com/fernandezpablo85/scribe-java|Scribe (178 people watching in github)]] [[https://github.com/fernandezpablo85/scribe-java|Scribe (178 people watching in github)]]
 +
 +I've tried the example of the Twitter library for this library and it doesn't work, so I've decided to abandon this library.
  
 [[http://docs.spring.io/spring-security/oauth/|OAuth for spring security (138 people watching in github)]] [[http://docs.spring.io/spring-security/oauth/|OAuth for spring security (138 people watching in github)]]
java/miningthesocialweb.1390067912.txt.gz · Last modified: 2022/12/02 22:02 (external edit)