@SuppressWarnings("unchecked") @Test @Ignore public void demoReceiveSearchResults() throws Exception { PropertiesFactoryBean pf = new PropertiesFactoryBean(); pf.setLocation(new ClassPathResource("sample.properties")); pf.afterPropertiesSet(); Properties prop = pf.getObject(); TwitterTemplate template = new TwitterTemplate( prop.getProperty("z_oleg.oauth.consumerKey"), prop.getProperty("z_oleg.oauth.consumerSecret"), prop.getProperty("z_oleg.oauth.accessToken"), prop.getProperty("z_oleg.oauth.accessTokenSecret")); SearchReceivingMessageSource tSource = new SearchReceivingMessageSource(template, "foo"); tSource.setQuery(SEARCH_QUERY); tSource.afterPropertiesSet(); for (int i = 0; i < 50; i++) { Message<Tweet> message = (Message<Tweet>) tSource.receive(); if (message != null) { Tweet tweet = message.getPayload(); logger.info(tweet.getFromUser() + " - " + tweet.getText() + " - " + tweet.getCreatedAt()); } } }
@Override public void run() { List<Tweet> userTimeline; try { log.info("Getting latest tweets from public timeline and feeding them into processing grid"); // Return all the tweets from the Twitter API userTimeline = twitterTemplate.timelineOperations().getUserTimeline("BriefingcomSMU"); } catch (ApiException e) { log.log(Level.SEVERE, "Error getting tweets from public timeline from twitter", e); return; } try { // according to the API we may get duplicate tweets if invoked with frequency of lower than 60 // seconds. // We will filter tweets which are duplicates for (Tweet publicTweet : userTimeline) { if (previousTimeLineTweets.contains(publicTweet.getId())) { continue; } logTweet(publicTweet); gigaSpace.write(buildTweet(publicTweet)); } } catch (DataAccessException e) { log.log(Level.SEVERE, "error feeding tweets", e); } finally { previousTimeLineTweets.clear(); for (Tweet publicTweet : userTimeline) { previousTimeLineTweets.add(publicTweet.getId()); } } }
public SpaceDocument buildTweet(Tweet tweet) { return new SpaceDocument( "Tweet", new DocumentProperties() .setProperty("Id", tweet.getId()) .setProperty("Text", tweet.getText()) .setProperty("CreatedAt", tweet.getCreatedAt()) .setProperty("FromUserId", tweet.getFromUserId()) .setProperty("ToUserId", tweet.getToUserId()) .setProperty("Processed", Boolean.FALSE)); }
private void logTweet(Tweet tweet) { log.fine( String.format( "Tweet id=%d\tfromUser=%s\ttext=%s \n", tweet.getId(), tweet.getFromUser(), tweet.getText())); }