Example usage for twitter4j.conf ConfigurationBuilder ConfigurationBuilder

List of usage examples for twitter4j.conf ConfigurationBuilder ConfigurationBuilder

Introduction

In this page you can find the example usage for twitter4j.conf ConfigurationBuilder ConfigurationBuilder.

Prototype

ConfigurationBuilder

Source Link

Usage

From source file:Logic.mongoC.java

public boolean conect() {
    try {//from   w  ww  . j a  va  2 s  . co  m
        cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey("JdGDNmMv8tAkKsETMCY7DZHKb")
                .setOAuthConsumerSecret("3FPCYWtVjbXcR1uJa2r26vvTBUWZaTL6Y7sMkGlnir1dZOcPSi")
                .setOAuthAccessToken("306043744-RpbVQtTG0cpZ3c2ItfN2QaruVEmliJ24B5NVnLjz")
                .setOAuthAccessTokenSecret("mOjCZi44GTWW7ELNaVRow4o9JBU3dqTZQCLPPGXNRg4bP");
        factory = new TwitterFactory(cb.build());
        twitter = factory.getInstance();
        mc = new MongoClient();
        db = mc.getDatabase("social_media");
        coll = db.getCollection("Usuarios");
        this.gson = new GsonBuilder().setPrettyPrinting().create();
        return true;

    } catch (Exception ex) {
        return false;
    }

}

From source file:mashup.MashTweetManager.java

public static ArrayList<String> getTweets(String topic) throws IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthAccessToken(Common.AccessToken)
            .setOAuthAccessTokenSecret(Common.AccessTokenSecret).setOAuthConsumerKey(Common.ConsumerKey)
            .setOAuthConsumerSecret(Common.ConsumerSecret);

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();//from w w w .  java2 s  .c  om

    //Twitter twitter = new TwitterFactory().getInstance();

    //  twitter.setOAuthAccessToken(null);

    ArrayList<String> tweetList = new ArrayList<>();

    try {
        Query query = new Query(topic.toLowerCase().trim());

        query.setCount(100);
        query.setLocale("en");
        query.setLang("en");

        QueryResult result = null;

        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();

            for (Status tweet : tweets) {
                String data = tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", "");

                HinghlishStopWords.removeStopWords(data.trim());
                // Remove Special... Characters 
                // Remove stop words 
                tweetList.add(tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", ""));

            }

        } while ((query = result.nextQuery()) != null);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
    return tweetList;
}

