public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test = new Test();
test.myRun();
}
private static void myRun(){
List<Map<String, Object>> etfList= getETFName_db();
//getETFDebtInfo(etfList,"ETFBasicsINfo",1);//添加ETF基础数据(Name ,类型,基金家族) 已經添加完成
String url=null;
List<Map<String, Object>> etfErrorList=new ArrayList<Map<String ,Object>>();
int count=0;//
for(int i=0;i<etfList.size();i++){
int num=0;
String chart=etfList.get(i).get("Ticker").toString();
//https://query1.finance.yahoo.com/v7/finance/chart/"+chart+"?range=10y&interval=1d&indicators=quote"
//+ "&includeTimestamps=true&includePrePost=false&corsDomain=finance.yahoo.com"
try {
url="https://query1.finance.yahoo.com/v7/finance/chart/"+chart+"?range=10y&interval=1d&indicators=quote"
+ "&includeTimestamps=true&includePrePost=false&corsDomain=finance.yahoo.com";
while(Run(url)==0){
num++;
if(num<3){
continue;
}else {
Map<String, Object>ETFMap =new HashMap<>();
ETFMap.put("ETF", chart);
etfErrorList.add(ETFMap);
break;
}
}
} catch (Exception e) {
// TODO: handle exception
if(num<3){
count++;
}
System.out.println(chart+"出錯");
}
}
getETFDebtInfo(etfErrorList,"ETFBasicsINfo",2);
if(etfErrorList.size()>0){
System.out.println("添加完成!共"+count+"条!");
System.out.println("失败"+etfErrorList.size()+"条!");
System.out.println("失败信息:"+etfErrorList);
}else{
System.out.println("全部添加完成!共"+count+"条!");
}
}
@SuppressWarnings("unchecked")
private static int Run(String url){
Map<String,Object> htmlMap=new HashMap<>();//1
Map<String,Object> chartMap=new HashMap<>();//2
List<Map<String, Object>> mapList=new ArrayList<Map<String,Object>>();//3
Map<String,Object>map4=new HashMap<>();//4
cUrl c=new cUrl();
String html=null;
try {
html=c.httpGet(url);//得到一个html 页面元素
if(html == null || html.length() < 10){
return 0;
}
} catch (Exception e) {
// TODO: handle exception
}
htmlMap= (Map<String, Object>) JSONObject.parse(html);//把该页面元素转成JSON对象 存如Map
for (String charkey : htmlMap.keySet()){
chartMap=(Map<String, Object>) htmlMap.get(charkey);
for (String chartkey : chartMap.keySet()) {//2
if(chartMap.get(chartkey)!=null){
mapList=(List<Map<String, Object>>) chartMap.get(chartkey);
map4=(Map<String, Object>) mapList.get(0).get("meta");
getETFDebtInfo(mapList,map4.get("symbol").toString(),0);
}
}
}
return 1;
}
private static void getETFDebtInfo(List<Map<String, Object>> getETFDebtInfoList,String ETFName,int num) {
String RobomongDBName=null;
switch (num) {
case 0:
RobomongDBName="ETFDebtInfo";
break;
case 1:
RobomongDBName="ETFBasicsINfo";
break;
case 2:
RobomongDBName="ETFErrorLog";
break;
default:
break;
}
List<Map<String, Object>> mfo = getETFDebtInfoList;
Map<String, Object> kMap = new HashMap<>();
kMap.put(ETFName, mfo);
String kJson = JSON.toJSONString(kMap);
MGDB mgdb = new MGDB();
mgdb.DBEnd();
mgdb.SetCollection(RobomongDBName);
Map<String, Object> wMap = new HashMap<>();
Map<String, Object> eMap = new HashMap<>();
eMap.put(mgdb.exists, 1);
wMap.put(RobomongDBName, eMap);
String wjson = JSON.toJSONString(wMap);
mgdb.JsonWhere(wjson);
mgdb.Delete();
mgdb.Insert(kJson);
mgdb.Close();
System.out.println("添加"+RobomongDBName+"数据 ok!");
}