Search in sources :

Example 1 with Tweet

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();
        }
    }
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) Tweet(org.springframework.social.twitter.api.Tweet) StatusDetails(org.springframework.social.twitter.api.StatusDetails) Twitter(org.springframework.social.twitter.api.Twitter) Secured(org.springframework.security.access.annotation.Secured)

Example 2 with Tweet

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;
    }
}
Also used : SearchParameters(org.springframework.social.twitter.api.SearchParameters) Tweet(org.springframework.social.twitter.api.Tweet) List(java.util.List) SearchResults(org.springframework.social.twitter.api.SearchResults)

Example 3 with Tweet

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);
}
Also used : MetadataStore(org.springframework.integration.metadata.MetadataStore) RedisMetadataStore(org.springframework.integration.redis.metadata.RedisMetadataStore) Tweet(org.springframework.social.twitter.api.Tweet) RedisMetadataStore(org.springframework.integration.redis.metadata.RedisMetadataStore) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 4 with Tweet

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));
}
Also used : SearchParameters(org.springframework.social.twitter.api.SearchParameters) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Tweet(org.springframework.social.twitter.api.Tweet) ArrayList(java.util.ArrayList) List(java.util.List) SearchMetadata(org.springframework.social.twitter.api.SearchMetadata) SearchResults(org.springframework.social.twitter.api.SearchResults) Test(org.junit.Test)

Example 5 with Tweet

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());
        }
    }
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) Tweet(org.springframework.social.twitter.api.Tweet) StatusDetails(org.springframework.social.twitter.api.StatusDetails) Twitter(org.springframework.social.twitter.api.Twitter) Secured(org.springframework.security.access.annotation.Secured)

Aggregations

Tweet (org.springframework.social.twitter.api.Tweet)16 Test (org.junit.Test)11 SearchParameters (org.springframework.social.twitter.api.SearchParameters)8 SearchResults (org.springframework.social.twitter.api.SearchResults)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 SearchMetadata (org.springframework.social.twitter.api.SearchMetadata)7 TwitterTemplate (org.springframework.social.twitter.api.impl.TwitterTemplate)6 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)3 Properties (java.util.Properties)2 Ignore (org.junit.Ignore)2 PropertiesFactoryBean (org.springframework.beans.factory.config.PropertiesFactoryBean)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Message (org.springframework.messaging.Message)2 Secured (org.springframework.security.access.annotation.Secured)2 SearchOperations (org.springframework.social.twitter.api.SearchOperations)2 StatusDetails (org.springframework.social.twitter.api.StatusDetails)2 Twitter (org.springframework.social.twitter.api.Twitter)2 MessageHistory (org.springframework.integration.history.MessageHistory)1 MetadataStore (org.springframework.integration.metadata.MetadataStore)1