-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooksLookUp.java
207 lines (180 loc) · 4.86 KB
/
BooksLookUp.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
package bookslookup;
import java.io.*;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.net.URLConnection;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import static java.util.Comparator.*;
import static java.util.stream.Collectors.*;
class BooksLookUp
{
public static void main(String[] args) throws IOException
{
try
{
List<String> isbns = readIsbn();
sequentialRun(isbns);
concurrentRun(isbns);
}
catch (IOException e)
{
e.getMessage();
}
catch (Exception e)
{
e.getMessage();
}
}
public static void concurrentRun(List<String> isbnList) throws Exception
{
List<Book> listOfBooks = new ArrayList<>();
ExecutorService executor = Executors.newFixedThreadPool(15);
long startTime = System.nanoTime();
for(String isbn: isbnList)
{
Runnable thread = new Runnable()
{
@Override
public void run()
{
try
{
listOfBooks.add(findDetailsForABook(isbn));
}
catch(Exception e)
{
try
{
throw new Exception();
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
};
executor.submit(thread);
}
Runnable secThread = new Runnable()
{
@Override
public void run()
{
try
{
executor.awaitTermination(5, TimeUnit.SECONDS);
long endTime = System.nanoTime();
List<Book> sortedBookList = sortBookList(listOfBooks);
displayBookDetails(sortedBookList);
System.out.println("Concurrent Execution Time: " + (endTime - startTime)/1000000000 + " Seconds\n\n");
}
catch (InterruptedException e)
{
try
{
throw new InterruptedException();
}
catch (Exception e1)
{
}
}
}
};
executor.submit(secThread);
}
public static void sequentialRun(List<String> isbnList) throws Exception
{
try
{
long startTime = System.nanoTime();
List<Book> listOfBooks = findDetailsForAllBooks(isbnList);
long endTime = System.nanoTime();
List<Book> sortedBookList = sortBookList(listOfBooks);
displayBookDetails(sortedBookList);
System.out.println("Sequential Execution Time: " + (endTime - startTime)/1000000000 + " Seconds\n\n");
}
catch (Exception e)
{
throw new Exception();
}
}
public static List<String> readIsbn() throws IOException
{
return Files.lines(Paths.get(System.getProperty("user.dir") + "/bookslookup/ISBNPool.txt"))
.collect(toList());
}
public static List<Book> findDetailsForAllBooks(List<String> isbnList)
{
return isbnList.stream()
.map(BooksLookUp::findDetailsForABook)
.collect(toList());
}
public static Book findDetailsForABook(String isbn)
{
try
{
BufferedReader inputStream;
URL baseUrl;
baseUrl = new URL("https://www.amazon.com/exec/obidos/ASIN/");
URL currentUrl = new URL(baseUrl, isbn);
URLConnection connection = (currentUrl).openConnection();
connection.setRequestProperty("Content-Type", "text/html");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
inputStream = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String title = "";
String rank = "";
String currentLine = "";
while((currentLine = inputStream.readLine()) != null)
{
if(currentLine.contains("<meta name=\"title\" content="))
title = extractTitle(currentLine);
if(currentLine.contains("in Books"))
rank = extractRank(currentLine);
}
if(rank == "")
rank = "0";
return new Book(title, isbn, rank);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
public static String extractTitle(String currentLine)
{
String title = "";
String [] titleLine = currentLine.split("content");
String [] newTitleLine = titleLine[1].substring(2).split(":");
title = newTitleLine[0];
return title;
}
public static String extractRank(String currentLine)
{
String rank = "";
String[] rankLine = currentLine.split(" ");
rank = rankLine[0].substring(1);
return rank;
}
public static List<Book> sortBookList(List<Book> bookList)
{
return bookList.stream()
.sorted(comparing(Book::getRank))
.collect(toList());
}
private static void displayBookDetails(List<Book> bookList)
{
for(Book book : bookList)
System.out.println(book);
}
}