| 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 CountDownLatch latch;
|
|---|
| 17 |
|
|---|
| 18 | public Scraper() {
|
|---|
| 19 | urls = new ArrayList<>();
|
|---|
| 20 | ObjectMapper mapper = new ObjectMapper();
|
|---|
| 21 | try {
|
|---|
| 22 | ClassLoader classLoader = getClass().getClassLoader();
|
|---|
| 23 | JsonNode root = mapper.readTree(new File(classLoader.getResource("URLsJSON.json").getFile()));
|
|---|
| 24 |
|
|---|
| 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 |
|
|---|
| 40 |
|
|---|
| 41 | public Void call() {
|
|---|
| 42 | System.out.println("Scraper has started ");
|
|---|
| 43 | for (String url : urls) {
|
|---|
| 44 | new ScraperThread(url, latch).start();
|
|---|
| 45 | }
|
|---|
| 46 | return null;
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|