From source file:my.twittergui.TwitterUI.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    String text;//from   w  w  w .  ja  v a2 s .  co  m
    text = jTextField1.getText();
    //jTextArea1.append(text+"\n");
    jTextArea1.append("Searching for: ");
    String[] strarray = text.split(" ");
    for (int i = 0; i < strarray.length; i++) {
        jTextArea1.append(strarray[i] + "\n");
    }

    File filed = null;
    filed = new File("C:\\Results");
    if (!filed.exists()) {
        filed.mkdir();
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("").setOAuthConsumerSecret("").setOAuthAccessToken("")
            .setOAuthAccessTokenSecret("");

    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {

            try {

                int fileno = 0;
                String strI = Integer.toString(fileno);
                String fname = "C:\\results\\DataCol" + strI + ".csv";
                File file = new File(fname);
                FileWriter bw = new FileWriter(file, true);

                if (file.length() == 0) {
                    bw.write("\"Screen Name\", text, \"created at\", geolocation, Retweet? \r\n");
                    bw.flush();
                }
                while (file.length() > 10485760) {
                    fileno += 1;
                    strI = Integer.toString(fileno);
                    fname = "DataCol" + strI + ".txt";
                    file = new File(fname);
                    bw = new FileWriter(file, true);
                    bw.write("\"Screen Name\", \"text\", \"created_at\", \"geolocation\" \r\n");
                    bw.flush();

                }

                // if(!status.isRetweet()){
                bw.write("\r\n");
                bw.write("\"" + status.getUser().getScreenName() + "\",");
                String tweettxt = status.getText();
                tweettxt = tweettxt.replace("\n", "");
                tweettxt = tweettxt.replace(",", "");
                tweettxt = tweettxt.replace("\"", "");
                bw.write("\"" + tweettxt + "\",");

                bw.write("\"" + status.getCreatedAt() + "\",");
                if (status.getGeoLocation() != null) {
                    bw.write("\"" + status.getGeoLocation() + "\"");
                } else
                    bw.write("N/A,");
                if (status.isRetweet())
                    bw.write("Yes");
                else
                    bw.write("No");

                bw.flush();

                bw.close();
                //System.out.print("\n");
                String str = "@" + status.getUser().getScreenName() + "  " + status.getText() + " "
                        + status.getCreatedAt() + " ";
                if (status.getGeoLocation() != null)
                    str += status.getGeoLocation();
                //System.out.print(str);
                jTextArea1.append(str + "\n");
                // }

                bw.close();
            } catch (IOException e) {
                //System.out.print("EXCEPTION");
                e.printStackTrace();
            }

        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onStallWarning(StallWarning stall) {
        }

        public void onScrubGeo(long a, long b) {
        }

        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
    TwitterStream twitterStream = tf.getInstance();
    twitterStream.addListener(listener);
    // Filter
    FilterQuery filtre = new FilterQuery();
    Scanner in = new Scanner(System.in);

    filtre.track(strarray);
    twitterStream.filter(filtre);

}

From source file:net.archenemy.archenemyapp.model.TwitterAdapter.java

License:Apache License

private Twitter getAuthorizedTwitterInstance(Context context) {

    TwitterAuthToken authToken = com.twitter.sdk.android.Twitter.getSessionManager().getActiveSession()
            .getAuthToken();/*from w w  w  .  j a v  a  2s. c  o  m*/

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setDebugEnabled(true).setOAuthConsumerKey(getKey(context))
            .setOAuthConsumerSecret(getSecret(context)).setOAuthAccessToken(authToken.token)
            .setOAuthAccessTokenSecret(authToken.secret);

    return new TwitterFactory(configurationBuilder.build()).getInstance();
}

From source file:net.codestory.http.filters.twitter.TwitterAuthFilter.java

License:Apache License

private static Authenticator createAuthenticator(String oAuthKey, String oAuthSecret) {
    Configuration config = new ConfigurationBuilder().setOAuthConsumerKey(oAuthKey)
            .setOAuthConsumerSecret(oAuthSecret).build();

    TwitterFactory twitterFactory = new TwitterFactory(config);

    return new TwitterAuthenticator(twitterFactory);
}

From source file:net.hekatoncheir.speechtweet.SpeechTweet.java

License:Apache License

private boolean verifyCredentials() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    String accessToken = sharedPreferences.getString("accessToken", null);
    String accessTokenSecret = sharedPreferences.getString("accessTokenSecret", null);
    if (accessToken == null || accessTokenSecret == null)
        return false;

    // Read configuration
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(mOAuthConsumerKey).setOAuthConsumerSecret(mOAuthConsumerSecret)
            .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret);

    ///*  ww  w  .  j a  v a  2s  .  co  m*/
    mIsVerifyCredentials = true;
    mRingPlayer = MediaPlayer.create(SpeechTweet.this, R.raw.ring);
    mRingPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            if (mIsVerifyCredentials) {
                mRingPlayer.start();
            }
        }
    });
    mRingPlayer.start();
    showMessage(R.string.message_ring);

    TwitterFactory factory = new TwitterFactory(cb.build());
    mTwitter = factory.getInstance();
    new Thread(new Runnable() {
        @Override
        public void run() {
            User user = null;
            try {
                user = mTwitter.verifyCredentials();
            } catch (TwitterException e) {
            }
            final User u = user;
            mHandler.post(new Runnable() {
                public void run() {
                    SpeechTweet.this.finishVerifyCredentials(u);
                }
            });
        }
    }).start();
    return true;
}

