Created
July 15, 2011 07:27
-
-
Save nobeans/1084245 to your computer and use it in GitHub Desktop.
lottery by groovy and twitter4j
This file contains 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
@GrabConfig(systemClassLoader=true) | |
@Grab('org.twitter4j:twitter4j-core:[2.1,)') | |
import twitter4j.* | |
if (!args) { | |
System.err.println "usage: groovy lottery.groovy <KEYWORDS>..." | |
System.exit 1 | |
} | |
// 強制除外する人を列挙する | |
def blacklist = [ | |
"nobeans", | |
] | |
def dump = {} | |
def query = new Query(args.join(" ")) | |
def debugMode = false | |
if (args.toList().contains("--debug")) { | |
debugMode = true | |
dump = { println("[${it.createdAt}] ${it.fromUser}: ${it.text}") } | |
query = new Query((args - "--debug").join(" ")) | |
} | |
println "Query: ${query.query}" | |
Twitter twitter = new TwitterFactory().instance | |
def tweets = twitter. | |
search(query).tweets. | |
findAll { !(it.fromUser in blacklist) }. | |
findAll { !(it.text =~ /RT @/) }. | |
each(dump) // for --debug | |
if (debugMode) { | |
println "Total tweets count: ${tweets.size()}" | |
println "All entried users: ${tweets*.fromUser.unique().sort()}" | |
} | |
// じらす | |
//3.times { print "."; sleep 1000 } | |
//println "" | |
int width = 55 | |
println ">"*width | |
if (tweets) { | |
// 同じ人のツイートが複数回あったら、どれを選ぶかを | |
// ランダムにするためにシャッフルする | |
3.times { Collections.shuffle(tweets) } | |
// 1人1ツイートに絞る | |
tweets = tweets.unique{ it.fromUser } | |
// 最終決戦。先頭になった人の勝ち | |
3.times { Collections.shuffle(tweets) } | |
tweets.head().with { | |
println it.fromUser.center(width) | |
println "" | |
println it.text | |
} | |
} else { | |
println "No one exists".center(width) | |
} | |
println "<"*width | |
println "Powered by Twitter4J".padLeft(width) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment