Questo sito utilizza i cookie per migliorare l'esperienza di navigazione. Continuando la navigazione acconsenti all'utilizzo dei cookie.

How to do your own twitter application

To write our twitter application we need the registration  and authentication token, and then we need a library.

We will use twitter4j API , and to get the tokens we have to visit https://dev.twitter.com/ and register our application. 

Just to have fun we can register an application with read only access. We can't post any twits but we are allowed to make any kind of search and to view our timeline.


first of all we create a property twitter4j.properties file with the tokens and put it in the root classpath af the application: 


oauth.consumerKey=DsYZo7SDqYFF1XGtewyctQ
oauth.consumerSecret=F413TLC279fqMVrUkGDYHJ5bZNqjVCMha2QpnKkuU
oauth.accessToken=163634804-nmPM7JjNeqJnykDBor1qT0oSNfsiBNk1UofQHmbx
oauth.accessTokenSecret=D3Ni4yNCFQjD6WCWplv2You4tOeX5IJoxU1zQ8Mjai8


Then we build a very simple class that have a search method: 

package it.cimino.massimo.twitter.oauth;
import java.util.List;

public class TwitterSearch {
    
    final Twitter twitter = new TwitterFactory().getInstance();
    public List<Status> findTws(final Query query ) throws TwitterExceptio{
        QueryResult res = twitter.search(query);
        List<Status> twits = res.getTweets();
        return twits ;
    }
}

To run the class we use JUnit.

package it.cimino.massimo.twitter.oauth;
import static org.junit.Assert.*;
public class TwitterSearchTest {
    TwitterSearch tested ;
    Query query ;
    @Before
    public void setUp() throws Exception {
        tested = new TwitterSearch();
        query = new Query("from:@sourceforge");
    }
    @After
    public void tearDown() throws Exception {
        query = null ;
        tested = null ;
    }
    @Test
    public void testFindTws() {
        try {
            List<Status> twets = tested.findTws(query);
            assertNotNull(twets);
            assertTrue("no twits in the search result list ", twets.size()>0 );
            for (Iterator iterator = twets.iterator(); iterator.hasNext();) {
                Status status = (Status) iterator.next();
                System.out.println(status.getText());
                
                 for (URLEntity urle : status.getURLEntities()){
                     System.out.println(urle.getExpandedURL());
                    
                 }
                    
            }
        } catch (TwitterException e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
        
    }



Remember to put 100% of your code under unit test!
It 'the only way to be reasonably sure not to introduce errors when you make changes in  your code in any point .

Concerning  the code there is nothing to explain, twitter4j give us a "facade" class that has a reasch method:

final Twitter twitter = new TwitterFactory().getInstance();
QueryResult res = twitter.search(query);


But we have to make an object Query that have the very important information of "what to search" :
let's have a look In the unit test code: 
query = new Query("from:@sourceforge");

so we are looking for twit of a very interesting user which I highly recommend to follow : FF @sourceforce

Any other examples?



twitter search    containing both "twitter" and "search". This is the default operator.
"happy hour"    containing the exact phrase "happy hour".
love OR hate    containing either "love" or "hate" (or both).
beer -root    containing "beer" but not "root".
#haiku    containing the hashtag "haiku".
from:alexiskold    sent from person "alexiskold".
to:techcrunch    sent to person "techcrunch".
@mashable    referencing person "mashable".
"happy hour" near:"san francisco"    containing the exact phrase "happy hour" and sent near "san francisco".
near:NYC within:15mi    sent within 15 miles of "NYC".
superhero since:2011-07-14    containing "superhero" and sent since date "2011-07-14" (year-month-day).
ftw until:2011-07-14    containing "ftw" and sent up to date "2011-07-14".
movie -scary :)    containing "movie", but not "scary", and with a positive attitude.
flight :(    containing "flight" and with a negative attitude.
traffic ?    containing "traffic" and asking a question.
hilarious filter:links    containing "hilarious" and linking to URLs.
news source:twitterfeed    containing "news" and entered via TwitterFeed



Have fun

http://twitter4j.org/en/
https://dev.twitter.com




Share

Aggiungi commento


Codice di sicurezza
Aggiorna