clientetwitter.ClienteTwitter.java Source code

Java tutorial

Introduction

Here is the source code for clientetwitter.ClienteTwitter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package clientetwitter;

import twitter4j.Status;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author siciliandefense
 */
public class ClienteTwitter {

    /**
     * @param args the command line arguments
     * @throws twitter4j.TwitterException
     */
    public static void main(String[] args) throws TwitterException {

        //its just sending random numbers from 1 to 10 (i didnt know twitter
        //doesnt let duplicated messages so i had to try with random numbers)
        Timer timer = new Timer();
        TimerTask task;
        ReadTimeLine();

        task = new TimerTask() {
            @Override
            public void run() {
                try {
                    int numAleatorio = new Random().nextInt(10) + 1;
                    String texto = String.valueOf(numAleatorio);
                    PostInTimeLine(texto);
                } catch (TwitterException ex) {
                    Logger.getLogger(ClienteTwitter.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };

        timer.schedule(task, 1000, 30000); //posts every 30 seconds

    }

    public static void PostInTimeLine(String texto) throws TwitterException {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey("xxxxxxxxxxxx") //to get all the OAuth go to apps.twitter.com 
                .setOAuthConsumerSecret("xxxxxxxxxxxxxxxx").setOAuthAccessToken("xxxxxxxxxxxxxxxxxxxxxxx")
                .setOAuthAccessTokenSecret("xxxxxxxxxxxxxxxxxxxx");

        TwitterFactory tf = new TwitterFactory(cb.build());
        twitter4j.Twitter tw = tf.getInstance();

        //posting
        Status stat = tw.updateStatus(texto);
        System.out.println("Posted");

    }

    public static void ReadTimeLine() throws TwitterException {

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey("ZpF1TSpAyQlBLMN7egUz3uT3D")
                .setOAuthConsumerSecret("r2icg36QB6G862Re2IvwHNGuDK25z5awKSeNG9kV9LRpcAmuOW")
                .setOAuthAccessToken("4314010284-QGXKFbFQ5TK4zqbplRsfoP0wL6NTv7bsMJypWex")
                .setOAuthAccessTokenSecret("4v61YXTkzM3Kob0xAHu59ISgM0fTKSLrOuDXTG5ctX7rr");

        TwitterFactory tf = new TwitterFactory(cb.build());
        twitter4j.Twitter tw = tf.getInstance();

        //reading
        List<Status> statuses = tw.getHomeTimeline();
        for (Status status1 : statuses) {
            System.out.println(status1.getUser().getName() + ": " + status1.getText());
        }

    }

}