source: backend/GlobeGuru-backend/src/main/java/Scraper.java@ 53bad7e

Last change on this file since 53bad7e was c164f8f, checked in by Kristijan <kristijanzafirovski26@…>, 6 days ago

pred-finalna

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[d4d8f61]1import com.fasterxml.jackson.databind.JsonNode;
2import com.fasterxml.jackson.databind.ObjectMapper;
3
4import java.io.File;
5import java.io.IOException;
6import java.util.ArrayList;
7import java.util.Iterator;
8import java.util.List;
[c164f8f]9import java.util.concurrent.Callable;
[d4d8f61]10import java.util.concurrent.ConcurrentLinkedQueue;
11import java.util.concurrent.CountDownLatch;
12
[c164f8f]13public 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}
Note: See TracBrowser for help on using the repository browser.