Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Objects;
- public class Quotes {
- public String[][] get(String[] id, String[] properties) {
- List<List<String>> quotesList = new ArrayList<>();
- String[][] quotes;
- try {
- URL q_endpoint = new URL(buildUrl(id, properties));
- BufferedReader in = new BufferedReader(new InputStreamReader(q_endpoint.openStream()));
- String inputLineTmp;
- while ((inputLineTmp = in.readLine()) != null && !Objects.equals(inputLineTmp, "")) {
- quotesList.add(Arrays.asList(inputLineTmp.split(",")));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- quotes = new String[quotesList.size()][];
- for (int i = 0; i < quotesList.size(); i++) {
- List<String> quote = quotesList.get(i);
- quotes[i] = quote.toArray(new String[quote.size()]);
- }
- return quotes;
- }
- private String buildUrl(String id[], String[] properties) {
- String url = "http://finance.yahoo.com/d/quotes.csv";
- //add and encode ids to the url
- try {
- if (id.length != 0) {
- url += "?s=";
- for (int i = 0; i < id.length; i++) {
- url += URLEncoder.encode(id[i], "UTF-8");
- if (i != id.length - 1) url += ',';
- }
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- //add to url properties
- if (properties.length != 0) {
- url += "&f=";
- for (int i = 0; i < properties.length; i++) {
- url += properties[i];
- if (i != properties.length - 1) url += ',';
- }
- }
- //add static part
- url += "&e=.csv";
- return url;
- }
- public static void main(String[] args) {
- Quotes q = new Quotes();
- String[][] o = q.get(new String[] {"GOOG"}, new String[]{"nsl1op"});
- System.out.println(Arrays.toString(o));
- }
- }
Add Comment
Please, Sign In to add comment