1 | import com.fasterxml.jackson.databind.JsonNode;
|
---|
2 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Iterator;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.concurrent.Callable;
|
---|
10 | import java.util.concurrent.ConcurrentLinkedQueue;
|
---|
11 | import java.util.concurrent.CountDownLatch;
|
---|
12 |
|
---|
13 | public class Scraper implements Callable<Void> {
|
---|
14 |
|
---|
15 | private List<String> urls;
|
---|
16 | private ConcurrentLinkedQueue<Option> optionsQueue;
|
---|
17 | private CountDownLatch latch;
|
---|
18 |
|
---|
19 | public Scraper() {
|
---|
20 | urls = new ArrayList<>();
|
---|
21 | this.optionsQueue = new ConcurrentLinkedQueue<>();
|
---|
22 | ObjectMapper mapper = new ObjectMapper();
|
---|
23 | try {
|
---|
24 | ClassLoader classLoader = getClass().getClassLoader();
|
---|
25 | JsonNode root = mapper.readTree(new File(classLoader.getResource("URLsJSON.json").getFile()));
|
---|
26 |
|
---|
27 | JsonNode urlNode = root.get("agencyurls");
|
---|
28 | if (urlNode.isArray()) {
|
---|
29 | Iterator<JsonNode> elements = urlNode.elements();
|
---|
30 | while (elements.hasNext()) {
|
---|
31 | JsonNode next = elements.next();
|
---|
32 | urls.add(next.asText());
|
---|
33 | }
|
---|
34 | }
|
---|
35 | System.out.println("Loaded " + urls.size() + " urls");
|
---|
36 | } catch (IOException e) {
|
---|
37 | throw new RuntimeException(e);
|
---|
38 | }
|
---|
39 | this.latch = new CountDownLatch(urls.size());
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | public Void call() {
|
---|
44 | System.out.println("Scraper has started ");
|
---|
45 | for (String url : urls) {
|
---|
46 | new ScraperThread(url, optionsQueue, latch).start();
|
---|
47 | }
|
---|
48 | return null;
|
---|
49 | }
|
---|
50 | }
|
---|