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/21 20:33] – [Oauth libraries in java] 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 javaI've checked also the downloads page. Because 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. 
 + 
 +==== My first code ==== 
 + 
 +It is very hard to program with Twitter4J or even Scribe because there is no documentation availableso 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 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:  
 + 
 +<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 ===
  
-==== Create the project ====+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?
  
-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+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
 +{
  
-AQUI ME QUEDO+    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 =====
java/miningthesocialweb.1390332811.txt.gz · Last modified: 2022/12/02 22:02 (external edit)