[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 CountDownLatch latch;
|
---|
| 17 |
|
---|
[c164f8f] | 18 | public Scraper() {
|
---|
[d4d8f61] | 19 | urls = new ArrayList<>();
|
---|
| 20 | ObjectMapper mapper = new ObjectMapper();
|
---|
| 21 | try {
|
---|
[c164f8f] | 22 | ClassLoader classLoader = getClass().getClassLoader();
|
---|
| 23 | JsonNode root = mapper.readTree(new File(classLoader.getResource("URLsJSON.json").getFile()));
|
---|
| 24 |
|
---|
[d4d8f61] | 25 | JsonNode urlNode = root.get("agencyurls");
|
---|
| 26 | if (urlNode.isArray()) {
|
---|
| 27 | Iterator<JsonNode> elements = urlNode.elements();
|
---|
| 28 | while (elements.hasNext()) {
|
---|
| 29 | JsonNode next = elements.next();
|
---|
| 30 | urls.add(next.asText());
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | System.out.println("Loaded " + urls.size() + " urls");
|
---|
| 34 | } catch (IOException e) {
|
---|
| 35 | throw new RuntimeException(e);
|
---|
| 36 | }
|
---|
| 37 | this.latch = new CountDownLatch(urls.size());
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[c164f8f] | 40 |
|
---|
| 41 | public Void call() {
|
---|
[d4d8f61] | 42 | System.out.println("Scraper has started ");
|
---|
| 43 | for (String url : urls) {
|
---|
[df7f390] | 44 | new ScraperThread(url, latch).start();
|
---|
[d4d8f61] | 45 | }
|
---|
[c164f8f] | 46 | return null;
|
---|
[d4d8f61] | 47 | }
|
---|
| 48 | }
|
---|