-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
81 lines (63 loc) · 2.8 KB
/
Copy pathApp.java
File metadata and controls
81 lines (63 loc) · 2.8 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.Queue;
public class App {
//getting the API Key
private static final String API_KEY = System.getenv("TWELVE_DATA_API_KEY");
private static final String GET_URL = "https://api.twelvedata.com/price?symbol=DIA&apikey="+API_KEY;
//or i could hard code the api key into the URL variable as so (delete before submitting):
//private static final String GET_URL = "https://api.twelvedata.com/price?symbol=DIA&apikey={pastemyurlhere}";
private static final Queue<Double> resultQueue = new LinkedList<>();
public static void main(String[] args) throws IOException {
while(true){
sendGetRequest();
try {
Thread.sleep((15000));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
private static void sendGetRequest() throws IOException {
URL obj = new URL(GET_URL);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
System.out.println("GET Response code: " + responseCode);
//if the response code is successful
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// parsing, storing and printing result
String output =response.toString();
Double price = getPriceFromOutput(output);
resultQueue.add(price);
System.out.println(resultQueue);
LocalDateTime dateTimeStamp = LocalDateTime.now();
System.out.println("Added data point: price= "+ price +
", timestamp= " + dateTimeStamp);
System.out.println("Current queue size: " + resultQueue.size());
System.out.println("Waiting 15 seconds to obtain the next value");
} else {
System.out.println("GET request did not work.");
}
}
private static Double getPriceFromOutput(String JSONOutput){
String marker = "\"price\":\"";
int start = JSONOutput.indexOf(marker);
start += marker.length();
int end = JSONOutput.indexOf("\"", start);
String price = JSONOutput.substring(start, end);
return Double.parseDouble(price);
}
}