importjava.io.IOException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;importjava.util.Timer;importjava.util.TimerTask;importorg.apache.commons.lang.StringUtils;importorg.jsoup.Jsoup;importorg.jsoup.nodes.Document;importorg.jsoup.select.Elements;importcom.jdkeji.sdcx.common.exceptions.SdcxException;importcom.jdkeji.sdcx.common.model.Flight;importnet.minidev.json.JSONObject;/*** 携程 航班爬取数据工具类
* @Description:
*@author:haoqingshuang
* @CreatTime:2017年9月28日
**/
public classXcFlightUtil {public static Map cacheMap = new HashMap();public staticTimer cacheFlightTimer;static{
clearCacheMap();
}public static synchronizedTimer getFlightTimer(){if(cacheFlightTimer == null){return cacheFlightTimer = newTimer();
}else{returncacheFlightTimer;
}
}//定时清空航班缓存数据 立即清空 10分钟
public static voidclearCacheMap(){
TimerTask timertask= newjava.util.TimerTask() {
@Overridepublic voidrun() {
cacheMap.clear();
}
};
Timer cacheFlightTimer=getFlightTimer();
cacheFlightTimer.schedule(timertask,0,600000);
}public static Document getDocument(String url) throwsSdcxException{try{returnJsoup.connect(url).get();
}catch(IOException e) {throw new SdcxException("获取航班信息异常,请自行查询航班数据!");
}
}/*** 根据航班号 爬取航班信息
* @description:
*@author:haoqingshuang
* @CreateDate:2017年9月28日*/
public static JSONObject findFlightByFlightCode(String FlightNumber) throwsSdcxException{
Flight flightModel= newFlight();if(StringUtils.isEmpty(FlightNumber)){throw new SdcxException("请输入航班号!");
}for(String key : cacheMap.keySet()) {if(FlightNumber.equals(key)){if(null !=cacheMap.get(key)){returncacheMap.get(key).getFlightJsonobject();
}
}
}
String nowDate= new SimpleDateFormat("yyyyMMdd").format(newDate());
Document doc= getDocument("http://flights.ctrip.com/actualtime/fno--"+FlightNumber.trim()+"-"+nowDate+".html");if(null ==doc){throw new SdcxException("网络异常,请稍后再试!");
}//航班详情
Elements flightDetail = doc.select("[class=detail-m]");
Elements detailfly= flightDetail.select("[class=detail-fly]");
Elements inldepartureTime= detailfly.select("[class=inl departure] [class=time]");
flightModel.setActualFlyTime(commonIsNull(inldepartureTime));
Elements inldeparturegray= detailfly.select("[class=inl departure] [class=gray]");
flightModel.setPlanFlyTime(commonIsNull(inldeparturegray));
Elements inlbetween= detailfly.select("[class=inl between]");
flightModel.setFlightStatus(commonIsNull(inlbetween));
Elements inlarriveTime= detailfly.select("[class=inl arrive] [class=time]");
flightModel.setActualArriveTime(commonIsNull(inlarriveTime));
Elements inlarrivegray= detailfly.select("[class=inl arrive] [class=gray]");
flightModel.setPlayArriveTime(commonIsNull(inlarrivegray));
Elements detailroute= flightDetail.select("[class=detail-fly detail-route]");
Elements routeinldeparture= detailroute.select("[class=inl departure] p");
flightModel.setSetoutAddress(commonIsNull(routeinldeparture));
Elements routeinlbetween= detailroute.select("[class=inl between]").select("p");
flightModel.setFlyData(commonIsNull(routeinlbetween));
Elements routeinlarrive= detailroute.select("[class=inl arrive] p");
flightModel.setArriveAddres(commonIsNull(routeinlarrive));
Elements additionalDetail= doc.select("[class=detail-info] [class=operation]");
Elements operation= additionalDetail.select("[class=item]");
flightModel.setFlyAdditional(commonIsNull(operation));
Elements flightT= doc.select("[class=detail-t]");
Elements flightCompany= flightT.select("[class=ml5]");
flightModel.setFlightCompay(commonIsNull(flightCompany));
Elements flightCode= flightT.select("strong");
flightModel.setFlightCode(commonIsNull(flightCode));
Elements flightPunctuality= doc.select("[class=f14 gray ml10]");
flightModel.setPunctualityRate(commonIsNull(flightPunctuality));
cacheMap.put(flightModel.getFlightCode(), flightModel);returnflightModel.getFlightJsonobject();
}public staticString commonIsNull(Elements elements) {try{if(null !=elements){if(null != elements.get(0)){return elements.get(0).text();
}
}return "";
}catch(Exception e) {return "";
}
}public staticString commonDoubleIsNull(Elements elements) {try{
StringBuilder sBuilder= newStringBuilder();if(null !=elements){for (int i = 0; i < elements.size(); i++) {if(null !=elements.get(i)){
sBuilder.append(elements.get(i).text());
}
}
}returnsBuilder.toString();
}catch(Exception e) {return "";
}
}public static voidmain(String[] args) {try{//System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
System.out.println(findFlightByFlightCode("HU7835"));;//System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
} catch(SdcxException e) {
System.out.println(e.getMessage());
}
}
}