-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotten_final.java
645 lines (580 loc) · 20.5 KB
/
rotten_final.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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import java.util.*;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class RottenTomatoes
{
//stores urls for different calls.
HashMap <String,String> API_methods = new HashMap <String,String>();
//base api for evry call
String base_api = "http://api.rottentomatoes.com";
//api key
String key = "tggs2dy337qu9jw28h8wdsg3";
public static final String TEXT_DATA = "T";
public static final String NUMERIC_DATA = "N";
public static final String ROTTEN = "R";
public static final String CRITIC = "C";
public static final String AUDIENCE = "A";
public static final String FILENAME = "RottenTomatoes.txt";
public RottenTomatoes()
{
//initializes the hash table with urls.
this.API_methods.put("Movie Reviews","/api/public/v1.0/movies/:id/reviews.json");
this.API_methods.put("MovieSearch","/api/public/v1.0/movies.json");
this.API_methods.put("inTheatres","/api/public/v1.0/lists/movies/in_theaters.json");
this.API_methods.put("getCast","/api/public/v1.0/movies/");
}
public class Review
{
//used for mapping json object to a a java class.
public String critic;
public String date;
public String freshness;
public String publication;
//quote is same as review string.
public String quote;
//returns quote
public String get_quote()
{
return quote;
}
//returns date
public String get_date()
{
return date;
}
}
public class AbridgedCast
{
public String id;
public String name;
public List<String> characters;
}
public class Ratings
{
public String critics_rating;
public int critics_score;
public String audience_rating;
public int audience_score;
}
public class Images
{
String thumbnail;
String profile;
String detailed;
String original;
}
public class Movie {
public String id;
public String title;
public List<String> genres;
public String year;
public String mpaaRating;
public List<AbridgedCast> abridgedCast;
public Ratings ratings;
public String runtime;
public String criticsConsensus;
public String synopsis;
public Images posters;
public String getID() {
return this.id;
}
}
public List<Movie> get_inTheatre_movies() throws Exception
{
List<Movie> movie_list = new LinkedList<Movie>();
String final_uri = base_api + API_methods.get("inTheatres");
final_uri = final_uri + "?limit=16&country=us&apikey=" + key;
System.out.println(final_uri);
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
GenericUrl url = new GenericUrl(final_uri);
HttpRequest request = requestFactory.buildGetRequest(url);
String response = request.execute().parseAsString();
JsonParser parser = new JsonParser();
JsonObject jsonResponse = parser.parse(response).getAsJsonObject();
JsonArray movies = jsonResponse.get("movies").getAsJsonArray();
Gson gson = new Gson();
JsonObject movie;
Movie movie_element;
for (int i = 0;i<movies.size();i++)
{
movie = movies.get(i).getAsJsonObject();
movie_element = gson.fromJson(movie, Movie.class);
movie_list.add(movie_element);
}
return movie_list;
}
public List<String> get_cast(String movie_name) throws Exception
{
try {
Movie movie = get_movie_info(movie_name);
String movie_id = movie.id;
List<AbridgedCast> result = new ArrayList<AbridgedCast>();
List<String> actors = new ArrayList<String>();
String final_uri = base_api + API_methods.get("getCast");
final_uri = final_uri+ movie_id + "/cast.json?apikey=" + key;
System.out.println("movie cast uri: "+ final_uri);
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
GenericUrl url = new GenericUrl(final_uri);
HttpRequest request = requestFactory.buildGetRequest(url);
Thread.sleep(1000);
String response = request.execute().parseAsString();
JsonParser parser = new JsonParser();
JsonObject jsonResponse = parser.parse(response).getAsJsonObject();
Gson gson = new Gson();
JsonArray castArray = jsonResponse.getAsJsonArray("cast");
for (int i = 0; i < castArray.size(); i++) {
JsonObject castJson = castArray.get(i).getAsJsonObject();
AbridgedCast cast = gson.fromJson(castJson, AbridgedCast.class);
result.add(cast);
}
//System.out.println(":::Cast:::");
for (int i = 0; i < result.size(); i++)
{
//System.out.println(result.get(i).name);
actors.add(movie_name+"\t"+result.get(i).name);
}
return actors;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("failes to retrive cast for "+movie_name);
return null;
}
}
/*method to get movieID from movies.
returns Movie object.*/
public Movie get_movie_info(String movie_name) throws Exception
{
//create the final url by appending key,page limit and page number.
String final_uri = base_api + API_methods.get("MovieSearch");
//Hardcoding Anirrudha's key to prevent violation of query limits.
final_uri = final_uri + "?q=" + movie_name + "&page_limit=1&page=1&apikey=" + "2d85u2btkfxrt45yrzkpjxe2";
//System.out.println(final_uri);
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
GenericUrl url = new GenericUrl(final_uri);
HttpRequest request = requestFactory.buildGetRequest(url);
Thread.sleep(1000);
String response = request.execute().parseAsString();
JsonParser parser = new JsonParser();
JsonObject jsonResponse = parser.parse(response).getAsJsonObject();
JsonArray movies = jsonResponse.get("movies").getAsJsonArray();
Gson gson = new Gson();
JsonObject movie = new JsonObject();
try {
movie = movies.get(0).getAsJsonObject();
} catch (Exception e) {
System.out.println("data for movie " + movie_name + " does not exists.");
e.printStackTrace();
return null;
}
Movie movie_element = gson.fromJson(movie, Movie.class);
return movie_element;
}
//container class for getReviewsFromPage
public class ReviewPackage
{
List <String> results = new LinkedList<String>();
int total;
}
/*Returns all the reviews from a single page wrapped in a class
* which also contains total number of reviews to be used to
* make subsequent calls to get all reviews. */
public ReviewPackage getReviewsFromPage(String movie_id,int page_limit,int page,String category) throws Exception
{
String final_uri = base_api + API_methods.get("Movie Reviews");
final_uri = final_uri.replaceAll(":id", movie_id);
final_uri = final_uri+"?review_type=" + category +"&page_limit=" + String.valueOf(page_limit)
+ "&page=" + String.valueOf(page) + "&country=us&apikey=" + key;
System.out.println(final_uri);
ReviewPackage pack = new ReviewPackage();
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
GenericUrl url = new GenericUrl(final_uri);
HttpRequest request = requestFactory.buildGetRequest(url);
String response = request.execute().parseAsString();
JsonParser parser = new JsonParser();
JsonObject jsonResponse = parser.parse(response).getAsJsonObject();
pack.total = jsonResponse.get("total").getAsInt();
JsonArray review_array = jsonResponse.getAsJsonArray("reviews");
Gson gson = new Gson();
for (int i = 0; i < review_array.size(); i++)
{
JsonObject review = review_array.get(i).getAsJsonObject();
Review review_element = gson.fromJson(review, Review.class);
pack.results.add(review_element.get_quote());
}
return pack;
//number of calls to API to get all reviews, page has maximum limit of 50.
}
/*given movie name returns a list of all the reviews of the movie*/
public List<String> getAllReviews(String MovieName,String choice) throws Exception
{
//gets the ID for movie.
if (get_movie_info(MovieName) == null)
{
return null;
}
else
{
String movie_id = get_movie_info(MovieName).id;
String category;
if (choice.compareTo("critic") == 0)
{
category = "top_critic";
}
else
{
category = "all";
}
//after getting ID calls getReviewFromPage iteratively till all pages are covered.
ReviewPackage init = getReviewsFromPage(movie_id,50,1,category);
int total = init.total;
int number_ofCallsRequired = (int)Math.ceil((total/50));
ReviewPackage temp;
for (int i = 1;i<=number_ofCallsRequired;i++)
{
Thread.sleep(3000);
temp = getReviewsFromPage(movie_id,50,1+i,category);
init.results.addAll(temp.results);
}
System.out.println(init.results.size());
return init.results;
}
}
/*returns a class containing urls for the psoters*/
public Ratings get_movie_rating(String movie_name) throws Exception
{
if (get_movie_info(movie_name) == null)
{
return null;
}
else
{
Ratings rating = get_movie_info(movie_name).ratings;
return rating;
}
}
/*given movie name and destination file path retrieves the poster for
* the movie and stores it in the given location.*/
public void get_movie_posters(String movie_name,String destination_file) throws Exception
{
if (get_movie_info(movie_name) == null)
{
return;
}
try {
Images posters = get_movie_info(movie_name).posters;
String poster_url = posters.detailed.replaceAll("_tmb", "_det");
//String destinationFile = "image.jpg";
URL url = new URL(poster_url);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destination_file);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("unable to retrive poster for movie: "+movie_name);
e.printStackTrace();
}
}
public String generateOutputFile(String movie_name) throws Exception
{
String Filename = movie_name;
File inputFile = new File(Filename);
FileWriter fileWriter;
//if it fails the below test will return from here and not go further
//thus no need to put rating retrieval in a if block.
if (getAllReviews( movie_name,"critic") == null)
{
return null;
}
List <String> critic_reviews = getAllReviews( movie_name,"critic");
List <String> all_reviews = getAllReviews( movie_name,"all");
Ratings rating = get_movie_rating(movie_name);
String Audience_Score = String.valueOf(rating.audience_score);
String Critic_Score = String.valueOf(rating.critics_score);
String commentCriticMeta = movie_name + "\t" + TEXT_DATA +
ROTTEN + CRITIC + " ";
String commentAudienceMeta = movie_name + "\t" + TEXT_DATA +
ROTTEN + AUDIENCE + " ";
String ScoreAudienceMeta = movie_name + "\t" + NUMERIC_DATA +
ROTTEN + AUDIENCE + " ";
String ScoreCriticMeta = movie_name + "\t" + NUMERIC_DATA
+ ROTTEN + CRITIC + " ";
try {
inputFile.createNewFile();
fileWriter = new FileWriter(inputFile);
BufferedWriter bw = new BufferedWriter(fileWriter);
for (String review : critic_reviews) {
//replace new line with space
review = review.replaceAll("\n", " ").replaceAll("\r", " ");
String data = commentCriticMeta + review;
bw.write(data);
bw.newLine();
}
for (String review : all_reviews) {
//replace newline with space
review = review.replaceAll("\n", " ").replaceAll("\r", " ");
String data = commentAudienceMeta + review;
bw.write(data);
bw.newLine();
}
bw.write(ScoreAudienceMeta + Audience_Score);
bw.newLine();
bw.write(ScoreCriticMeta + Critic_Score);
bw.newLine();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
return Filename;
}
public String generateCastFile(String movie_name) throws Exception
{
String orig_mname = movie_name;
if (movie_name.contains(":"))
movie_name = movie_name.replace(":", " ");
String Filename = "RTcast_"+movie_name + "_cast.txt";
File inputFile = new File(Filename);
FileWriter fileWriter;
//if it fails the below test will return from here and not go further
//thus no need to put rating retrieval in a if block.
List<String> cast = get_cast(orig_mname);
if (cast == null)
{
return null;
}
try {
inputFile.createNewFile();
fileWriter = new FileWriter(inputFile);
BufferedWriter bw = new BufferedWriter(fileWriter);
for (String actor : cast)
{
bw.write(actor);
bw.newLine();
}
bw.newLine();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
return Filename;
}
/*contains example calls which were used to unit test functionality
* can also be used as reference to make calls from external modules.*/
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
RottenTomatoes RT = new RottenTomatoes();
String option = args[0];//can be movie_data/poster request/cast_request.
String bucketname = args[1];
String folder_name = args[2];
S3Utility s3client = new S3Utility(bucketname,folder_name);
List <String> movies = new LinkedList<String>();
String CurrentLine;
String filename;
//movie names start from arg[3] onwards.
int arg_count = 3;
while (arg_count < args.length)
{
String temp_movie = args[arg_count].replace("_", " ");//for anirrudha's code.
movies.add(temp_movie);
arg_count++;
}
if (option.equalsIgnoreCase("-info") )
{
for (int i = 0;i < movies.size();i++)
{
try {
Thread.sleep(10000);//sleep for 10 seconds, so that API limit is not violated.
filename = RT.generateOutputFile(movies.get(i));
if (!filename.equals(null))
s3client.uploadFile(filename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else if (option.equalsIgnoreCase("-poster"))
{
for (int i = 0;i < movies.size();i++)
{
try {
System.out.println("downloading poster of "+movies.get(i));
Thread.sleep(10000);//sleep for 10 seconds, so that API limit is not violated.
RT.get_movie_posters(movies.get(i),movies.get(i)+ ".jpg");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else if (option.equalsIgnoreCase("-cast"))
{
for (int i = 0;i < movies.size();i++)
{
try {
Thread.sleep(10000);//sleep for 10 seconds, so that API limit is not violated.
filename = RT.generateCastFile(movies.get(i));
if (!filename.equals(null))
s3client.uploadFile(filename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//this part collects information about movie cast.
RottenTomatoes RT = new RottenTomatoes();
String path = args[0];
String bucketname = args[1];
String folder_name = args[2];
String option = args[3];//can be for movie_data/cast/posters.
S3Utility s3client = new S3Utility(bucketname,folder_name);
String CurrentLine;
String filename;
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]);
}
if (option.equalsIgnoreCase("-cast")) {
//System.out.println(movielist);
for (int i = 0; i < movielist.size(); i++) {
Thread.sleep(10000);//sleep for 10 seconds, so that API limit is not violated.
RT.generateCastFile(movielist.get(i));
System.out.println("Done retrieving cast for::: "
+ movielist.get(i));
}
}
//"movies2014.txt" "augurbucket" "movies"
/*RottenTomatoes RT = new RottenTomatoes();
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(10000);//sleep for 10 seconds, so that API limit is not violated.
filename = RT.generateOutputFile(movielist.get(i));
if (filename==null)
continue;
s3client.uploadFile(filename);
}
if (args.length < 4)
throw new UnsupportedOperationException();
String option = args[0];//if option = "-c" upload a file with score and reviews
// if option = "-p" retrieve movie posters and save in a folder.
RottenTomatoes RT = new RottenTomatoes();
String bucket_name = args[1];
String folder_name = args[2];
String option = args[3];
S3Utility s3client = new S3Utility(bucket_name,folder_name);
int arg_count = 4;
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()));
if (option.equalsIgnoreCase("-c") )
{
for (int i = 0;i < movies.size();i++)
{
Thread.sleep(10000);//sleep for 10 seconds, so that API limit is not violated.
filename = RT.generateOutputFile(movies.get(i));
s3client.uploadFile(filename);
}
}
else if (option.equalsIgnoreCase("-p"))
{
for (int i = 0;i < movies.size();i++)
{
System.out.println("downloading poster of "+movies.get(i));
Thread.sleep(10000);//sleep for 10 seconds, so that API limit is not violated.
RT.get_movie_posters(movies.get(i),movies.get(i)+ ".jpg");
}
}
//RT.generateOutputFile("gone girl");
RT.get_movie_posters("gone girl","image.jpg");
Ratings rating = RT.get_movie_rating("gone girl");
System.out.println("audience score " + String.valueOf(rating.audience_score));
System.out.println("critics score " + String.valueOf(rating.critics_score));
List <Movie> inTheatre_list;
inTheatre_list = RT.get_inTheatre_movies();
for (int i = 0;i<inTheatre_list.size();i++)
{
System.out.println(inTheatre_list.get(i).title);
}
String review;
List <String> reviews = new LinkedList<String>();
reviews = RT.getAllReviews("dolphin tale 2","all");
for (int i = 0;i<reviews.size();i++)
{
review = reviews.get(i);
System.out.println(reviews.get(i));
}
NLP analyser = new NLP();
int temp_sentiment;
String review;
//RT.getID("gone girl");
List <String> reviews = new LinkedList<String>();
reviews = RT.getAllReviews("dolphin tale 2");
for (int i = 0;i<reviews.size();i++)
{
review = reviews.get(i);
temp_sentiment = analyser.analyse(review);
System.out.println(reviews.get(i) + " ::: "+ String.valueOf(temp_sentiment));
}
}
*/