/
grittr.groovy
executable file
·550 lines (468 loc) · 15 KB
/
grittr.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import SQLite.Database;
import SQLite.Stmt;
import twitter4j.*;
import twitter4j.http.*;
public class GrittR {
public static String VERSION = "GrittR 1.0";
private static Twitter twitter;
private static ExtendedUser user;
private static AccessToken accessToken;
private static String method;
private static String username;
private static String password;
private static String[] arguments;
private static String argStr;
/**************************************************************************
twitter "wrapper" methods
**************************************************************************/
public static void getDirectMessages() {
def messagesList;
try {
messagesList = twitter.getDirectMessages();
println AinsiColors.MAJENTA_ON_DEFAULT +
"\nDirect messages \n" +
AinsiColors.NORMAL;
messagesList.each {
println "${GrittR.humanDate(it.getCreatedAt())} " +
"${it.getSender().getScreenName()} \n${it.getText()} \n";
}
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to get your direct messages ...sorry" +
AinsiColors.NORMAL;
}
finally { System.exit(0); }
}
public static void getSentDirectMessages() {
def messagesList;
try {
println AinsiColors.MAJENTA_ON_DEFAULT +
"\nSent messages \n" +
AinsiColors.NORMAL;
messagesList = twitter.getSentDirectMessages();
messagesList.each {
println "${GrittR.humanDate(it.getCreatedAt())} " +
"${ it.getSender().getScreenName() } \n${it.getText()} \n";
}
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to get your sent messages ...sorry" +
AinsiColors.NORMAL;
}
finally { System.exit(0); }
}
public static void getFavorites() {
def statusList;
try {
statusList = twitter.getFavorites();
println AinsiColors.MAJENTA_ON_DEFAULT +
"\nFavorite tweets \n" +
AinsiColors.NORMAL;
displayStatusMessage(statusList);
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to get your favorite tweets ...sorry" +
AinsiColors.NORMAL;
}
finally { System.exit(0); }
}
public static void getFollowers() {
def userList;
try {
println AinsiColors.MAJENTA_ON_DEFAULT +
"\nFollowers \n" +
AinsiColors.NORMAL;
userList = twitter.getFollowers(twitter.getUserId());
userList.each {
println "${ it.getScreenName() }";
}
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to list your followers ... sorry" +
AinsiColors.NORMAL;
}
finally { System.exit(0); }
}
public static void getFriendsTimeline() {
def statusList;
try {
statusList = twitter.getFriendsTimeline();
println AinsiColors.MAJENTA_ON_DEFAULT +
"\nFriends timeline \n" +
AinsiColors.NORMAL;
displayStatusMessage(statusList);
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to list your friend's timeline ... sorry" +
AinsiColors.NORMAL;
}
finally { System.exit(0); }
}
public static getMentions() {
def statusList;
try {
statusList = twitter.getMentions();
println AinsiColors.MAJENTA_ON_DEFAULT +
"\n@${twitter.getUserId()} \n" +
AinsiColors.NORMAL;
displayStatusMessage(statusList);
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to list your mentions timeline ... sorry" +
AinsiColors.NORMAL;
}
finally { System.exit(0); }
}
static void getPublicTimeline() {
def statusList;
try {
println AinsiColors.MAJENTA_ON_DEFAULT +
"\nPublic timeline \n" +
AinsiColors.NORMAL;
statusList = twitter.getPublicTimeline();
displayStatusMessage(statusList);
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to list public timeline ... sorry" +
AinsiColors.NORMAL;
ex.printStackTrace();
}
finally { System.exit(0); }
}
public static void getUserTimeline() {
def statusList;
try {
println AinsiColors.MAJENTA_ON_DEFAULT +
"\nYour timeline \n" +
AinsiColors.NORMAL;
statusList = twitter.getUserTimeline(twitter.getUserId(), new Date());
displayStatusMessage(statusList);
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to list your user timeline ... sorry" +
AinsiColors.NORMAL;
}
finally { System.exit(0); }
}
static void updateStatus(String newStatus) {
if(newStatus.length() >= 139)
newStatus = newStatus.substring(0, 139);
def status;
try {
status = twitter.updateStatus(newStatus);
println AinsiColors.CYAN_ON_DEFAULT +
"\nSuccessfully updated your status to : ${status.getText()}" +
" - [ID: ${status.getId()}]" +
AinsiColors.NORMAL;
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to update your status... sorry" +
AinsiColors.NORMAL;
}
finally { System.exit(0); }
}
/**************************************************************************/
public static void authenticateUser(String username, String password) {
if(username == null || password == null) {
println AinsiColors.RED_ON_DEFAULT +
"You must supply your USERNAME and " +
"PASSWORD for the method : ${method}" +
AinsiColors.NORMAL;
System.exit(1);
}
println "Logging in as ${username}";
try {
twitter = new Twitter(username, password);
user = twitter.verifyCredentials();
println "Logged in as ${user.getScreenName()}";
try {
AccessToken at = loadAccessToken(user.getId());
if(at != null)
twitter.setOAuthAccessToken(at);
customizeClient(twitter);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
catch(Exception ex) {
println AinsiColors.RED_ON_DEFAULT +
"Unable to verify your twitter credentials ...sorry" +
AinsiColors.NORMAL;
System.exit(1);
}
}
// TODO : implement oauth support...
public static void authorizeClient() {
twitter.setOAuthConsumer("JDy3FSsqzDpBYS9Xtvp1AA", "AftY58IPpEctiiIQpFaDuDuGKJHDbmnZGiyqrA12E");
RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (null == accessToken) {
println "Open the following URL and grant access to your account:";
println requestToken.getAuthorizationURL();
println "Hit enter when it's done.[Enter]:";
br.readLine();
try {
accessToken = requestToken.getAccessToken();
}
catch (TwitterException te) {
if( 401 == te.getStatusCode()) {
println "Unable to get the access token." +
AinsiColors.NORMAL;
}
else {
te.printStackTrace();
}
}
}
AccessToken at = loadAccessToken(user.getId());
if(at == null) {
// save accessToken for future reference.
storeAccessToken(twitter.verifyCredentials().getId() , accessToken);
}
else {
// twitter.setOAuthAccessToken(at.getToken(), at.getTokenSecret());
// accessToken = loadAccessToken(user.getId());
twitter.setOAuthAccessToken(at);
println "GrittR is authorized...";
}
}
public static void storeAccessToken(int userId, AccessToken at) {
def db, sb, stmt;
try {
db = new Database();
db.open("grittr.db", 0755);
sb = new StringBuffer();
sb.append("create table if not exists tokens (");
sb.append("id integer primary key autoincrement, ");
sb.append("userid varchar(255) not null, ");
sb.append("token varchar(255) not null, ");
sb.append("tokenSecret varchar(255) not null)");
try {
stmt = db.prepare(sb.toString());
while(stmt.step()) { }
}
catch(Exception ex) {
println "Error creating tokens table ...sorry" +
AinsiColors.NORMAL;
}
sb = new StringBuffer();
sb.append("insert into tokens values (");
sb.append("NULL, ");
sb.append("'" + userId.toString() + "'");
sb.append(", ");
sb.append("'" + at.getToken() + "'");
sb.append(", ");
sb.append("'" + at.getTokenSecret() + "'");
sb.append(")");
try {
stmt = db.prepare(sb.toString());
while(stmt.step()) { }
}
catch(Exception ex) {
println "Error storing your access token ...sorry" +
AinsiColors.NORMAL;
}
}
catch(Exception ex) {
println "Unable to store access token ...sorry" +
AinsiColors.NORMAL;
}
}
public static AccessToken loadAccessToken(int userId) {
def db, sb, stmt;
AccessToken accessToken = null;
String token, tokenSecret = null;
try {
db = new Database();
db.open("grittr.db", 0755);
sb = new StringBuffer();
sb.append("select * from tokens where userid = ");
sb.append("'" + userId.toString() + "'");
try {
stmt = db.prepare(sb.toString());
while(stmt.step()) {
stmt.column_count().times {
if(stmt.column_origin_name(it).equalsIgnoreCase("token"))
token = stmt.column_string(it);
if(stmt.column_origin_name(it).equalsIgnoreCase("tokenSecret"))
tokenSecret = stmt.column_string(it);
}
}
accessToken = new AccessToken(token, tokenSecret);
}
catch(Exception ex) {
println "Error while retreiving your access token ...sorry" +
AinsiColors.NORMAL;
}
}
catch(Exception ex) {
println "Unable to retreive access token ...sorry" +
AinsiColors.NORMAL;
}
return accessToken;
}
/**************************************************************************/
public static void displayStatusMessage(List<Status> statusList) {
statusList.each {
println "${AinsiColors.CYAN_ON_DEFAULT}" +
"${GrittR.humanDate(it.getCreatedAt())} " +
"${AinsiColors.GREEN_ON_DEFAULT} " +
"${it.getUser().getScreenName()} " +
"${AinsiColors.MAJENTA_ON_DEFAULT} " +
"[ID: ${it.getId()}]" +
"${AinsiColors.BLUE_ON_DEFAULT} " +
"\n${it.getText()}\n" +
"${AinsiColors.NORMAL} ";
}
}
public static String helpString() {
StringBuffer sb = new StringBuffer();
sb.append(AinsiColors.GREEN_ON_DEFAULT);
sb.append("\n");
sb.append(GrittR.VERSION);
sb.append("\n");
sb.append("groovy grittr method [(opt) arguments]\n\n");
sb.append("Example usage :\n");
sb.append("groovy grittr about \n");
sb.append("groovy grittr public \n");
sb.append("groovy grittr direct USERNAME PASSWORD \n");
sb.append("groovy grittr favorites USERNAME PASSWORD \n");
sb.append("groovy grittr followers USERNAME PASSWORD \n");
sb.append("groovy grittr friends USERNAME PASSWORD \n");
sb.append("groovy grittr mentions USERNAME PASSWORD \n");
sb.append("groovy grittr sent USERNAME PASSWORD \n");
sb.append("groovy grittr update USERNAME PASSWORD 'your new status' \n");
sb.append("groovy grittr user USERNAME PASSWORD \n");
sb.append(AinsiColors.NORMAL);
return sb.toString();
}
public static String parseMethod(String method) {
// some methods need "authentication", others dont
switch(method) {
case "about":
printAbout();
break;
case "public":
getPublicTimeline();
break;
case "authorize":
authenticateUser(username, password);
authorizeClient();
break;
case "direct":
authenticateUser(username, password);
getDirectMessages();
break;
case "favorites":
authenticateUser(username, password);
getFavorites();
break;
case "followers":
authenticateUser(username, password);
getFollowers();
break;
case "friends":
authenticateUser(username, password);
getFriendsTimeline();
break;
case "mentions":
authenticateUser(username, password);
getMentions();
break;
case "sent":
authenticateUser(username, password);
getSentDirectMessages();
break;
case "update":
authenticateUser(username, password);
updateStatus(argStr);
break;
case "user":
authenticateUser(username, password);
getUserTimeline();
break;
default:
println GrittR.helpString();
break;
}
}
public static String humanDate(date) {
return new SimpleDateFormat("yy/MM/dd").format(date);
}
public static void customizeClient(Twitter twitter) {
twitter.setClientURL("http://github.com/julien/grittr/tree/master");
twitter.setClientVersion("1.0");
twitter.setSource("grittr");
twitter.setUserAgent("grittr");
}
public static void printAbout() {
println AinsiColors.GREEN_ON_DEFAULT +
"url : ${twitter.getClientURL()}\n" +
"version : ${twitter.getClientVersion()}\n" +
"source : ${twitter.getSource()}\n" +
"user agent : ${twitter.getUserAgent()}" +
AinsiColors.NORMAL;
}
/**************************************************************************/
public static void main(String[] args) {
if(args.length > 0) {
method = args[0];
if (args.length > 1)
username = args[1];
else
username = "";
if(args.length > 2)
password = args[2];
else
password = null;
if(args.length > 3) {
arguments = new String[args.length - 3];
System.arraycopy(args, 3, arguments, 0, args.length - 3);
argStr = arguments.join(" ");
}
twitter = new Twitter();
customizeClient(twitter);
parseMethod(method);
// println "${AinsiColors.GREEN_ON_DEFAULT}" +
// "method : ${method}, args : ${argStr}${AinsiColors.NORMAL}";
}
else {
println GrittR.helpString();
System.exit(1);
}
// TODO : implement oauth support...
// accessToken = loadAccessToken(user.getId());
// if(accessToken != null)
// twitter.setOAuthAccessToken(accessToken);
}
/**************************************************************************/
}
/* a simple utility class to print
out "colored" messages in the console" */
class AinsiColors {
public static String NORMAL = "\u001b[0m";
public static String REVERSE = "\u001b[7m";
// colors on "default" console background
public static String BLUE_ON_DEFAULT = "\u001b[34;49m";
public static String CYAN_ON_DEFAULT = "\u001b[36;49m";
public static String GREEN_ON_DEFAULT = "\u001b[32;49m";
public static String MAJENTA_ON_DEFAULT = "\u001b[35;49m";
public static String RED_ON_DEFAULT = "\u001b[31;49m";
public static String YELLOW_ON_DEFAULT = "\u001b[36;49m";
public static String WHITE_ON_DEFAULT = "\u001b[37;49m";
}