Last active
December 16, 2015 05:39
-
-
Save manute/5386359 to your computer and use it in GitHub Desktop.
Twitter4j Search with locale in Grails Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.social | |
| import twitter4j.* | |
| import groovy.util.logging.Slf4j | |
| import org.springframework.context.i18n.LocaleContextHolder as LCH | |
| @Slf4j | |
| class TwitterSearch { | |
| private final static MAX_TWEETS_PER_PAGE = 10 | |
| private twitter | |
| def query | |
| TwitterSearch(){ | |
| twitter = new TwitterFactory().getInstance() | |
| } | |
| def tweetsInLocale(){ | |
| def locale = LCH.getLocale() as String | |
| withCatchTwitterException { | |
| Query q = new Query("$query lang:$locale ") | |
| q.setRpp(MAX_TWEETS_PER_PAGE) | |
| QueryResult result = twitter.search(q) | |
| return result.getTweets() | |
| } | |
| } | |
| private withCatchTwitterException(closure){ | |
| try{ | |
| closure() | |
| }catch (TwitterException te) { | |
| log.error "Failed to search tweets" , te | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment