List of usage examples for twitter4j Twitter lookupUsers
ResponseList<User> lookupUsers(long... ids) throws TwitterException;
From source file:br.com.porcelli.hornetq.integration.twitter.stream.StreamHandler.java
License:Apache License
public StreamHandler(final String connectorName, final Map<String, Object> configuration, final StorageManager storageManager, final PostOffice postOffice) { this.connectorName = connectorName; this.mbean = new TwitterStreamManagement(this); try {/* w w w.j a v a2 s . c o m*/ MBeanServer mbServer = ManagementFactory.getPlatformMBeanServer(); ObjectName mbeanName = new ObjectName("org.hornetq:module=ConnectorService,name=" + connectorName); mbServer.registerMBean(mbean, mbeanName); } catch (Exception e) { log.error("Error on registering JMX info.", e); } final Configuration conf = new ConfigurationBuilder() .setOAuthConsumerKey(ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_CONSUMER_KEY, null, configuration)) .setOAuthConsumerSecret(ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_CONSUMER_SECRET, null, configuration)) .setOAuthAccessToken(ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_ACCESS_TOKEN, null, configuration)) .setOAuthAccessTokenSecret(ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_ACCESS_TOKEN_SECRET, null, configuration)) .build(); final String queueName = ConfigurationHelper.getStringProperty(InternalTwitterConstants.PROP_QUEUE_NAME, null, configuration); final String userScreenName = ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_SCREEN_NAME, null, configuration); final String lastTweetQueueName = ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_LAST_TWEET_QUEUE_NAME, null, configuration); final String lastDMQueueName = ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_LAST_DM_QUEUE_NAME, null, configuration); Long lastTweetId = null; if (lastTweetQueueName != null && lastTweetQueueName.trim().length() > 0) { final Binding lastTweetBinding = postOffice.getBinding(new SimpleString(lastTweetQueueName)); if (lastTweetBinding != null) { final Queue lastTweetQueue = (Queue) lastTweetBinding.getBindable(); if (lastTweetQueue.getMessageCount() > 0) { long ltweetId = Long.MIN_VALUE; for (final Iterator<MessageReference> iterator = lastTweetQueue.iterator(); iterator .hasNext();) { final MessageReference msg = iterator.next(); lastTweetId = msg.getMessage().getBodyBuffer().readLong(); if (lastTweetId > ltweetId) { ltweetId = lastTweetId; } } lastTweetId = ltweetId + 1L; } } } Long lastDMId = null; if (lastDMQueueName != null && lastDMQueueName.trim().length() > 0) { final Binding lastTweetBinding = postOffice.getBinding(new SimpleString(lastDMQueueName)); if (lastTweetBinding != null) { final Queue lastDMQueue = (Queue) lastTweetBinding.getBindable(); if (lastDMQueue.getMessageCount() > 0) { long ldmId = Long.MIN_VALUE; for (final Iterator<MessageReference> iterator = lastDMQueue.iterator(); iterator.hasNext();) { final MessageReference msg = iterator.next(); lastDMId = msg.getMessage().getBodyBuffer().readLong(); if (lastDMId > ldmId) { ldmId = lastDMId; } } lastDMId = ldmId + 1L; } } } final String[] mentionedUsers = splitProperty(ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_MENTIONED_USERS, null, configuration)); final String[] hashTags = splitProperty( ConfigurationHelper.getStringProperty(InternalTwitterConstants.PROP_HASHTAGS, null, configuration)); int[] userIds = null; int userId = -1; Twitter twitter = null; if (mentionedUsers != null) { try { twitter = new TwitterFactory(conf).getInstance(); userId = twitter.getId(); userIds = userIds(twitter.lookupUsers(mentionedUsers)); } catch (final TwitterException e) { mbean.notifyException(e); } finally { if (twitter != null) { twitter.shutdown(); } } } data = new TwitterStreamDTO(queueName, userScreenName, userId, lastTweetQueueName, lastDMQueueName, lastTweetId, lastDMId, mentionedUsers, userIds, hashTags, conf, postOffice); final String reclaimers = ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_TWEET_RECLAIMERS, null, configuration); message = new MessageQueuing(data, this.mbean, splitProperty(reclaimers)); streamListenersInsanceArgs = new Object[] { data, message, mbean }; final String listenerList = ConfigurationHelper .getStringProperty(InternalTwitterConstants.PROP_STREAM_LISTENERS, null, configuration); final String[] listeners = splitProperty(listenerList); if (listeners != null) { final UserStreamHandler userHandler = buildUserStreamHandler(listeners); final SiteStreamHandler siteHandler = buildSiteStreamHandler(listeners); final StatusStreamHandler statusHandler = buildStatusStreamHandler(listeners); if (userHandler != null || siteHandler != null || statusHandler != null) { streamHandlers = new HashSet<BaseStreamHandler>(); if (userHandler != null) { streamHandlers.add(userHandler); } if (siteHandler != null) { streamHandlers.add(siteHandler); } if (statusHandler != null) { streamHandlers.add(statusHandler); } } else { streamHandlers = null; } } else { streamHandlers = null; } }
From source file:com.avishkar.NewGetFollowersIDs.java
License:Apache License
private static List<Long> getUserFollwers(long twitterUserId) throws UnknownHostException, TwitterException, InterruptedException { Twitter twitter = new TwitterFactory(AccessTokenUtil.getConfig()).getInstance(); List<Long> filteredUserListOnFollowerCount = new ArrayList<Long>(); final Gson gson = new Gson(); try {/*w w w . ja v a 2 s . co m*/ checkRateLimit("/followers/ids", twitter); System.out.println("Listing followers's Follower for ID:" + twitterUserId + System.lineSeparator()); IDs followerIDs = twitter.getFollowersIDs(twitterUserId, -1); JsonObject followerJSON = new JsonObject(); followerJSON.addProperty("id", twitterUserId); followerJSON.addProperty("followers", gson.toJson(followerIDs.getIDs())); DBAccess.insert(gson.toJson(followerJSON)); System.out.println("Current Followers Fetched Size:" + followerIDs.getIDs().length); // Filtering for influential user if (followerIDs.getIDs().length > 1000) { System.out.println("User assumed as Influential"); return null; } int from = 0; int to = 99; int limit = checkRateLimit("/users/lookup", twitter); while (from < followerIDs.getIDs().length) { if (to > followerIDs.getIDs().length) to = followerIDs.getIDs().length - 1; if (limit == 0) checkRateLimit("/users/lookup", twitter); ResponseList<User> followers = twitter .lookupUsers(Arrays.copyOfRange(followerIDs.getIDs(), from, to)); System.out.println("Recieved User count:" + followers.size()); for (User user : followers) { DBAccess.insertUser(gson.toJson(user)); if (user.getFollowersCount() < 1000) { // if(user.getStatusesCount()>0 && // user.getStatus()!=null && // sevenDaysAgo.after(user.getStatus().getCreatedAt())) filteredUserListOnFollowerCount.add(user.getId()); getStatuses(user.getScreenName(), twitter); } else System.out.println("User " + user.getScreenName() + " is pruned for Influential or over subscription." + " Follower count:" + user.getFollowersCount() + " Friends Count:" + user.getFriendsCount()); } from += 100; to += 100; limit--; } } catch (TwitterException te) { if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED || te.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println("Encountered locked profile. Skipping " + twitterUserId); return null; // log something here } te.printStackTrace(); System.out.println("Failed to get followers' Follower: " + te.getMessage()); // System.exit(-1); } return filteredUserListOnFollowerCount; }
From source file:com.dwdesign.tweetings.loader.IDsUsersLoader.java
License:Open Source License
@Override public List<ParcelableUser> getUsers() throws TwitterException { final Twitter twitter = getTwitter(); if (twitter == null) return null; if (mIDs == null) { mIDs = getIDs();//from www . ja v a 2 s . c o m if (mIDs == null) return null; } final long[] ids = mIDs.getIDs(); final int max_id_idx = mMaxId > 0 ? ArrayUtils.indexOf(ids, mMaxId) : 0; if (max_id_idx < 0) return null; if (max_id_idx == ids.length - 1) return Collections.emptyList(); final int count = max_id_idx + mLoadItemLimit < ids.length ? mLoadItemLimit : ids.length - max_id_idx; final long[] ids_to_load = new long[count]; int temp_idx = max_id_idx; for (int i = 0; i < count; i++) { ids_to_load[i] = ids[temp_idx]; temp_idx++; } final ResponseList<User> users = twitter.lookupUsers(ids_to_load); final List<ParcelableUser> result = new ArrayList<ParcelableUser>(); for (final User user : users) { final int position = ArrayUtils.indexOf(mIDs.getIDs(), user.getId()); result.add(new ParcelableUser(user, mAccountId, position)); } return result; }
From source file:com.michaelfitzmaurice.clocktwerk.apps.FollowerCounter.java
License:Apache License
public static void main(String[] args) throws Exception { File outputDir = new File(args[0]); String[] users = new String[args.length - 1]; System.arraycopy(args, 1, users, 0, users.length); LOG.info("Setting up Twitter client..."); Twitter twitter = new TwitterFactory().getInstance(); LOG.info("Twitter client connected"); LOG.info("Getting followers for users " + Arrays.asList(users)); ResponseList<User> foundUsers = twitter.lookupUsers(users); for (User user : foundUsers) { LOG.info("\t'{}' has {} followers", user.getName(), user.getFollowersCount()); recordFollowersToCsv(outputDir, user.getScreenName(), user.getFollowersCount()); }/*w ww .j a va2s . c o m*/ }
From source file:de.vanita5.twittnuker.loader.support.IDsUsersLoader.java
License:Open Source License
@Override public List<User> getUsers(final Twitter twitter) throws TwitterException { if (twitter == null) return null; final CursorPaging paging = new CursorPaging(getCount()); if (getCursor() > 0) { paging.setCursor(getCursor());//ww w. ja v a2 s.c o m } final IDs ids = getIDs(twitter, paging); if (ids == null) return null; setCursorIds(ids); return twitter.lookupUsers(ids.getIDs()); }
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./* www.ja v a 2 s .c o m*/ * * @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; }
From source file:org.apache.streams.twitter.provider.TwitterTimelineProvider.java
License:Apache License
private Collection<Long> retrieveIds(String[] screenNames) { Twitter client = getTwitterClient(); List<Long> ids = Lists.newArrayList(); try {/*from www. j a v a 2s . co m*/ for (User tStat : client.lookupUsers(screenNames)) { ids.add(tStat.getId()); } } catch (TwitterException e) { LOGGER.error("Failure retrieving user details.", e.getMessage()); } return ids; }
From source file:org.apache.streams.twitter.provider.TwitterUserInformationProvider.java
License:Apache License
private void loadBatch(Long[] ids) { Twitter client = getTwitterClient(); int keepTrying = 0; // keep trying to load, give it 5 attempts. //while (keepTrying < 10) while (keepTrying < 1) { try {//from ww w .j a va 2 s .co m long[] toQuery = new long[ids.length]; for (int i = 0; i < ids.length; i++) toQuery[i] = ids[i]; for (User tStat : client.lookupUsers(toQuery)) { String json = DataObjectFactory.getRawJSON(tStat); ComponentUtils.offerUntilSuccess(new StreamsDatum(json), providerQueue); } keepTrying = 10; } catch (TwitterException twitterException) { keepTrying += TwitterErrorHandler.handleTwitterError(client, twitterException); } catch (Exception e) { keepTrying += TwitterErrorHandler.handleTwitterError(client, e); } } }
From source file:org.apache.streams.twitter.provider.TwitterUserInformationProvider.java
License:Apache License
private void loadBatch(String[] ids) { Twitter client = getTwitterClient(); int keepTrying = 0; // keep trying to load, give it 5 attempts. //while (keepTrying < 10) while (keepTrying < 1) { try {/*from ww w. jav a 2 s . co m*/ for (User tStat : client.lookupUsers(ids)) { String json = DataObjectFactory.getRawJSON(tStat); providerQueue.offer(new StreamsDatum(json)); } keepTrying = 10; } catch (TwitterException twitterException) { keepTrying += TwitterErrorHandler.handleTwitterError(client, twitterException); } catch (Exception e) { keepTrying += TwitterErrorHandler.handleTwitterError(client, e); } } }
From source file:org.loklak.harvester.TwitterAPI.java
License:Open Source License
/** * search for twitter user names by a given set of user id's * @param id_strs/* w w w. ja va 2 s .com*/ * @param lookupLocalUsersByUserId if this is true and successful, the resulting names may contain users without user info in the user dump * @return * @throws IOException * @throws TwitterException */ public static Map<String, Number> getScreenName(Set<Number> id_strs, final int maxFollowers, boolean lookupLocalUsersByUserId) throws IOException, TwitterException { // we have several sources to get this mapping: // - 1st / fastest: mapping from DAO.twitter_user_dump // - 2nd / fast : mapping from DAO.searchLocalUserByUserId(user_id) // - 3rd / slow : from twitter API with twitter.lookupUsers(String[] user_id) // first we check all fast solutions until trying the twitter api Map<String, Number> r = new HashMap<>(); Set<Number> id4api = new HashSet<>(); for (Number id_str : id_strs) { if (r.size() >= maxFollowers) break; JsonFactory mapcapsule = DAO.user_dump.get("id_str", id_str.toString()); if (mapcapsule != null) { JSONObject map = mapcapsule.getJSON(); String screen_name = map.has("screen_name") ? (String) map.get("screen_name") : null; if (screen_name != null) { r.put(screen_name, id_str); continue; } } if (lookupLocalUsersByUserId) { UserEntry ue = DAO.searchLocalUserByUserId(id_str.toString()); if (ue != null) { String screen_name = ue.getScreenName(); if (screen_name != null) { r.put(screen_name, id_str); continue; } } } id4api.add(id_str); } while (id4api.size() > 100 && id4api.size() + r.size() > maxFollowers) id4api.remove(id4api.iterator().next()); // resolve the remaining user_ids from the twitter api if (r.size() < maxFollowers && id4api.size() > 0) { TwitterFactory tf = getAppTwitterFactory(); if (tf == null) return new HashMap<>(); Twitter twitter = tf.getInstance(); collect: while (id4api.size() > 0) { // construct a query term with at most 100 id's int chunksize = Math.min(100, id4api.size()); long[] u = new long[chunksize]; Iterator<Number> ni = id4api.iterator(); for (int i = 0; i < chunksize; i++) { u[i] = ni.next().longValue(); } try { ResponseList<User> users = twitter.lookupUsers(u); for (User usr : users) { JSONObject map = user2json(usr); enrichLocation(map); DAO.user_dump.putUnique(map); r.put(usr.getScreenName(), usr.getId()); id4api.remove(usr.getId()); } } catch (TwitterException e) { if (r.size() == 0) throw e; break collect; } } } return r; }