This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
twitter_cleanup.bal
95 lines (90 loc) · 3.05 KB
/
twitter_cleanup.bal
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
// Clean up all the old tweets
// BE SUPER CAREFUL
// Parameters: twitter account, number of days to delete
// To start:
// ballerina run twitter_cleanup.bal --config twitter.toml
// To list all tweets from today:
// curl localhost:9090/B7aDemo/0
// To delete all tweets from today:
// curl -X DELETE localhost:9090/B7aDemo/0
import ballerina/config;
import ballerina/http;
import ballerina/io;
import ballerina/time;
import wso2/twitter;
twitter:Client twc = new ({
clientId: config:getAsString("clientId"),
clientSecret: config:getAsString("clientSecret"),
accessToken: config:getAsString("accessToken"),
accessTokenSecret: config:getAsString("accessTokenSecret"),
clientConfig: {}
});
@http:ServiceConfig {
basePath: "/"
}
service tweetCleaner on new http:Listener(9090) {
@http:ResourceConfig {
methods: ["GET"],
path: "/{account}/{days}"
}
resource function list(http:Caller caller, http:Request request, string account, int days) {
http:Response res = new;
twitter:Status[] tws = getTweets(account, days);
json[] tweets = [];
foreach var tw in tws {
json t = {
date: tw.createdAt,
id: tw.id,
text: tw.text
};
tweets[tweets.length()] = t;
io:println(tw.createdAt + ": " + tw.id.toString());
}
json out = {tweets: tweets};
res.setJsonPayload(<@untainted>out, contentType = "application/json");
var err = caller->respond(res);
if (err is error) {
io:println("Error when responding ", err);
}
}
@http:ResourceConfig {
methods: ["DELETE"],
path: "/{account}/{days}"
}
resource function delete(http:Caller caller, http:Request request, string account, int days) {
http:Response res = new;
json[] tweets = [];
twitter:Status[] tws = getTweets(account, days);
foreach var tw in tws {
json t = {
date: tw.createdAt,
id: tw.id,
text: tw.text
};
tweets[tweets.length()] = t;
var err = twc->destroyStatus(<@untainted>tw.id);
if (err is error) {
io:println("Error when destroying status ", err);
}
io:println(tw.createdAt + ": " + tw.id.toString());
}
json out = {deleted: tweets};
res.setJsonPayload(<@untainted>out, contentType = "application/json");
var err = caller->respond(res);
if (err is error) {
io:println("Error when responding ", err);
}
}
}
function getTweets(string account, int days) returns @tainted twitter:Status[] {
string searchStr = "from:" + account + " since:" + time:format(time:subtractDuration(time:currentTime(), 0, 0, days, 0, 0, 0, 0), "yyyy-MM-dd").toString();
io:println(searchStr);
var v = twc->search(searchStr, {});
twitter:Status[] out = [];
if (v is twitter:Status[]) {
out = v;
} else {
io:println("Error: ", v);
}
return out;
}