codice java per nuova modalità scarico dati EOD yahoo

homohominilupus

Nuovo Utente
Registrato
8/12/09
Messaggi
155
Punti reazioni
10
Sperando possa esservi utile, ho scritto un frammento di codice in Java per permettere di scaricare i dati EOD da yahoo secondo la nuova modalità, che utilizza dati di sessione.
Il linguaggio è Java, facilmente adattabile in altri contesti:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author user
*/
public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {

String symbol="XMIB.MI";
Logger LOG=Logger.getLogger(JavaApplication1.class.getName());
//LOG.info(URLEncoder.encode("PgPdwIF5x\u002Fv", "UTF-8"));
//if (true)return;
URL url1 = new URL("https://finance.yahoo.com/quote/"+symbol+"/history?p="+symbol);


URLConnection conn = url1.openConnection();
//conn.setRequestMethod("GET");
Map<String,List<String>> map=conn.getHeaderFields();
StringBuilder cookie=new StringBuilder();
StringBuilder result = new StringBuilder();
map.keySet().forEach((String k) -> {
map.get(k).forEach(v->{LOG.log(Level.INFO, "{0}\t{1}", new Object[]{k, v}); if (k!=null && k.equalsIgnoreCase("set-cookie")) cookie.append(v);});
});
LOG.info(cookie.toString());
String scookie=cookie.toString().split(";")[0];
LOG.info(scookie);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
int k1=result.indexOf("CrumbStore");
int k2=result.indexOf(""",k1+22);
String crumb=result.substring(k1+21,k2).replace(""", "").replace("\\u00", "%");
LOG.info(crumb);
//String u2="https://query1.finance.yahoo.com/v7/finance/download/XMIB.MI?period1=1492793692&period2=1495385692&interval=1d&events=history&crumb="+crumb;
String u2="https://query1.finance.yahoo.com/v7/finance/download/"+symbol+"?period1=0&period2="+System.currentTimeMillis()+"&interval=1d&events=history&crumb="+crumb;
LOG.info(u2);
URL url2 = new URL(u2);
URLConnection cc=url2.openConnection();
cc.setRequestProperty("Cookie", scookie);
rd = new BufferedReader(new InputStreamReader(cc.getInputStream()));
result = new StringBuilder();
while ((line = rd.readLine()) != null) {
result.append(line).append("\n");
}
rd.close();
LOG.info(result.toString());
}

}
 
Indietro