-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwiitter_client_UnitTest.java
More file actions
181 lines (168 loc) · 5.7 KB
/
twiitter_client_UnitTest.java
File metadata and controls
181 lines (168 loc) · 5.7 KB
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
//https://twittercommunity.com/t/pagination-in-search-1-1/10228
//https://dev.twitter.com/rest/tools/console
//https://dev.twitter.com/rest/public/search
//https://dev.twitter.com/rest/reference/get/search/tweets
//http://stackoverflow.com/questions/23341215/extracting-tweets-of-a-specific-hashtag-using-twitter4j
package twitter_Augur;
import twitter4j.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class twitter_client extends Client_Builder {
public static final String TEXT_DATA = "T";
public static final String NUMERIC_DATA = "N";
public static final String TWITTER = "T";
public static final String CRITIC = "C";
public static final String AUDIENCE = "A";
public static final String FILENAME = "Twitter.txt";
public QueryResult getReview(String movie_name,long max_id) throws TwitterException {
Query query = new Query( movie_name + "-filter:retweets -filter:links -filter:replies -filter:images");
query.setCount(100);
query.setLang("en");
if (max_id != -1)
query.setMaxId(max_id);
//query.
QueryResult result = twitter.search(query);
return result;
/*for (Status status : result.getTweets()) {
System.out.println(status.getText());
System.out.println("# " + status.getId());
}
int size = result.getTweets().size();
Status last_status = result.getTweets().get(size-1);
System.out.println(last_status.getId());
System.out.println(result.getMaxId());
System.out.println(result.getSinceId());*/
}
public List<String> extractTweets(QueryResult result)
{
List <String> temp = new LinkedList<String>();
for (Status status : result.getTweets())
{
temp.add(status.getText());
}
return temp;
}
public List<String> getAllReviews(String movie_name) throws TwitterException, InterruptedException
{
QueryResult initial = getReview(movie_name,-1);
List<String> reviews = new LinkedList<String>();
int size = initial.getTweets().size();
System.out.println("initial size "+size);
Status last_status;
if (size > 0)
{
last_status = initial.getTweets().get(size-1);
long last_id = last_status.getId();
long max_id = last_id;
int count = 1;
reviews.addAll(extractTweets(initial));
while (size > 99 && count < 5)
{
Thread.sleep(1000);//sleep necessary so that requests/sec limit is not violated.
initial = getReview(movie_name,max_id);
size = initial.getTweets().size();
if (size > 0) {
last_status = initial.getTweets().get(size - 1);
last_id = last_status.getId();
max_id = last_id;
reviews.addAll(extractTweets(initial));
System.out.println("count: " + count
+ " comments retrieved: " + reviews.size());
}
count ++;
}
System.out.println("Total calls made for " + movie_name +" " + count);
}
return reviews;
}
public String generateOutputFile(String movie_name) throws Exception
{
String Filename = movie_name;
File inputFile = new File(Filename);
FileWriter fileWriter;
List <String> reviews = getAllReviews( movie_name);
if (reviews.isEmpty() != true) {
String commentMeta = movie_name + "\t" + TEXT_DATA + TWITTER
+ AUDIENCE + " ";
try {
inputFile.createNewFile();
fileWriter = new FileWriter(inputFile);
BufferedWriter bw = new BufferedWriter(fileWriter);
for (String review : reviews) {
String data = commentMeta + review;
bw.write(data);
bw.newLine();
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
return Filename;
}
else
return "blank_file";
}
public static void main(String[] args) throws Exception
{
twitter_client t = new twitter_client();
String path = args[0];
String bucketname = args[1];
String folder_name = args[2];
String CurrentLine;
String filename;
S3Utility s3client = new S3Utility(bucketname,folder_name);
String[] parts = {"1","2"};
List <String> movielist = new LinkedList<String>();
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
while ((CurrentLine = br.readLine()) != null) {
parts = CurrentLine.split("\t");
movielist.add(parts[0]);
}
System.out.println(movielist);
for (int i = 0;i<movielist.size();i++)
{
Thread.sleep(26000);//sleep for 26 seconds, so that API limit is not violated.
filename = t.generateOutputFile(movielist.get(i));
if (filename.equalsIgnoreCase("blank_file") != true)
{
System.out.println("initiating upload for "+movielist.get(i));
s3client.uploadFile(filename);
}
else
{
System.out.println("no tweets found for "+movielist.get(i));
}
}
/*if (args.length < 3)
{
System.out.println("Usage:javac -c jar <bucket-name> <folder> <movie1> <movie2>...<movie-n>");
throw new UnsupportedOperationException();
}
twitter_client t = new twitter_client();
String bucket_name = args[0];
S3Utility s3client = new S3Utility(bucket_name,args[1]);
int arg_count = 2;
String filename;
List<String> movies = new ArrayList<String>();
while (arg_count < args.length)
{
String temp_movie = args[arg_count].replace("_", " ");//for anirrudha's code.
movies.add(temp_movie);
arg_count++;
}
//System.out.println((movies.toString()));
for (int i = 0;i < movies.size();i++)
{
Thread.sleep(60000);//sleep for 60 seconds, so that API limit is not violated.
filename = t.generateOutputFile(movies.get(i));
if (filename.equalsIgnoreCase("blank_file") != true)
s3client.uploadFile(filename);
}*/
}
}