[d4d8f61] | 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;
|
---|
[c164f8f] | 9 | import java.util.concurrent.Callable;
|
---|
[d4d8f61] | 10 | import java.util.concurrent.ConcurrentLinkedQueue;
|
---|
| 11 | import java.util.concurrent.CountDownLatch;
|
---|
| 12 |
|
---|
[c164f8f] | 13 | public class Scraper implements Callable<Void> {
|
---|
| 14 |
|
---|
[d4d8f61] | 15 | private List<String> urls;
|
---|
| 16 | private ConcurrentLinkedQueue<Option> optionsQueue;
|
---|
| 17 | private CountDownLatch latch;
|
---|
| 18 |
|
---|
[c164f8f] | 19 | public Scraper() {
|
---|
[d4d8f61] | 20 | urls = new ArrayList<>();
|
---|
| 21 | this.optionsQueue = new ConcurrentLinkedQueue<>();
|
---|
| 22 | ObjectMapper mapper = new ObjectMapper();
|
---|
| 23 | try {
|
---|
[c164f8f] | 24 | ClassLoader classLoader = getClass().getClassLoader();
|
---|
| 25 | JsonNode root = mapper.readTree(new File(classLoader.getResource("URLsJSON.json").getFile()));
|
---|
| 26 |
|
---|
[d4d8f61] | 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 |
|
---|
[c164f8f] | 42 |
|
---|
| 43 | public Void call() {
|
---|
[d4d8f61] | 44 | System.out.println("Scraper has started ");
|
---|
| 45 | for (String url : urls) {
|
---|
[c164f8f] | 46 | new ScraperThread(url, optionsQueue, latch).start();
|
---|
[d4d8f61] | 47 | }
|
---|
[c164f8f] | 48 | return null;
|
---|
[d4d8f61] | 49 | }
|
---|
| 50 | }
|
---|