2010-11-05 23:10:03 +00:00
|
|
|
package com.jdbernard.twitter
|
|
|
|
|
|
|
|
import twitter4j.Twitter
|
|
|
|
import twitter4j.TwitterFactory
|
|
|
|
import twitter4j.conf.Configuration
|
|
|
|
import twitter4j.conf.PropertyConfiguration
|
|
|
|
|
|
|
|
public class TwitterCLI {
|
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
private static TwitterCLI nailgunInst
|
|
|
|
private Twitter twitter
|
2010-11-05 23:10:03 +00:00
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2010-11-06 15:52:52 +00:00
|
|
|
TwitterCLI inst = new TwitterCLI(new File(System.getProperty("user.home"),
|
|
|
|
".groovy-twitter-cli-rc"))
|
2010-11-05 23:10:03 +00:00
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
inst.run(args as List)
|
|
|
|
}
|
2010-11-05 23:10:03 +00:00
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
public static void nailMain(String[] args) {
|
2010-11-05 23:10:03 +00:00
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
if (nailgunInst == null) nailgunInst = new TwitterCLI(new File(
|
|
|
|
System.getProperty("user.home"), ".groovy-twitter-cli-rc"))
|
2010-11-05 23:10:03 +00:00
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
nailgunInst.run(args as List)
|
2010-11-05 23:10:03 +00:00
|
|
|
}
|
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
public static void setColor(boolean bright, int code) {
|
2010-11-05 23:10:03 +00:00
|
|
|
print "\u001b[${bright?'1':'0'};${code}m"
|
|
|
|
}
|
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
public static void resetColor() { print "\u001b[m" }
|
2010-11-05 23:10:03 +00:00
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
public static void colorPrint(def message, boolean bright, int color) {
|
2010-11-05 23:10:03 +00:00
|
|
|
setColor(bright, color)
|
|
|
|
print message
|
|
|
|
resetColor()
|
|
|
|
}
|
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
public static void colorPrintln(def message, boolean bright, int color) {
|
2010-11-05 23:10:03 +00:00
|
|
|
setColor(bright, color)
|
|
|
|
println message
|
|
|
|
resetColor()
|
|
|
|
}
|
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
public TwitterCLI(File propFile) {
|
|
|
|
|
|
|
|
// load the configuration
|
|
|
|
Configuration conf
|
|
|
|
propFile.withInputStream { is -> conf = new PropertyConfiguration(is) }
|
|
|
|
|
|
|
|
// create a twitter instance
|
|
|
|
twitter = (new TwitterFactory(conf)).getInstance()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run(List args) {
|
|
|
|
if (args.size() < 1) printUsage()
|
|
|
|
|
|
|
|
switch (args[0].toLowerCase()) {
|
|
|
|
case ~/t.*/: timeline(args.tail()); break
|
|
|
|
case ~/p.*/: post(args.tail()); break
|
|
|
|
|
|
|
|
default:
|
|
|
|
printUsage()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void timeline(List args) {
|
2010-11-05 23:10:03 +00:00
|
|
|
|
|
|
|
// default to showing my friends timeline
|
|
|
|
if (args.size() == 0) args = ["friends"]
|
|
|
|
|
|
|
|
while (args.size() > 0) {
|
|
|
|
def option = args.pop()
|
|
|
|
switch (option) {
|
|
|
|
// friends
|
|
|
|
case ~/f.*/: printTimeline(twitter.friendsTimeline); break
|
|
|
|
// mine
|
|
|
|
case ~/m.*/: printTimeline(twitter.userTimeline); break
|
|
|
|
// user
|
|
|
|
case ~/u.*/:
|
|
|
|
if (args.size() == 0)
|
|
|
|
colorPrintln("No user specificied.", true, 31)
|
|
|
|
else printTimeline(twitter.getUserTimeline(args.pop()))
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
colorPrint("Unknown timeline: ", true, 31)
|
|
|
|
colorPrintln(option, true, 33)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printTimeline(def timeline) {
|
|
|
|
timeline.each { status ->
|
|
|
|
colorPrint(status.user.screenName, true, 34)
|
|
|
|
print ": "
|
|
|
|
println status.text
|
|
|
|
println ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-06 15:52:52 +00:00
|
|
|
public static void printUsage() {
|
2010-11-05 23:10:03 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|