Java tutorial
/* * Copyright (c) 2013 WDCY Information Technology Co. Ltd * www.wdcy.cc * All rights reserved. * * This software is the confidential and proprietary information of * WDCY Information Technology Co. Ltd ("Confidential Information"). * You shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement you * entered into with WDCY Information Technology Co. Ltd. */ package com.silentwu.schedule.component; import com.silentwu.schedule.dto.StartCityDto; import com.silentwu.schedule.dto.TargetCityDto; import com.silentwu.schedule.entity.City; import com.silentwu.schedule.entity.Schedule; import com.silentwu.schedule.utils.DateUtils; import com.silentwu.schedule.utils.HttpUtils; import com.silentwu.schedule.utils.JsonUtils; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import java.io.IOException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author Wu Tianqiang */ @Component public class WebAnalyzeService { public final static String START_CTIY_URL = "http://www.scqcp.com/get/getCity.html"; public final static String TARGET_CITY_URL = "http://www.scqcp.com/get/targetCitys.html"; public final static String SCHEDULE_URL = "http://www.scqcp.com/query/searchTicket.html"; public static void main(String[] args) throws IOException { System.out.println(URLDecoder.decode("%E6%88%90%E9%83%BD%E5%B8%82", "utf-8")); final WebAnalyzeService webAnalyzeService = new WebAnalyzeService(); final List<City> startCities = webAnalyzeService.findStartCities("cd"); System.out.println("startCities:\n" + startCities); final List<City> endCities = webAnalyzeService.findTargetCities(startCities.get(0).getId(), startCities.get(0).getChinaName(), "my"); System.out.println(endCities); // System.out.println(webAnalyzeService.findScheduleFromHtml()); } public List<Schedule> findScheduleFromHtml(String cityId, String cityName, String targetName, String searchDate) throws IOException { Map<String, Object> params = new HashMap<String, Object>(); params.put("cityId", cityId); params.put("cityName", cityName); params.put("targetNmae", targetName); params.put("searchDate", searchDate); String html = HttpUtils.doGet(SCHEDULE_URL, params); Map<String, Object> map = htmlConvertToMap(html); Assert.isTrue((Boolean) map.get("success"), "?"); List<Schedule> schedules = new ArrayList<Schedule>(); for (Object schedule : (ArrayList) map.get("data")) { schedules.add(fromMap((Map) schedule)); } Assert.notEmpty(schedules, "?"); return schedules; } public static Schedule fromMap(Map map) { Schedule schedule = new Schedule(); schedule.setStation((String) map.get("CarryStaName")); schedule.setStartDate(DateUtils.stringToDate((String) map.get("DrvDateTime"))); schedule.setPass((String) map.get("StopName")); schedule.setTerminus((String) map.get("EndStaName")); if ("??".equals(map.get("SchTypeName"))) { schedule.setType("??"); } else { schedule.setType("?"); } schedule.setMileage((Double) map.get("Mile")); schedule.setMotorcycleType((String) map.get("BusTypeName")); schedule.setPrice((Double) map.get("FullPrice")); schedule.setAmount((Integer) map.get("SAmount")); schedule.setFreeChildren((Integer) map.get("ChildSAmount")); return schedule; } private Map<String, Object> htmlConvertToMap(String html) { Pattern p = Pattern.compile("(\\{\"success)(.*)(\"}]})"); Matcher m = p.matcher(html); List<String> schedules = new ArrayList<String>(); while (m.find()) { schedules.add(m.group()); } String result = schedules.get(0); return JsonUtils.encodeJson2Object(result, Map.class); } /** * * * @return */ public List<City> findStartCities(String query) throws IOException { Map<String, Object> params = new HashMap<String, Object>(); params.put("type", "shike_st"); params.put("callback", "jsonpcallback410"); params.put("s", query); String result = HttpUtils.doGet(START_CTIY_URL, params); List<StartCityDto> startCityDtoLists = JsonUtils.encodeJson2ListEntity( result.substring(result.indexOf('['), result.lastIndexOf(']') + 1), StartCityDto.class); return StartCityDto.toCitys(startCityDtoLists); } /** * */ public List<City> findTargetCities(int startCityId, String cityChinaName, String query) throws IOException { Map<String, Object> params = new HashMap<String, Object>(); params.put("cid", startCityId); params.put("s", query); params.put("city_name", cityChinaName); params.put("t", "undefined"); params.put("callback", "jsonpcallback645"); String result = HttpUtils.doGet(TARGET_CITY_URL, params); List<TargetCityDto> targetCityDtos = JsonUtils.encodeJson2ListEntity( result.substring(result.indexOf('['), result.lastIndexOf(']') + 1), TargetCityDto.class); return TargetCityDto.toCities(targetCityDtos); } }