-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweetExtractor.java
177 lines (146 loc) · 6.52 KB
/
TweetExtractor.java
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
package api.twitter;
import controller.FileController;
import controller.TwitterSmoothingController;
import db.MongoCRUD;
import twitter4j.*;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class TweetExtractor {
// TODO cambia le apikey
private String apikey = "";
private String apiSecret = "";
private AccessToken accessToken;
private Twitter twitter;
private TwitterStream twitterStream;
public TweetExtractor(){
this.accessToken = null;
this.twitter = TwitterFactory.getSingleton();
this.twitterStream = new TwitterStreamFactory().getInstance();
}
public void auth() throws TwitterException{
twitter.setOAuthConsumer(this.apikey, this.apiSecret);
RequestToken requestToken = twitter.getOAuthRequestToken();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (null == accessToken) {
System.out.println("Open the following URL and grant access to your account:");
System.out.println(requestToken.getAuthorizationURL());
System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
String pin = null;
try {
pin = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
try{
if(pin.length() > 0){
this.accessToken = twitter.getOAuthAccessToken(requestToken, pin);
}else{
this.accessToken = twitter.getOAuthAccessToken();
}
} catch (TwitterException te) {
if(401 == te.getStatusCode()){
System.out.println("Unable to get the access token.");
}else{
te.printStackTrace();
}
}
}
storeAccessToken(accessToken);
Status status = twitter.updateStatus("200 OK");
System.out.println("Successfully updated the status to [" + status.getText() + "].");
}
private static void storeAccessToken(AccessToken accessToken){
FileController fileController = new FileController();
fileController.saveTokens(accessToken.getToken(),accessToken.getTokenSecret());
}
public List<String> searchTweet(String searchQuery) throws TwitterException {
Query query = new Query(searchQuery);
QueryResult result = this.twitter.search(query);
List<String> tweets = new ArrayList<>();
for (Status state : result.getTweets()) {
System.out.println("@" + state.getUser().getScreenName() + ":> " + state.getText());
if (state.getURLEntities().length > 0)
tweets.add(state.getUser().getScreenName() + ":> " + state.getText());
}
return tweets;
}
public void lister() throws TwitterException, IOException {
MongoCRUD mongoCRUD = new MongoCRUD();
mongoCRUD.setDbName("twitterDB");
mongoCRUD.setCollection("en");
StatusListener listener = new StatusListener() {
@Override
public void onStatus(Status status) {
if ( status.getText().charAt(0) != 'R' & status.getText().charAt(1) != 'T' & status.getText().charAt(3) != '@' &
status.getText().split(" ").length >= 10 & status.getText().contains("#")) {
mongoCRUD.saveTweet("@" + status.getUser().getScreenName(), status.getText());
System.out.print(".");
if (status.getText().length() == 280)
System.out.print("\n");
}
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
@Override
public void onStallWarning(StallWarning warning) {
System.out.println("Got stall warning:" + warning);
}
@Override
public void onException(Exception ex) {
ex.printStackTrace();
}
};
this.twitterStream.addListener(listener);
this.twitterStream.sample("en");
}
public void listenSmooth() throws TwitterException, IOException {
MongoCRUD mongoCRUD = new MongoCRUD();
mongoCRUD.setDbName("twitterDB");
mongoCRUD.setCollection("smooth");
TwitterSmoothingController tsc = new TwitterSmoothingController();
StatusListener listener = new StatusListener() {
@Override
public void onStatus(Status status) {
if (!status.getText().substring(0,4).equals("RT @")) {
if (tsc.check(status.getText())) {
mongoCRUD.saveTweet("@" + status.getUser().getScreenName(), status.getText());
tsc.saveHistory(status.getText());
}
}
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
@Override
public void onStallWarning(StallWarning warning) { System.out.println("Got stall warning:" + warning); }
@Override
public void onException(Exception ex) { ex.printStackTrace(); }
};
this.twitterStream.addListener(listener);
this.twitterStream.sample("en");
}
}