Repository navigation
Expand file tree
/
Copy pathApp.java
More file actions
93 lines (75 loc) · 3.12 KB
/
Copy pathApp.java
File metadata and controls
93 lines (75 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Instant;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class App {
static class PricePoint {
String price;
String timestamp;
PricePoint(String price, String timestamp) {
this.price = price;
this.timestamp = timestamp;
}
@Override
public String toString() {
return "price=" + price + ", timestamp=" + timestamp;
}
}
private static final Queue<PricePoint> priceQueue = new ConcurrentLinkedQueue<>();
public static void main(String[] args) throws InterruptedException {
String apiKey = System.getenv("TWELVE_DATA_API_KEY");
if (apiKey == null || apiKey.isBlank()) {
System.out.println("No API key found!");
return;
}
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
Runnable pollTask = () -> {
try {
String json = fetchJson(apiKey);
String price = extractPrice(json);
String timestamp = Instant.now().toString();
PricePoint point = new PricePoint(price, timestamp);
priceQueue.add(point);
System.out.println("Added data point: " + point);
System.out.println("Current queue size: " + priceQueue.size());
} catch (Exception e) {
System.out.println("Error fetching price: " + e.getMessage());
}
};
// run immediately, then every 15 seconds
scheduler.scheduleAtFixedRate(pollTask, 0, 15, TimeUnit.SECONDS);
// let it run for 5 polls (75 seconds) then stop, so it doesn't run forever
Thread.sleep(75000);
scheduler.shutdown();
System.out.println("Done. Total data points collected: " + priceQueue.size());
}
private static String fetchJson(String apiKey) throws Exception {
String urlStr = "https://api.twelvedata.com/price?symbol=DIA&apikey=" + apiKey;
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
conn.disconnect();
return response.toString();
}
private static String extractPrice(String json) {
String key = "\"price\"";
int keyIndex = json.indexOf(key);
int colonIndex = json.indexOf(':', keyIndex);
int firstQuote = json.indexOf('"', colonIndex);
int secondQuote = json.indexOf('"', firstQuote + 1);
return json.substring(firstQuote + 1, secondQuote);
}
}