use of org.springframework.social.twitter.api.Tweet in project Gemma by PavlidisLab.
the class TwitterOutboundImpl method sendManualTweet.
@Override
@Secured({ "GROUP_ADMIN" })
public void sendManualTweet(String feed) {
TwitterOutboundImpl.log.debug("Checking if Twitter is enabled");
if (!Settings.getBoolean("gemma.twitter.enabled")) {
TwitterOutboundImpl.log.info("Twitter is disabled.");
return;
}
if (StringUtils.isNotBlank(feed)) {
TwitterOutboundImpl.log.info("Sending out tweet: '" + feed + "'");
String consumerKey = Settings.getString("twitter.consumer-key");
String consumerSecret = Settings.getString("twitter.consumer-secret");
String accessToken = Settings.getString("twitter.access-token");
String accessTokenSecret = Settings.getString("twitter.access-token-secret");
Twitter twitter = new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
StatusDetails metadata = new StatusDetails();
metadata.setWrapLinks(true);
try {
Tweet tweet = twitter.timelineOperations().updateStatus(feed, metadata);
TwitterOutboundImpl.log.info("tweet info:" + tweet.toString());
} catch (Exception e) {
TwitterOutboundImpl.log.info(e.toString());
e.printStackTrace();
}
}
}
use of org.springframework.social.twitter.api.Tweet in project spring-integration by spring-projects.
the class TwitterSearchOutboundGateway method handleRequestMessage.
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
Object args;
if (this.searchArgsExpression != null) {
args = this.searchArgsExpression.getValue(this.evaluationContext, requestMessage);
} else {
args = requestMessage.getPayload();
}
Assert.notNull(args, "The twitter search expression cannot evaluate to 'null'.");
SearchParameters searchParameters;
if (args instanceof SearchParameters) {
searchParameters = (SearchParameters) args;
} else if (args instanceof String) {
searchParameters = new SearchParameters((String) args).count(DEFAULT_PAGE_SIZE);
} else if (args instanceof List) {
List<?> list = (List<?>) args;
Assert.isTrue(list.size() > 0 && list.size() < 5, "Between 1 and 4 search arguments are required");
Assert.isInstanceOf(String.class, list.get(0), "The first search argument (query) must be a String");
searchParameters = new SearchParameters((String) list.get(0));
if (list.size() > 1) {
Assert.isInstanceOf(Number.class, list.get(1), "The second search argument (pageSize) must be a Number");
searchParameters.count(((Number) list.get(1)).intValue());
if (list.size() > 2) {
Assert.isInstanceOf(Number.class, list.get(2), "The third search argument (sinceId) must be a Number");
searchParameters.sinceId(((Number) list.get(2)).longValue());
}
if (list.size() > 3) {
Assert.isInstanceOf(Number.class, list.get(3), "The fourth search argument (maxId) must be a Number");
searchParameters.maxId(((Number) list.get(3)).longValue());
}
}
} else {
throw new IllegalArgumentException("Search Expression must evaluate to a 'SearchParameters', 'String' or 'List'.");
}
SearchResults results = this.getTwitter().searchOperations().search(searchParameters);
if (results != null) {
List<Tweet> tweets = (results.getTweets() != null ? results.getTweets() : Collections.<Tweet>emptyList());
return this.getMessageBuilderFactory().withPayload(tweets).setHeader(TwitterHeaders.SEARCH_METADATA, results.getSearchMetadata());
} else {
return null;
}
}
use of org.springframework.social.twitter.api.Tweet in project spring-integration by spring-projects.
the class SearchReceivingMessageSourceWithRedisTests method testPollForTweetsThreeResultsWithRedisMetadataStore.
@Test
@RedisAvailable
public void testPollForTweetsThreeResultsWithRedisMetadataStore() throws Exception {
String metadataKey = TestUtils.getPropertyValue(twitterSearchAdapter, "source.metadataKey", String.class);
// There is need to set a value, not 'remove' and re-init 'twitterMessageSource'
this.metadataStore.put(metadataKey, "-1");
this.twitterMessageSource.afterPropertiesSet();
MetadataStore metadataStore = TestUtils.getPropertyValue(this.twitterSearchAdapter, "source.metadataStore", MetadataStore.class);
assertTrue("Expected metadataStore to be an instance of RedisMetadataStore", metadataStore instanceof RedisMetadataStore);
assertSame(this.metadataStore, metadataStore);
assertEquals("twitterSearchAdapter.74", metadataKey);
this.twitterSearchAdapter.start();
Message<?> receive = this.tweets.receive(10000);
assertNotNull(receive);
receive = this.tweets.receive(10000);
assertNotNull(receive);
receive = this.tweets.receive(10000);
assertNotNull(receive);
/* We received 3 messages so far. When invoking receive() again the search
* will return again the 3 test Tweets but as we already processed them
* no message (null) is returned. */
assertNull(this.tweets.receive(0));
String persistedMetadataStoreValue = this.metadataStore.get(metadataKey);
assertNotNull(persistedMetadataStoreValue);
assertEquals("3", persistedMetadataStoreValue);
this.twitterSearchAdapter.stop();
this.metadataStore.put(metadataKey, "1");
this.twitterMessageSource.afterPropertiesSet();
this.twitterSearchAdapter.start();
receive = this.tweets.receive(10000);
assertNotNull(receive);
assertThat(receive.getPayload(), instanceOf(Tweet.class));
assertEquals(((Tweet) receive.getPayload()).getId(), 2L);
receive = this.tweets.receive(10000);
assertNotNull(receive);
assertThat(receive.getPayload(), instanceOf(Tweet.class));
assertEquals(((Tweet) receive.getPayload()).getId(), 3L);
assertNull(this.tweets.receive(0));
persistedMetadataStoreValue = this.metadataStore.get(metadataKey);
assertNotNull(persistedMetadataStoreValue);
assertEquals("3", persistedMetadataStoreValue);
}
use of org.springframework.social.twitter.api.Tweet in project spring-integration by spring-projects.
the class TwitterSearchOutboundGatewayTests method testStringQueryCustomExpression.
@Test
public void testStringQueryCustomExpression() {
this.gateway.setSearchArgsExpression(new SpelExpressionParser().parseExpression("{'bar', 1, 2, 3}"));
Tweet tweet = mock(Tweet.class);
SearchMetadata searchMetadata = mock(SearchMetadata.class);
final SearchResults searchResults = new SearchResults(Collections.singletonList(tweet), searchMetadata);
doAnswer(invocation -> {
SearchParameters searchParameters = invocation.getArgument(0);
assertEquals("bar", searchParameters.getQuery());
assertEquals(Integer.valueOf(1), searchParameters.getCount());
assertEquals(Long.valueOf(2), searchParameters.getSinceId());
assertEquals(Long.valueOf(3), searchParameters.getMaxId());
return searchResults;
}).when(this.searchOps).search(any(SearchParameters.class));
this.gateway.handleMessage(new GenericMessage<String>("foo"));
Message<?> reply = this.outputChannel.receive(0);
assertNotNull(reply);
@SuppressWarnings("unchecked") List<Tweet> tweets = (List<Tweet>) reply.getPayload();
assertEquals(1, tweets.size());
assertSame(tweet, tweets.get(0));
assertSame(searchMetadata, reply.getHeaders().get(TwitterHeaders.SEARCH_METADATA));
}
use of org.springframework.social.twitter.api.Tweet in project Gemma by PavlidisLab.
the class TwitterOutboundImpl method sendDailyFeed.
@Override
@Secured({ "GROUP_AGENT" })
public void sendDailyFeed() {
TwitterOutboundImpl.log.debug("Checking if Twitter is enabled");
if (!TwitterOutboundImpl.enabled.get()) {
return;
}
String feed = this.generateDailyFeed();
TwitterOutboundImpl.log.info("Twitter is enabled. Checking if Twitter feed is empty.");
if (StringUtils.isNotBlank(feed)) {
TwitterOutboundImpl.log.info("Sending out tweet: '" + feed + "'");
String consumerKey = Settings.getString("twitter.consumer-key");
String consumerSecret = Settings.getString("twitter.consumer-secret");
String accessToken = Settings.getString("twitter.access-token");
String accessTokenSecret = Settings.getString("twitter.access-token-secret");
Twitter twitter = new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
StatusDetails metadata = new StatusDetails();
metadata.setWrapLinks(true);
try {
Tweet tweet = twitter.timelineOperations().updateStatus(feed, metadata);
TwitterOutboundImpl.log.info("tweet info:" + tweet.toString());
} catch (Exception e) {
TwitterOutboundImpl.log.info(e.toString());
}
}
}
Aggregations