List of usage examples for twitter4j TwitterFactory getSingleton
public static Twitter getSingleton()
From source file:NewJFrame.java
private void TweetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TweetButtonActionPerformed String message = TweetArea.getText(); Twitter twitter = TwitterFactory.getSingleton(); twitter.setOAuthConsumer(API_KEY, API_SECRET); try {//from w w w . j a v a 2 s.com RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (null == accessToken) { String pin = br.readLine(); if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(); } } storeAccessToken(twitter.verifyCredentials().getId(), accessToken); } catch (TwitterException | IOException ex) { Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ac.simons.tweetarchive.Application.java
License:Apache License
static void createTwitterOauthTokens(final String consumerKey, final String consumerSecret) throws Exception { final Twitter twitter = TwitterFactory.getSingleton(); twitter.setOAuthConsumer(consumerKey, consumerSecret); final RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken = null;//ww w. j ava 2 s. c o m final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (null == accessToken) { System.out.println("Open the following URL and grant access to your account:"); System.out.println(requestToken.getAuthorizationURL()); System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:"); String pin = br.readLine(); try { if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(); } } catch (TwitterException te) { if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else { throw te; } } } final Properties properties = new Properties(); properties.put("twitter4j.oauth.consumerKey", consumerKey); properties.put("twitter4j.oauth.consumerSecret", consumerSecret); properties.put("twitter4j.oauth.accessToken", accessToken.getToken()); properties.put("twitter4j.oauth.accessTokenSecret", accessToken.getTokenSecret()); try (final FileOutputStream out = new FileOutputStream("application.properties", true)) { properties.store(out, null); } }
From source file:aic2013.extractor.TwitterExtractor.java
public static void main(String[] args) throws Exception { EntityManagerFactory emf = null;//from w w w. ja v a 2 s .c o m MongoClient mongoClient = null; Twitter twitter = null; Neo4jConnection neo4j = null; try { // Database configuration Properties prop = new Properties(); prop.load(new FileInputStream("hibernate.properties")); // prop.load(TwitterExtractor.class.getClassLoader().getResourceAsStream("hibernate.properties")); emf = Persistence.createEntityManagerFactory("twitterdb", prop); final String mongoServer = "localhost"; mongoClient = new MongoClient(mongoServer); twitter = TwitterFactory.getSingleton(); // final String neo4jJdbc = "jdbc:neo4j:ec2-54-217-131-208.eu-west-1.compute.amazonaws.com:7474"; final String neo4jJdbc = "jdbc:neo4j://localhost:7474"; neo4j = new Driver().connect(neo4jJdbc, new Properties()); neo4j.setAutoCommit(true);//false); final TwitterDataAccess twitterDataAccess = new TwitterDataAccess(twitter); final MongoDataAccess mongoDataAccess = new MongoDataAccess(mongoClient); final Neo4jService neo4jService = new Neo4jService(neo4j); final EntityManager em = emf.createEntityManager(); final UserDataAccess userDataAcces = new UserDataAccess(em); final TopicExtractionCoordinator extractionCoordinator = new TopicExtractionCoordinatorImpl(); // final ConcurrentLinkedQueue<E> final UserService userService = new UserService(em); final Processor<Status> mongoProcessor = new Processor<Status>() { @Override public void process(final Status status) { final TwitterUser user = new TwitterUser(status.getUser()); userService.persist(user); neo4jService.transactional(new Neo4jUnitOfWork() { @Override public void process() throws SQLException { neo4jService.createPersonIfAbsent(user); // HashtagEntity[] topics = // status.getHashtagEntities(); Status originalStatus = status; if (status.isRetweet()) { originalStatus = status.getRetweetedStatus(); } extractionCoordinator.doExtraction(originalStatus, new TopicExtractionCallback() { @Override public void handleExtractionResult(Set<Topic> extractedTopics) { /* TODO: comment out outputs */ // System.out.println(status.getText()); // for (Topic t : extractedTopics) { // System.out.println(t.getName()); // } try { /* add hash tags to topics */ for (HashtagEntity tag : status.getHashtagEntities()) { extractedTopics.add(new Topic(new String[] { tag.getText() })); } for (Topic topic : extractedTopics) { neo4jService.createTopicIfAbsent(topic); if (status.isRetweet()) { neo4jService.createRelationIfAbsent("RETWEETS", user, topic); } else { neo4jService.createRelationIfAbsent("TWEETS", user, topic); } } } catch (SQLException e) { throw new RuntimeException(e); } } @Override public void handleExtractionError(ExtractionException e) { throw new RuntimeException(e); } }); } }); } }; final Processor<TwitterUser> userProcessor = new Processor<TwitterUser>() { @Override public void process(final TwitterUser user) { try { twitterDataAccess.forAllFollowers(user, new Processor<User>() { @Override public void process(User u) { final TwitterUser follower = em.find(TwitterUser.class, u.getId()); if (follower != null) { neo4jService.transactional(new Neo4jUnitOfWork() { @Override public void process() throws SQLException { neo4jService.createUniqueRelation("FOLLOWS", user, follower); } }); } } }); } catch (Exception ex) { throw new RuntimeException(ex); } } }; // invoke the processor for every entry in mongo mongoDataAccess.forAll(mongoProcessor); extractionCoordinator.awaitTermination(-1, TimeUnit.SECONDS); // invoke the processor for every entry in the rdbms // userDataAcces.forAll(userProcessor); } finally { if (emf != null && emf.isOpen()) { emf.close(); } if (mongoClient != null) { mongoClient.close(); } if (twitter != null) { twitter.shutdown(); } if (neo4j != null) { neo4j.close(); } } }
From source file:App.PruebaTwitter.java
/** * @param args the command line arguments * @throws twitter4j.TwitterException//from www . j a v a 2 s . c om */ public static void main(String[] args) throws TwitterException { Twitter twitter = TwitterFactory.getSingleton(); String message = "Probando a twittear desde java"; Status status = twitter.updateStatus(message); }
From source file:au.com.tyo.sn.twitter.SNTwitter.java
License:Apache License
public void getAppAuthorized(String consumerKey, String consumerKeySecret) throws TwitterException { twitter = TwitterFactory.getSingleton(); try {//from ww w . ja va 2s . c o m twitter.setOAuthConsumer(consumerKey, consumerKeySecret); } catch (Exception ex) { // the consumerkey and consumerKeySecret may have been already set } requestToken = twitter.getOAuthRequestToken(getCallback().toString()); openAuthorizationURL(requestToken.getAuthorizationURL()); }
From source file:be.ugent.tiwi.sleroux.newsrec.newsreclib.twitter.AddUsersToFollowToDatabase.java
License:Apache License
public static void insert() throws DaoException { twitter = TwitterFactory.getSingleton(); AccessToken accessToken = new AccessToken(TwitterCredentialProvider.getAccessToken(), TwitterCredentialProvider.getAccessSecret()); twitter.setOAuthConsumer(TwitterCredentialProvider.get0AuthConsumerKey(), TwitterCredentialProvider.get0AuthConsumerSecret()); twitter.setOAuthAccessToken(accessToken); for (String user : users) { try {// w ww . ja va2s .c o m long id = getId(user); dao.addUserToFollow(user, id); } catch (TwitterException ex) { logger.error(ex); } } }
From source file:be.ugent.tiwi.sleroux.newsrec.newsreclib.twitter.UserHelper.java
License:Apache License
public UserHelper() { twitter = TwitterFactory.getSingleton(); AccessToken accessToken = new AccessToken(TwitterCredentialProvider.getAccessToken(), TwitterCredentialProvider.getAccessSecret()); twitter.setOAuthConsumer(TwitterCredentialProvider.get0AuthConsumerKey(), TwitterCredentialProvider.get0AuthConsumerSecret()); twitter.setOAuthAccessToken(accessToken); }
From source file:be.ugent.tiwi.sleroux.newsrec.twittertest.App.java
public static void main(String[] args) throws TwitterException { Twitter twitter = TwitterFactory.getSingleton(); AccessToken accessToken = new AccessToken("2369203675-1Pk99eotzhCznmIgr3iXb670DpsEVQCANuoBHRs", "6ua72cabIzcFsBgE15RVzULeKT44TnIrOIaf9chY9dlWe"); twitter.setOAuthConsumer("tQjT8XvB7OPNTl8qdhchDo3J2", "FXWVS3OEW7omiUDSLpET0aRInoUumGPWRxOVyk7GrhiwcfLBnV"); twitter.setOAuthAccessToken(accessToken); User user = twitter.showUser("billclinton"); System.out.println("id=" + user.getId()); StreamReaderService srs = new StreamReaderService(); srs.readTwitterFeed();/* w w w . ja v a2 s. c om*/ }
From source file:be.ugent.tiwi.sleroux.newsrec.twittertest.GrantAccess.java
public static void main(String args[]) throws Exception { // The factory instance is re-useable and thread safe. Twitter twitter = TwitterFactory.getSingleton(); twitter.setOAuthConsumer("tQjT8XvB7OPNTl8qdhchDo3J2", "FXWVS3OEW7omiUDSLpET0aRInoUumGPWRxOVyk7GrhiwcfLBnV"); RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken = null;//from ww w . j av a2s.c o m BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (null == accessToken) { System.out.println("Open the following URL and grant access to your account:"); System.out.println(requestToken.getAuthorizationURL()); System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:"); String pin = br.readLine(); try { if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(); } } catch (TwitterException te) { if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else { te.printStackTrace(); } } } storeAccessToken(twitter.verifyCredentials().getId(), accessToken); }
From source file:br.shura.team.mpsbot.runtime.ConnectedBot.java
License:Open Source License
public void prepare(Function<URL, String> pinEvaluator) throws IllegalStateException, MalformedURLException, TwitterException { ApiKeys keys = getBot().getApiKeys(); if (!isPrepared()) { Twitter handler = TwitterFactory.getSingleton(); handler.setOAuthConsumer(keys.getConsumerKey(), keys.getConsumerSecret()); RequestToken requestToken = handler.getOAuthRequestToken(); URL url = new URL(requestToken.getAuthorizationURL()); while (accessToken == null) { String pin = pinEvaluator.apply(url); try { if (pin != null && !pin.isEmpty()) { this.accessToken = handler.getOAuthAccessToken(requestToken, pin); this.handler = handler; } else { return; }/* w ww . jav a2s. c o m*/ } catch (TwitterException exception) { System.err.println("Could not evaluate OAuth access token:"); System.err.println(exception); } } } getHandler().verifyCredentials(); }