|
@@ -47,9 +47,11 @@ public class HbdtSHFecthServiceImpl implements HbdtFetchService {
|
|
|
@Autowired
|
|
|
private IdGenerator idGenerator;
|
|
|
public static final String baseUrl = "http://www.shanghaiairport.com/ajax/flights/search.aspx?action=getData";
|
|
|
-
|
|
|
+
|
|
|
|
|
|
private int pageSize = 500;
|
|
|
+ private String shpd = "上海浦东";
|
|
|
+ private String shhq = "上海虹桥";
|
|
|
|
|
|
/**
|
|
|
* 获取江苏省范围内的航班信息
|
|
@@ -57,18 +59,31 @@ public class HbdtSHFecthServiceImpl implements HbdtFetchService {
|
|
|
@Override
|
|
|
public int doFetch() {
|
|
|
List<AirHbdt> airHbdtList = null;
|
|
|
+ List<AirHbdt> airHbdtList2 = null;
|
|
|
try {
|
|
|
- // 1.国内出发;2.国内到达
|
|
|
- airHbdtList = fetch(1);
|
|
|
- airHbdtList.addAll(fetch(2));
|
|
|
+
|
|
|
+ // 上海虹桥
|
|
|
+ // 1.国内出发;2.国内到达
|
|
|
+ airHbdtList = fetch(1, shhq);
|
|
|
+ airHbdtList.addAll(fetch(2, shhq));
|
|
|
if(null != airHbdtList && airHbdtList.size() > 0) {
|
|
|
- airHbdtService.insertAll(airHbdtList, "上海");
|
|
|
+ airHbdtService.insertAll(airHbdtList, shhq);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上海浦东
|
|
|
+ // 1.国内出发;2.国内到达
|
|
|
+ airHbdtList2 = fetch(1, shpd);
|
|
|
+ airHbdtList2.addAll(fetch(2, shpd));
|
|
|
+ if(null != airHbdtList2 && airHbdtList2.size() > 0) {
|
|
|
+ airHbdtService.insertAll(airHbdtList2, shpd);
|
|
|
}
|
|
|
}
|
|
|
catch (Throwable t) {
|
|
|
logger.error("抓取上海机场航班动态出错:{}", t.getMessage());
|
|
|
}
|
|
|
- return null == airHbdtList ? 0 : airHbdtList.size();
|
|
|
+ int count = null == airHbdtList ? 0 : airHbdtList.size();
|
|
|
+ int count2 = null == airHbdtList2 ? 0 : airHbdtList2.size();
|
|
|
+ return count + count2;
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
@@ -76,17 +91,17 @@ public class HbdtSHFecthServiceImpl implements HbdtFetchService {
|
|
|
a.doFetch();
|
|
|
}
|
|
|
|
|
|
- private List<AirHbdt> fetch(int type) throws IOException, ParserException {
|
|
|
+ private List<AirHbdt> fetch(int type, String target) throws IOException, ParserException {
|
|
|
List<AirHbdt> hbdtList = new ArrayList<AirHbdt>();
|
|
|
try {
|
|
|
- String html = requestHbdt(type, 1);
|
|
|
+ String html = requestHbdt(type, target, 1);
|
|
|
if(!StringUtils.isEmpty(html)) {
|
|
|
String flg = html.substring(0, html.indexOf("$$$"));
|
|
|
if("ok".equals(flg.toLowerCase())) {
|
|
|
// 获取总页数
|
|
|
String totalStr = html.substring(html.lastIndexOf("$$$")+3, html.length());
|
|
|
html = html.substring(html.indexOf("$$$")+3,html.lastIndexOf("$$$"));
|
|
|
- hbdtList = parseString(html, type);
|
|
|
+ hbdtList = parseString(html, target, type);
|
|
|
int totalNum = Integer.parseInt(totalStr);
|
|
|
if(totalNum > pageSize) {
|
|
|
int pageNum = totalNum/pageSize;
|
|
@@ -94,9 +109,9 @@ public class HbdtSHFecthServiceImpl implements HbdtFetchService {
|
|
|
pageNum = pageNum - 1;
|
|
|
}
|
|
|
for(int i = 0; i < pageNum; i++) {
|
|
|
- String htmlPage = requestHbdt(type, i+2);
|
|
|
+ String htmlPage = requestHbdt(type, target, i+2);
|
|
|
htmlPage = htmlPage.substring(htmlPage.indexOf("$$$")+3,htmlPage.lastIndexOf("$$$"));
|
|
|
- hbdtList.addAll(parseString(htmlPage, type));
|
|
|
+ hbdtList.addAll(parseString(htmlPage, target, type));
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
@@ -110,13 +125,19 @@ public class HbdtSHFecthServiceImpl implements HbdtFetchService {
|
|
|
return hbdtList;
|
|
|
}
|
|
|
|
|
|
- public String requestHbdt(int type, int currentPage) {
|
|
|
+ public String requestHbdt(int type, String target, int currentPage) {
|
|
|
String html = null;
|
|
|
List<NameValuePair> params = new ArrayList<>();
|
|
|
// 1.客班、2.货班
|
|
|
params.add(new BasicNameValuePair("flightType", "1"));
|
|
|
// 第一页
|
|
|
params.add(new BasicNameValuePair("currentPage", String.valueOf(currentPage)));
|
|
|
+ // 1.SHA、2.PVG
|
|
|
+ if(target.equals(shhq)) {
|
|
|
+ params.add(new BasicNameValuePair("airCities2", "SHA"));
|
|
|
+ } else if(target.equals(shpd)){
|
|
|
+ params.add(new BasicNameValuePair("airCities2", "PVG"));
|
|
|
+ }
|
|
|
// 每页显示条数
|
|
|
params.add(new BasicNameValuePair("pageSize", String.valueOf(pageSize)));
|
|
|
// 航向: 1.国内出发;2.国内到达
|
|
@@ -132,7 +153,7 @@ public class HbdtSHFecthServiceImpl implements HbdtFetchService {
|
|
|
return "上海航班动态数据抓取服务";
|
|
|
}
|
|
|
|
|
|
- private List<AirHbdt> parseString(String html, int type) throws ParserException {
|
|
|
+ private List<AirHbdt> parseString(String html,String target, int type) throws ParserException {
|
|
|
List<AirHbdt> retList = new ArrayList<AirHbdt>();
|
|
|
JSONArray jsonArray = null;
|
|
|
jsonArray = JSONArray.fromObject(html);
|
|
@@ -141,7 +162,7 @@ public class HbdtSHFecthServiceImpl implements HbdtFetchService {
|
|
|
JSONObject obj = (JSONObject) jsonArray.get(i);
|
|
|
AirHbdt hbdt = new AirHbdt();
|
|
|
hbdt.setId(idGenerator.generateStringId());
|
|
|
- hbdt.setTargetPort("上海");
|
|
|
+ hbdt.setTargetPort(target);
|
|
|
hbdt.setType(type);
|
|
|
// 航班号
|
|
|
hbdt.setHbh(obj.get("主航班号").toString());
|