List of usage examples for twitter4j TwitterObjectFactory getRawJSON
public static String getRawJSON(Object obj)
From source file:org.apache.streams.twitter.processor.FetchAndReplaceTwitterProcessor.java
License:Apache License
protected String fetch(Activity doc) throws TwitterException { String id = doc.getObject().getId(); LOGGER.debug("Fetching status from Twitter for {}", id); Long tweetId = Long.valueOf(id.replace("id:twitter:tweets:", "")); Status status = getTwitterClient().showStatus(tweetId); return TwitterObjectFactory.getRawJSON(status); }
From source file:org.apache.streams.twitter.provider.TwitterFollowingProviderTask.java
License:Apache License
private void collectUsers(Long id) { int keepTrying = 0; List<twitter4j.User> list = null; long curser = -1; twitter4j.User user;//from www. j av a2s . c om String userJson; try { user = client.users().showUser(id); userJson = TwitterObjectFactory.getRawJSON(user); } catch (TwitterException ex) { LOGGER.error("Failure looking up " + id); return; } do { try { PagableResponseList<twitter4j.User> page = null; if (provider.getConfig().getEndpoint().equals("followers")) { page = client.friendsFollowers().getFollowersList(id, curser, provider.getConfig().getPageSize().intValue()); } else if (provider.getConfig().getEndpoint().equals("friends")) { page = client.friendsFollowers().getFriendsList(id, curser, provider.getConfig().getPageSize().intValue()); } Objects.requireNonNull(list); Preconditions.checkArgument(list.size() > 0); for (twitter4j.User other : list) { String otherJson = TwitterObjectFactory.getRawJSON(other); try { Follow follow = null; if (provider.getConfig().getEndpoint().equals("followers")) { follow = new Follow().withFollowee(mapper.readValue(userJson, User.class)) .withFollower(mapper.readValue(otherJson, User.class)); } else if (provider.getConfig().getEndpoint().equals("friends")) { follow = new Follow().withFollowee(mapper.readValue(otherJson, User.class)) .withFollower(mapper.readValue(userJson, User.class)); } Objects.requireNonNull(follow); if (item_count < provider.getConfig().getMaxItems()) { ComponentUtils.offerUntilSuccess(new StreamsDatum(follow), provider.providerQueue); item_count++; } } catch (Exception ex) { LOGGER.warn("Exception: {}", ex); } } if (!page.hasNext()) { break; } if (page.getNextCursor() == 0) { break; } curser = page.getNextCursor(); page_count++; } catch (Exception ex) { keepTrying += TwitterErrorHandler.handleTwitterError(client, null, ex); } } while (provider.shouldContinuePulling(list) && curser != 0 && keepTrying < provider.getConfig().getRetryMax() && count < provider.getConfig().getMaxItems()); }
From source file:org.apache.streams.twitter.provider.TwitterFollowingProviderTask.java
License:Apache License
private void collectIds(Long id) { int keepTrying = 0; long curser = -1; twitter4j.User user;// w w w .ja v a 2 s .c o m String userJson; try { user = client.users().showUser(id); userJson = TwitterObjectFactory.getRawJSON(user); } catch (TwitterException ex) { LOGGER.error("Failure looking up " + id); return; } do { try { twitter4j.IDs ids = null; if (provider.getConfig().getEndpoint().equals("followers")) { ids = client.friendsFollowers().getFollowersIDs(id, curser, provider.getConfig().getMaxItems().intValue()); } else if (provider.getConfig().getEndpoint().equals("friends")) { ids = client.friendsFollowers().getFriendsIDs(id, curser, provider.getConfig().getMaxItems().intValue()); } Objects.requireNonNull(ids); Preconditions.checkArgument(ids.getIDs().length > 0); for (long otherId : ids.getIDs()) { try { Follow follow = null; if (provider.getConfig().getEndpoint().equals("followers")) { follow = new Follow().withFollowee(new User().withId(id)) .withFollower(new User().withId(otherId)); } else if (provider.getConfig().getEndpoint().equals("friends")) { follow = new Follow().withFollowee(new User().withId(otherId)) .withFollower(new User().withId(id)); } Objects.requireNonNull(follow); if (item_count < provider.getConfig().getMaxItems()) { ComponentUtils.offerUntilSuccess(new StreamsDatum(follow), provider.providerQueue); item_count++; } } catch (Exception ex) { LOGGER.warn("Exception: {}", ex); } } if (!ids.hasNext()) { break; } if (ids.getNextCursor() == 0) { break; } curser = ids.getNextCursor(); page_count++; } catch (TwitterException twitterException) { keepTrying += TwitterErrorHandler.handleTwitterError(client, id, twitterException); } catch (Exception ex) { keepTrying += TwitterErrorHandler.handleTwitterError(client, null, ex); } } while (shouldContinuePulling() && curser != 0 && keepTrying < provider.getConfig().getRetryMax()); }
From source file:org.apache.streams.twitter.provider.TwitterFriendsProviderTask.java
License:Apache License
protected void getFriends(Long id) { int keepTrying = 0; long curser = -1; do {/*from w ww . j a v a 2 s . c o m*/ try { twitter4j.User follower4j; String followerJson; try { follower4j = client.users().showUser(id); followerJson = TwitterObjectFactory.getRawJSON(follower4j); } catch (TwitterException e) { LOGGER.error("Failure looking up " + id); break; } PagableResponseList<User> followeeList = client.friendsFollowers().getFriendsList(id.longValue(), curser); for (twitter4j.User followee4j : followeeList) { String followeeJson = TwitterObjectFactory.getRawJSON(followee4j); try { Follow follow = new Follow() .withFollowee( mapper.readValue(followeeJson, org.apache.streams.twitter.pojo.User.class)) .withFollower( mapper.readValue(followerJson, org.apache.streams.twitter.pojo.User.class)); ComponentUtils.offerUntilSuccess(new StreamsDatum(follow), provider.providerQueue); } catch (JsonParseException e) { LOGGER.warn(e.getMessage()); } catch (JsonMappingException e) { LOGGER.warn(e.getMessage()); } catch (IOException e) { LOGGER.warn(e.getMessage()); } } curser = followeeList.getNextCursor(); } catch (TwitterException twitterException) { keepTrying += TwitterErrorHandler.handleTwitterError(client, twitterException); } catch (Exception e) { keepTrying += TwitterErrorHandler.handleTwitterError(client, e); } } while (curser != 0 && keepTrying < 10); }
From source file:org.apache.streams.twitter.provider.TwitterFriendsProviderTask.java
License:Apache License
protected void getFriends(String screenName) { int keepTrying = 0; long curser = -1; do {/*www .ja v a 2s . c o m*/ try { twitter4j.User follower4j; String followerJson; try { follower4j = client.users().showUser(screenName); followerJson = TwitterObjectFactory.getRawJSON(follower4j); } catch (TwitterException e) { LOGGER.error("Failure looking up " + screenName); break; } PagableResponseList<User> followeeList = client.friendsFollowers().getFriendsList(screenName, curser); for (twitter4j.User followee4j : followeeList) { String followeeJson = TwitterObjectFactory.getRawJSON(followee4j); try { Follow follow = new Follow() .withFollowee( mapper.readValue(followeeJson, org.apache.streams.twitter.pojo.User.class)) .withFollower( mapper.readValue(followerJson, org.apache.streams.twitter.pojo.User.class)); ComponentUtils.offerUntilSuccess(new StreamsDatum(follow), provider.providerQueue); } catch (JsonParseException e) { LOGGER.warn(e.getMessage()); } catch (JsonMappingException e) { LOGGER.warn(e.getMessage()); } catch (IOException e) { LOGGER.warn(e.getMessage()); } } curser = followeeList.getNextCursor(); } catch (TwitterException twitterException) { keepTrying += TwitterErrorHandler.handleTwitterError(client, twitterException); } catch (Exception e) { keepTrying += TwitterErrorHandler.handleTwitterError(client, e); } } while (curser != 0 && keepTrying < 10); }
From source file:org.apache.streams.twitter.provider.TwitterTimelineProviderTask.java
License:Apache License
@Override public void run() { Paging paging = new Paging(1, 200); List<Status> statuses = null; do {//from w w w . ja v a 2 s. c o m int keepTrying = 0; // keep trying to load, give it 5 attempts. //This value was chosen because it seemed like a reasonable number of times //to retry capturing a timeline given the sorts of errors that could potentially //occur (network timeout/interruption, faulty client, etc.) while (keepTrying < 5) { try { this.client = provider.getTwitterClient(); statuses = client.getUserTimeline(id, paging); for (Status tStat : statuses) { String json = TwitterObjectFactory.getRawJSON(tStat); provider.addDatum(new StreamsDatum(json)); } paging.setPage(paging.getPage() + 1); keepTrying = 10; } catch (TwitterException twitterException) { keepTrying += TwitterErrorHandler.handleTwitterError(client, twitterException); } catch (Exception e) { keepTrying += TwitterErrorHandler.handleTwitterError(client, e); } } } while (provider.shouldContinuePulling(statuses)); LOGGER.info(id + " Thread Finished"); }
From source file:org.graylog2.inputs.twitter.TwitterTransport.java
License:Open Source License
@Override public void launch(final MessageInput input) throws MisfireException { final ConfigurationBuilder cb = new ConfigurationBuilder() .setOAuthConsumerKey(configuration.getString(CK_OAUTH_CONSUMER_KEY)) .setOAuthConsumerSecret(configuration.getString(CK_OAUTH_CONSUMER_SECRET)) .setOAuthAccessToken(configuration.getString(CK_OAUTH_ACCESS_TOKEN)) .setOAuthAccessTokenSecret(configuration.getString(CK_OAUTH_ACCESS_TOKEN_SECRET)) .setJSONStoreEnabled(true);//from ww w. jav a2 s.c om final StatusListener listener = new StatusListener() { public void onStatus(final Status status) { try { input.processRawMessage(createMessageFromStatus(status)); } catch (IOException e) { LOG.debug("Error while processing tweet status", e); } } private RawMessage createMessageFromStatus(final Status status) throws IOException { return new RawMessage(TwitterObjectFactory.getRawJSON(status).getBytes(StandardCharsets.UTF_8)); } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onException(final Exception ex) { LOG.error("Error while reading Twitter stream", ex); } @Override public void onScrubGeo(long lon, long lat) { } @Override public void onStallWarning(StallWarning stallWarning) { LOG.info("Stall warning: {} ({}% full)", stallWarning.getMessage(), stallWarning.getPercentFull()); } }; final String[] track = Iterables.toArray( Splitter.on(',').omitEmptyStrings().trimResults().split(configuration.getString(CK_KEYWORDS)), String.class); final FilterQuery filterQuery = new FilterQuery(); filterQuery.track(track); if (twitterStream == null) { twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); } twitterStream.addListener(listener); twitterStream.filter(filterQuery); }
From source file:org.loklak.harvester.TwitterAPI.java
License:Open Source License
public static JSONObject user2json(User user) throws IOException { String jsonstring = TwitterObjectFactory.getRawJSON(user); JSONObject json = new JSONObject(jsonstring); json.put("retrieval_date", AbstractObjectEntry.utcFormatter.print(System.currentTimeMillis())); Object status = json.remove("status"); // we don't need to store the latest status update in the user dump // TODO: store the latest status in our message database return json;/*from ww w.j a v a2 s. co m*/ }
From source file:org.primeoservices.cfgateway.twitter.TwitterUserStream.java
License:Apache License
@Override @SuppressWarnings("unchecked") public void onStatus(final Status status) { final Struct data = RailoUtils.createStruct(); data.put("format", this.argType.toString()); if (ArgumentType.JSON.equals(this.argType)) { data.put("status", TwitterObjectFactory.getRawJSON(status)); } else {/*from www. j a v a 2s. c o m*/ data.put("status", status); } this.invokeListener("onStatus", data); }
From source file:org.primeoservices.cfgateway.twitter.TwitterUserStream.java
License:Apache License
@Override @SuppressWarnings("unchecked") public void onDirectMessage(final DirectMessage directMessage) { final Struct data = RailoUtils.createStruct(); data.put("format", this.argType.toString()); if (ArgumentType.JSON.equals(this.argType)) { data.put("directMessage", TwitterObjectFactory.getRawJSON(directMessage)); } else {/*from www.j a va 2s .c o m*/ data.put("directMessage", directMessage); } this.invokeListener("onDirectMessage", data); }