From source file:net.hekatoncheir.speechtweet.SpeechTweet.java

License:Apache License

private void startOAuth() {
    mIsOpenOAuth = true;/*  w w  w .  j  a  va 2s  .  c o  m*/

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(mOAuthConsumerKey)
            .setOAuthConsumerSecret(mOAuthConsumerSecret);

    TwitterFactory factory = new TwitterFactory(cb.build());
    mTwitter = factory.getInstance();

    new Thread(new Runnable() {
        @Override
        public void run() {
            Boolean isSuccess = false;
            try {
                mRequestToken = mTwitter.getOAuthRequestToken(mOAuthCallbackURL);
                isSuccess = true;
            } catch (TwitterException e) {
            }
            final Boolean s = isSuccess;
            mHandler.post(new Runnable() {
                public void run() {
                    SpeechTweet.this.finishGetOAuthRequestToken(s);
                }
            });
        }
    }).start();
}

From source file:net.nokok.twitduke.core.twitter.ConfigurationProvider.java

License:Open Source License

/**
 * TwitDuke?Consumer/ConsumerSecret???Configuration???
 * ???????????????????????/*from ww  w.  j  a  va  2 s  .c  o  m*/
 *
 * @return Consumer/ConsumerSecret???TwitDukeConfiguration
 */
public static Configuration getConfiguration() {
    return new ConfigurationBuilder().setOAuthConsumerKey("VOIW6nzPVPEGyILu0kgMRQ")
            .setOAuthConsumerSecret("x42tjv2Xrzsi3p5hfiGSYSiNLfa7VZv8Ozd0VHEaQ").build();
}

From source file:net.wasdev.gameon.auth.twitter.TwitterAuth.java

License:Apache License

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    ConfigurationBuilder c = new ConfigurationBuilder();
    c.setOAuthConsumerKey(key).setOAuthConsumerSecret(secret);

    Twitter twitter = new TwitterFactory(c.build()).getInstance();
    request.getSession().setAttribute("twitter", twitter);

    try {/*from   ww w .j a  v  a2  s .  c  o  m*/
        // twitter will tell the users browser to go to this address once
        // they are done authing.
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/TwitterCallback");

        // to initiate an auth request, twitter needs us to have a request
        // token.
        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());

        // stash the request token in the session.
        request.getSession().setAttribute("requestToken", requestToken);

        // send the user to twitter to be authenticated.
        response.sendRedirect(requestToken.getAuthenticationURL());

    } catch (TwitterException e) {
        throw new ServletException(e);
    }

}

From source file:net.wasdev.gameon.auth.twitter.TwitterCallback.java

License:Apache License

/**
 * Method that performs introspection on an AUTH string, and returns data as
 * a String->String hashmap.//  ww w. ja v a 2  s  . com
 * 
 * @param auth
 *            the authstring to query, as built by an auth impl.
 * @return the data from the introspect, in a map.
 * @throws IOException
 *             if anything goes wrong.
 */
public Map<String, String> introspectAuth(String token, String tokensecret) throws IOException {
    Map<String, String> results = new HashMap<String, String>();

    ConfigurationBuilder c = new ConfigurationBuilder();
    c.setOAuthConsumerKey(key).setOAuthConsumerSecret(secret).setOAuthAccessToken(token)
            .setOAuthAccessTokenSecret(tokensecret);

    Twitter twitter = new TwitterFactory(c.build()).getInstance();

    try {
        // ask twitter to verify the token & tokensecret from the auth
        // string
        // if invalid, it'll throw a TwitterException
        twitter.verifyCredentials();

        // if it's valid, lets grab a little more info about the user.
        long id = twitter.getId();
        ResponseList<User> users = twitter.lookupUsers(id);
        User u = users.get(0);
        String name = u.getName();
        String screenname = u.getScreenName();

        results.put("valid", "true");
        results.put("id", "twitter:" + id);
        results.put("name", name);
        results.put("screenname", screenname);

    } catch (TwitterException e) {
        results.put("valid", "false");
    }

    return results;
}