-
Notifications
You must be signed in to change notification settings - Fork 0
/
UrlData.java
175 lines (156 loc) · 6.49 KB
/
UrlData.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* The class manipulates the URL data.
*
* @Author: Junxiang Chen
* @RegistrationNumber: 180127586
* @Email: [email protected]
*/
/*
import dependencies
*/
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/*
URLData class
*/
public class UrlData {
/*
declare class variables
*/
private static final String url1 = "https://quotes.wsj.com/";
private static final String url2 = "/historical-prices/download?MOD_VIEW=page&num_rows=300&startDate=";
private static final String url3 = "&endDate=";
public static String ticker;
public static String startDate;
public static String endDate;
public static boolean validation = false;
public static String url;
private static final String errMsgTicker = "Invalid Input of Ticker. Please Select a Ticker!";
private static final String errMsgStartDate = "Invalid Input of Start Date. Please Select a Date!";
private static final String errMsgEndDate = "Invalid Input of End Date. Please Select a Date!";
public static String errMsg = "";
public static File file;
public static String filename = "HistoricalPrices.csv";
/*
define class methods
*/
/**
* parse the string of ticker name, to get the ticker symbol name
*
* @param val String, the value of ticker name
* @return String, the ticker symbol name
*/
private static String parseTicker(String val) {
String ticker = "";
for (int i = 0; i < val.length(); i++) {
if (val.substring(i, i + 1).equals(" ")) {
ticker = val.substring(0, i).trim();
break;
}
}
return ticker;
}
/**
* if the data given by user is valid, concat strings to make an URL
*/
public static void setUrl() {
if (ticker != null && startDate != null && endDate != null) {
validation = true;
url = url1 + parseTicker(ticker) + url2 + startDate + url3 + endDate;
System.out.println("Request URL:");
System.out.println(url);
}
}
/**
* validate whether the necessary data is all given by users
* if any part is invalid input, set relevant error message to remind the user to select again
*/
public static void setValidation() {
errMsg = "";
if (ticker == null) {
validation = false;
errMsg = errMsg + "\n" + errMsgTicker;
}
if (startDate == null) {
validation = false;
errMsg = errMsg + "\n" + errMsgStartDate;
}
if (endDate == null) {
validation = false;
errMsg = errMsg + "\n" + errMsgEndDate;
}
}
/**
* if the URL is valid, retrieve data from the WSJ site
* save *.csv file to the classpath
*/
public static void saveUrlAs() {
// find the classpath
file = new File(System.getProperty("user.dir"));
// if the URL is valid, set http connection
if (validation) {
if (!file.exists()) {
file.mkdirs();
System.out.println("not exist path");
} else {
System.out.println("Save file to: ");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
}
FileOutputStream fileOutputStream;
HttpURLConnection httpURLConnection;
InputStream inputStream;
try {
URL httpUrl = new URL(url);
// open connection
httpURLConnection = (HttpURLConnection) httpUrl.openConnection();
// set time out period
httpURLConnection.setConnectTimeout(60000);
httpURLConnection.setReadTimeout(60000);
// set the request method
httpURLConnection.setRequestMethod("GET");
// set other attributes
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(true);
// connect to the URL
httpURLConnection.connect();
// if the connection is succeed (respond code is 200), download data
if (httpURLConnection.getResponseCode() == 200) {
inputStream = httpURLConnection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
fileOutputStream = new FileOutputStream(System.getProperty("user.dir") + "/" + filename);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] bytes = new byte[1024];
int length = bufferedInputStream.read(bytes);
while (!Thread.interrupted() && length != -1) {
bufferedOutputStream.write(bytes, 0, length);
length = bufferedInputStream.read(bytes);
}
bufferedOutputStream.close();
bufferedInputStream.close();
// in this condition, the Internet is connected, so if the data is empty, it must be the reason that
// the user selected date range has no data
// set error message
errMsg = errMsg + "Internet Connection is OK.\n" +
"But in the Range of DATE for your Selected Ticker has not DATA to Display!\n" +
"Please Select another Range of DATE or another Ticker!\n";
} else {
// in this case, the Internet connection is fine, but the response is not successful,
// that is, it is not the user's fault
// so the error message is to reveal the response code to the user
errMsg = errMsg + "Http Response Code: " + httpURLConnection.getResponseCode() + "\n";
}
// after connection, disconnect to the WSJ site
httpURLConnection.disconnect();
} catch (Exception e) {
// in this case, the Internet connection is failed,
// so the error message is to remind the user to set the Internet correctly
// e.printStackTrace();
errMsg = errMsg + "Internet Connection Failed!\n" +
"Please Check your Internet Settings!\n";
}
}
}
}