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

Last change on this file since df7f390 was df7f390, checked in by Kristijan <kristijanzafirovski26@…>, 2 days ago

Added frontend functionality for changes and refactored code

  • Property mode set to 100644
File size: 1.5 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 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}
Note: See TracBrowser for help on using the repository browser.