Skip to content

Instantly share code, notes, and snippets.

@manute
Last active December 16, 2015 05:39
Show Gist options
  • Select an option

  • Save manute/5386359 to your computer and use it in GitHub Desktop.

Select an option

Save manute/5386359 to your computer and use it in GitHub Desktop.
Twitter4j Search with locale in Grails Application
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