List of usage examples for twitter4j.conf ConfigurationBuilder setDebugEnabled
public ConfigurationBuilder setDebugEnabled(boolean debugEnabled)
From source file:org.socialnetlib.android.TwitterApi.java
License:Apache License
Twitter getAndConfigureApiInstance() { if (mCurrentOAuthToken == null || mCurrentOAuthSecret == null) { mSocNetApi = null;//from w ww .ja v a 2s . com mOAuth = null; } else if (mSocNetApi == null) { ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setDebugEnabled(true).setOAuthConsumerKey(mAppConsumerKey) .setOAuthConsumerSecret(mAppConsumerSecret).setOAuthAccessToken(mCurrentOAuthToken) .setOAuthAccessTokenSecret(mCurrentOAuthSecret).setMediaProvider("TWITTER") // .setJSONStoreEnabled(true) .setIncludeEntitiesEnabled(true); Configuration configuration = configurationBuilder.build(); mSocNetApi = new TwitterFactory(configuration).getInstance(); // mOAuth = new // TwitterFactory(configuration).getOAuthAuthorization(); } return mSocNetApi; }
From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java
License:Open Source License
/** * Establishes a connection with twitter based on the user information given * in the configuration.//w w w. j a v a 2s. com * * @return True if it is possible to use the user information for the * connection, false otherwise. */ private boolean establishConnection() { // get property values from configuration String consumerKey = source.getPropertyValue(TwitterProperties.CONSUMER_KEY_PROPERTY); String consumerSecret = source.getPropertyValue(TwitterProperties.CONSUMER_SECRET_PROPERTY); String accessTokenValue = source.getPropertyValue(TwitterProperties.ACCESS_TOKEN_PROPERTY); String accessTokenSecret = source.getPropertyValue(TwitterProperties.ACCESS_TOKEN_SECRET_PROPERTY); // check properties if (consumerKey == null || consumerKey.isEmpty()) { log("A valid consumer key is needed in the configuration specified by " + TwitterProperties.CONSUMER_KEY_PROPERTY, LogService.LOG_WARNING); return false; } else if (consumerSecret == null || consumerSecret.isEmpty()) { log("A valid consumer secret is needed in the configuration specified by " + TwitterProperties.CONSUMER_SECRET_PROPERTY, LogService.LOG_WARNING); return false; } else if (accessTokenValue == null || accessTokenValue.isEmpty()) { log("A valid access token is needed in the configuration specified by " + TwitterProperties.ACCESS_TOKEN_PROPERTY, LogService.LOG_WARNING); return false; } else if (accessTokenSecret == null || accessTokenSecret.isEmpty()) { log("A valid token secret is needed in the configuration specified by " + TwitterProperties.ACCESS_TOKEN_SECRET_PROPERTY, LogService.LOG_WARNING); return false; } // get access with the provided credencials // TODO disable debug ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(accessTokenValue).setOAuthAccessTokenSecret(accessTokenSecret); TwitterFactory tf = new TwitterFactory(cb.build()); twitterAPI = tf.getInstance(); if (twitterAPI == null) { log("Could not create a connection to the twitter api!", LogService.LOG_ERROR); } return true; }
From source file:org.twitter.sample.main.Stream.java
public void getTweetsFromTwitter() { StatusListener listener = new StatusListener() { private boolean logQueueFull = true; @Override//from www . ja v a2 s. c om public void onException(Exception e) { log.error(e); } @Override public void onDeletionNotice(StatusDeletionNotice notice) { } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onStatus(Status status) { db.insert(status); } @Override public void onTrackLimitationNotice(int notice) { log.warn("*** TRACK LIMITATION REACHED: " + notice + " ***"); } @Override public void onStallWarning(StallWarning arg0) { log.warn("*** STALL WARNING: " + arg0); } }; ConfigurationBuilder twitterConfig = new ConfigurationBuilder(); twitterConfig.setDebugEnabled(false); twitterConfig.setOAuthAccessTokenSecret(access_token_secret); twitterConfig.setOAuthAccessToken(access_token); twitterConfig.setOAuthConsumerKey(consumer_key); twitterConfig.setOAuthConsumerSecret(consumer_secret); twitterConfig.setJSONStoreEnabled(true); TwitterStreamFactory fact = new TwitterStreamFactory(twitterConfig.build()); this.twitterStream = fact.getInstance(); this.twitterStream.addListener(listener); FilterQuery filterQuery = new FilterQuery(); filterQuery.language(new String[] { "en" }); this.twitterStream.filter(filterQuery); this.twitterStream.sample(); }
From source file:org.uma.jmetalsp.application.biobjectivetsp.sparkutil.StreamingConfigurationTSP.java
License:Open Source License
/** * Create Twitter configuration/*from ww w. java 2s. c o m*/ * @param consumerKey * @param consumerSecret * @param accessToken * @param accessTokenSecret * @param proxyHost * @param proxyPort * @param twitterFilter */ public void initializeTwitter(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret, String proxyHost, int proxyPort, String twitterFilter) { this.twitterFilter = twitterFilter; ConfigurationBuilder cb = new ConfigurationBuilder(); if (proxyHost != null) { System.setProperty("http.proxyHost", proxyHost); System.setProperty("http.proxyPort", String.valueOf(proxyPort)); System.setProperty("https.proxyHost", proxyHost); System.setProperty("https.proxyPort", String.valueOf(proxyPort)); } cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret); Configuration twitterConf = cb.build(); twitterAuth = AuthorizationFactory.getInstance(twitterConf); }
From source file:org.voiser.zoe.TwitterAgent.java
License:MIT License
/** * // ww w . ja v a2s .c om */ public TwitterAgent() { String consumerKey = System.getenv("zoe_twitter_consumer_key"); String consumerSecret = System.getenv("zoe_twitter_consumer_secret"); String accessToken = System.getenv("zoe_twitter_access_token"); String accessTokenSecret = System.getenv("zoe_twitter_access_token_secret"); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret).setUseSSL(true); TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance(); }
From source file:org.wso2.carbon.inbound.custom.poll.TwitterStreamData.java
License:Open Source License
/** * Setting up a connection with Twitter Stream API with the given * credentials/* ww w.j ava 2 s.c o m*/ * * @throws TwitterException */ private void setupConnection() throws TwitterException { if (log.isDebugEnabled()) { log.debug("Starting to setup the connection with the twitter streaming endpoint"); } ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setDebugEnabled(true).setOAuthConsumerKey(consumerKey) .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken) .setOAuthAccessTokenSecret(accessSecret); StatusListener statusStreamsListener; UserStreamListener userStreamListener; SiteStreamsListener siteStreamslistener; TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(); String twitterOperation = properties.getProperty(TwitterConstant.TWITTER_OPERATION); if (twitterOperation.equals(TwitterConstant.FILTER_STREAM_OPERATION) || twitterOperation.equals(TwitterConstant.FIREHOSE_STREAM_OPERATION) || twitterOperation.equals(TwitterConstant.LINK_STREAM_OPERATION) || twitterOperation.equals(TwitterConstant.SAMPLE_STREAM_OPERATION)) { statusStreamsListener = new StatusListenerImpl(); twitterStream.addListener(statusStreamsListener); } else if (twitterOperation.equals(TwitterConstant.USER_STREAM_OPERATION)) { userStreamListener = new UserStreamListenerImpl(); twitterStream.addListener(userStreamListener); } else if (twitterOperation.equals(TwitterConstant.SITE_STREAM_OPERATION)) { siteStreamslistener = new siteStreamsListenerImpl(); twitterStream.addListener(siteStreamslistener); } else { handleException("The operation :" + twitterOperation + " not found"); } /* Synchronously retrieves public statuses that match one or more filter predicates.*/ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.FILTER_STREAM_OPERATION)) { FilterQuery query = new FilterQuery(); if (languages != null) { query.language(languages); } if (tracks != null) { query.track(tracks); } if (follow != null) { query.follow(follow); } if (filterLevel != null) { query.filterLevel(filterLevel); } query.count(count); twitterStream.filter(query); } /* Returns a small random sample of all public statuses. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.SAMPLE_STREAM_OPERATION)) { if (languages != null) { if (languages.length == 1) { twitterStream.sample(languages[1]); } else { handleException("A language can be used for the sample operation"); } } twitterStream.sample(); } /* Asynchronously retrieves all public statuses.*/ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.FIREHOSE_STREAM_OPERATION)) { if (countParam != null) { twitterStream.firehose(count); } } /* User Streams provide a stream of data and events specific to the authenticated user.This provides to access the Streams messages for a single user. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.USER_STREAM_OPERATION)) { if (tracks != null) { twitterStream.user(tracks); } twitterStream.user(); } /* * Link Streams provide asynchronously retrieves all statuses containing 'http:' and 'https:'. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.LINK_STREAM_OPERATION)) { if (countParam != null) { twitterStream.links(count); } } /* * User Streams provide a stream of data and events specific to the * authenticated user.This provides to access the Streams messages for a * single user. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.RETWEET_STREAM_OPERATION)) { twitterStream.retweet(); } /* * Site Streams allows services, such as web sites or mobile push * services, to receive real-time updates for a large number of users. * Events may be streamed for any user who has granted OAuth access to * your application. Desktop applications or applications with few users * should use user streams. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.SITE_STREAM_OPERATION)) { twitterStream.site(withFollowings, follow); } }
From source file:org.wso2.carbon.inbound.twitter.poll.TwitterStreamData.java
License:Open Source License
/** * Setting up a connection with Twitter Stream API with the given * credentials/*from w w w .ja va 2 s . c o m*/ * * @throws TwitterException */ private void setupConnection() throws TwitterException { if (log.isDebugEnabled()) { log.debug("Starting to setup the connection with the twitter streaming endpoint"); } ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setDebugEnabled(true).setJSONStoreEnabled(true).setOAuthConsumerKey(consumerKey) .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken) .setOAuthAccessTokenSecret(accessSecret); StatusListener statusStreamsListener; UserStreamListener userStreamListener; SiteStreamsListener siteStreamslistener; twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(); String twitterOperation = properties.getProperty(TwitterConstant.TWITTER_OPERATION); if (twitterOperation.equals(TwitterConstant.FILTER_STREAM_OPERATION) || twitterOperation.equals(TwitterConstant.FIREHOSE_STREAM_OPERATION) || twitterOperation.equals(TwitterConstant.LINK_STREAM_OPERATION) || twitterOperation.equals(TwitterConstant.SAMPLE_STREAM_OPERATION) || twitterOperation.equals(TwitterConstant.RETWEET_STREAM_OPERATION)) { statusStreamsListener = new StatusListenerImpl(); twitterStream.addListener(statusStreamsListener); } else if (twitterOperation.equals(TwitterConstant.USER_STREAM_OPERATION)) { userStreamListener = new UserStreamListenerImpl(); twitterStream.addListener(userStreamListener); } else if (twitterOperation.equals(TwitterConstant.SITE_STREAM_OPERATION)) { siteStreamslistener = new siteStreamsListenerImpl(); twitterStream.addListener(siteStreamslistener); } else { handleException("The operation :" + twitterOperation + " not found"); } /* Synchronously retrieves public statuses that match one or more filter predicates.*/ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.FILTER_STREAM_OPERATION)) { FilterQuery query = new FilterQuery(); if (languages != null) { query.language(languages); } if (tracks != null) { query.track(tracks); } if (follow != null) { query.follow(follow); } if (locations != null) { query.locations(locations); } if (filterLevel != null) { query.filterLevel(filterLevel); } if (follow == null & tracks == null & locations == null) { handleException("At least follow, locations, or track must be specified."); } query.count(count); twitterStream.filter(query); } /* Returns a small random sample of all public statuses. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.SAMPLE_STREAM_OPERATION)) { if (languages != null) { if (languages.length == 1) { twitterStream.sample(languages[1]); } else { handleException("A language can be used for the sample operation"); } } else { twitterStream.sample(); } } /* Asynchronously retrieves all public statuses.*/ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.FIREHOSE_STREAM_OPERATION)) { if (countParam != null) { twitterStream.firehose(count); } } /* User Streams provide a stream of data and events specific to the authenticated user.This provides to access the Streams messages for a single user. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.USER_STREAM_OPERATION)) { if (tracks != null) { twitterStream.user(tracks); } else { twitterStream.user(); } } /* * Link Streams provide asynchronously retrieves all statuses containing 'http:' and 'https:'. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.LINK_STREAM_OPERATION)) { if (countParam != null) { twitterStream.links(count); } } /* * User Streams provide a stream of data and events specific to the * authenticated user.This provides to access the Streams messages for a * single user. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.RETWEET_STREAM_OPERATION)) { twitterStream.retweet(); } /* * Site Streams allows services, such as web sites or mobile push * services, to receive real-time updates for a large number of users. * Events may be streamed for any user who has granted OAuth access to * your application. Desktop applications or applications with few users * should use user streams. */ if ((properties.getProperty(TwitterConstant.TWITTER_OPERATION)) .equals(TwitterConstant.SITE_STREAM_OPERATION)) { twitterStream.site(withFollowings, follow); } }
From source file:Origin.Mypage.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, TwitterException { response.setContentType("text/html;charset=UTF-8"); /* TODO output your page here. You may use following sample code. */ ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(CONSUMER_KEY); cb.setOAuthConsumerSecret(CONSUMER_SECRET); cb.setOAuthAccessToken(ACCESS_TOKEN); cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();/*from www.ja v a 2 s. c o m*/ User user = twitter.verifyCredentials(); request.setCharacterEncoding("UTF-8"); HttpSession hs = request.getSession(); UserData ud = (UserData) hs.getAttribute("ud"); String search = ud.getLine() + "?"; //String search= request.getParameter("searchtweet"); Query query = new Query(); query.setCount(100); query.setQuery(search); QueryResult queryresult = null; try { queryresult = twitter.search(query); } catch (TwitterException e1) { e1.printStackTrace(); } ArrayList<String> userID = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { userID.add(tweet.getUser().getScreenName()); } ArrayList<String> profileimg = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { profileimg.add(tweet.getUser().getBiggerProfileImageURL()); } ArrayList<String> username = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { username.add(tweet.getUser().getName() + "<br>" + "@" + tweet.getUser().getScreenName()); } ArrayList<String> usernameURL = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { username.add(tweet.getUser().getURL()); } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); ArrayList<String> resulttweet = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { resulttweet.add(tweet.getText() + "<br>" + sdf.format(tweet.getCreatedAt())); } request.setAttribute("search", search); request.setAttribute("userID", userID); request.setAttribute("profileimg", profileimg); request.setAttribute("username", username); request.setAttribute("resulttweet", resulttweet); request.setAttribute("usernameURL", usernameURL); request.getRequestDispatcher("/mypage.jsp").forward(request, response); }
From source file:Origin.Searchtweet.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, TwitterException { response.setContentType("text/html;charset=UTF-8"); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(CONSUMER_KEY); cb.setOAuthConsumerSecret(CONSUMER_SECRET); cb.setOAuthAccessToken(ACCESS_TOKEN); cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();//from www . ja va 2 s. co m User user = twitter.verifyCredentials(); request.setCharacterEncoding("UTF-8"); String search = "?" + request.getParameter("searchtweet"); //String search= request.getParameter("searchtweet"); Query query = new Query(); query.setCount(100); query.setQuery(search); QueryResult queryresult = null; try { queryresult = twitter.search(query); } catch (TwitterException e1) { e1.printStackTrace(); } ArrayList<String> userID = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { userID.add(tweet.getUser().getScreenName()); } ArrayList<String> profileimg = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { profileimg.add(tweet.getUser().getBiggerProfileImageURL()); } ArrayList<String> username = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { username.add(tweet.getUser().getName() + "<br>" + "@" + tweet.getUser().getScreenName()); } ArrayList<String> usernameURL = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { username.add(tweet.getUser().getURL()); } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); ArrayList<String> resulttweet = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { resulttweet.add(tweet.getText() + "<br>" + sdf.format(tweet.getCreatedAt())); } request.setAttribute("search", search); request.setAttribute("userID", userID); request.setAttribute("profileimg", profileimg); request.setAttribute("username", username); request.setAttribute("resulttweet", resulttweet); request.setAttribute("usernameURL", usernameURL); request.getRequestDispatcher("/searchtweet.jsp").forward(request, response); }
From source file:Origin.Timeline.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(CONSUMER_KEY); cb.setOAuthConsumerSecret(CONSUMER_SECRET); cb.setOAuthAccessToken(ACCESS_TOKEN); cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); try {/*from ww w .j a v a2 s . c om*/ Twitter twitter = new TwitterFactory(cb.build()).getInstance(); User user = twitter.verifyCredentials(); Paging paging = new Paging(1, 200); ResponseList<Status> userstatus = twitter.getHomeTimeline(paging); request.setAttribute("userstatus", userstatus); request.getRequestDispatcher("/timeline.jsp").forward(request, response); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } }