packagecom.jointsky.jointframe.weather.utils;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.URL;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importorg.apache.commons.lang.StringUtils;importcom.jointsky.jointframe.weather.entity.ActuallyForecastWeather;importcom.jointsky.jointframe.weather.entity.ActuallyWeather;
public classUrlInfo {
private static Pattern proInfo = Pattern.compile("(.*?)", Pattern.DOTALL);
private static String countyCodes = "53614,53610,53618,53619";
public static List getURLInfoOfActully(String urlInfo,String charset) throwsException {
String info=getUrlInfo(urlInfo);//獲得網頁源碼(0是當天)
return getDataStructure(info,0);
}
public static List getURLInfoOfForecast(String urlInfo,String charset) throwsException {
String info=getUrlInfo(urlInfo);//獲得網頁源碼(1是預報)
return getDataStructure(info,1);
}
public static String getUrlInfo(String urlInfo) throwsException {//讀取目的網頁URL位址,擷取網頁源碼
URL url = newURL(urlInfo);
HttpURLConnection httpUrl=(HttpURLConnection)url.openConnection();
InputStream is=httpUrl.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuilder sb= newStringBuilder();
String line;while ((line = br.readLine()) != null) {//這裡是對連結進行處理
line = line.replaceAll("?a[^>]*>", "");//這裡是對樣式進行處理
line = line.replaceAll("]*>", "");
sb.append(line);
}
is.close();
br.close();returnsb.toString().trim();
}private static List getDataStructure(String str,intj) {//運用正規表達式對擷取的網頁源碼進行資料比對,提取我們所要的資料,在以後的過程中,我們可以采用httpclient+jsoup,//現在暫時運用正規表達式對資料進行抽取提取//String[] info = str.split("");
SimpleDateFormat sf = new SimpleDateFormat("HH");
Date dateTime= newDate();
String hour=sf.format(dateTime);
Integer h=Integer.parseInt(hour);int t = 0;//如果十二點之前當天會有四個時間段子產品(今天上午6~12;今天下午12~18;今天前半夜18~24;今天後半夜次日00~06)
if (h<=12) {
t=4;//如果十二點之後十八點之前當天會有三個時間段子產品(今天下午12~18;今天前半夜18~24;今天後半夜次日00~06)
}else if (12
t=3;//如果十八點之後當天會有兩個時間段子產品(今天前半夜18~24;今天後半夜次日00~06)
}else if(h>18) {
t=2;
}
String[] info= str.split("
");
List list_actually = new ArrayList();
List list_forecast = new ArrayList();
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");//當t的值是幾的時候相應的當天的天氣資料就還剩下幾個子產品,k就是用來控制第幾個子產品資料的參數
int k = 0;for(String s : info) {//這個Pattern對象将會使用matcher()方法來生成一個Matcher執行個體,接着便可以使用該 Matcher執行個體以編譯的正規表達式為基礎對目标字元串進行比對工作,多個Matcher是可以共用一個Pattern對象的。
Matcher m =proInfo.matcher(s);
ActuallyWeather actually= null;
ActuallyForecastWeather forecast= null;//使用find()方法查找第一個比對的對象
if(m.find()) {
actually= newActuallyWeather();
forecast= newActuallyForecastWeather();//傳回與組比對的子串内容
String[] ss = m.group(1).trim().replace(" ", "").split(">");if ("風力".equals(ss[0])) {
k++;
String[] strsss= s.split("
");int i = 0;if (k<=t&&j==0) {
actually= newActuallyWeather();for(String ss1 : strsss) {
Matcher mm=proInfo.matcher(ss1);if(mm.find()) {//設定産品型号
String[] sss = mm.group(1).trim().replace(" ", "").split(">");if (i%4==1) {
actually.setPlaceName(sss[0]);
}else if (i%4==2) {
actually.setWeatherStatus(sss[0]);
}else if (i%4==3) {
String temp= sss[0];
String[] temps= temp.split("/");
actually.setMaxTemperature(temps[0]+"℃");
actually.setMinTemperature(temps[1]);
}else if (i%4==0&&i!=0) {
actually.setWindPower(sss[0]);
}
}if (i%4==0&&i!=0) {
Date date= newDate();//釋出日期
actually.setPubTime(sdf.format(date));//前四子產品資料從當天早上七點開始加六個小時//date = new Date(date.getTime() + (k-1)*21600000);
int p = 0;//t是當日資料剩餘次數;k是目前循環次數
if ((t-k)==3) {
p= 6;
}else if ((t-k)==2) {
p= 12;
}else if ((t-k)==1) {
p= 18;
}else if ((t-k)==0) {
p= 24;
}//次日淩晨
if (24==p) {
Date time= new Date(date.getTime() + 86400000);
actually.setForecastTime(sdf.format(time)+" 00");
}else if (p<10) {
actually.setForecastTime(sdf.format(date)+" 0"+p);
}else if (p>10&&p!=24) {
actually.setForecastTime(sdf.format(date)+" "+p);
}
list_actually.add(actually);
actually=newActuallyWeather();
}
i++;
}
}else if (k>t&&j==1) {
forecast= newActuallyForecastWeather();for(String ss1 : strsss) {
Matcher mm=proInfo.matcher(ss1);if(mm.find()) {//設定産品型号
String[] sss = mm.group(1).trim().replace(" ", "").split(">");if (i%4==1) {
forecast.setPlaceName(sss[0]);
}else if (i%4==2) {
forecast.setWeatherStatus(sss[0]);
}else if (i%4==3) {
String temp= sss[0];
String[] temps= temp.split("/");
forecast.setMaxTemperature(temps[0]+"℃");
forecast.setMinTemperature(temps[1]);
}else if (i%4==0&&i!=0) {
forecast.setWindPower(sss[0]);
}
}if (i%4==0&&i!=0) {
Date date= newDate();//釋出日期
forecast.setPubTime(sdf.format(date));//從第五個子產品資料開始資料是從次日淩晨開始每下一組比上一組晚24小時
date = new Date(date.getTime() + (k-t)*86400000);
forecast.setForecastTime(sdf.format(date)+" 00");
list_forecast.add(forecast);
forecast=newActuallyForecastWeather();
}
i++;
}
}
}
}
}if (0==j) {returnlist_actually;
}else if (1==j) {returnlist_forecast;
}return null;
}
public static String getUrl(String countyCode) throwsException {
String url= "http://3g.nx121.com/pc/tqybxzb.aspx";if(StringUtils.isNotEmpty(countyCode)) {
url= url + "?sd="+countyCode;
}returnurl;
}
public static Map getDistrict(String countyCode) throwsException {
Map map = new HashMap();if ("53614".equals(countyCode)) {//市級行政區
map.put("cityLevel", "銀川市");//區縣級行政區
map.put("countyLevel", "市轄區");
}else if ("53610".equals(countyCode)) {//市級行政區
map.put("cityLevel", "銀川市");//區縣級行政區
map.put("countyLevel", "賀蘭縣");
}else if ("53618".equals(countyCode)) {//市級行政區
map.put("cityLevel", "銀川市");//區縣級行政區
map.put("countyLevel", "永甯縣");
}else if ("53619".equals(countyCode)) {//市級行政區
map.put("cityLevel", "銀川市");//區縣級行政區
map.put("countyLevel", "靈武市");
}returnmap;
}public staticPattern getProInfo() {returnproInfo;
}public static voidsetProInfo(Pattern proInfo) {
UrlInfo.proInfo=proInfo;
}public staticString getCountyCodes() {returncountyCodes;
}public static voidsetCountyCodes(String countyCodes) {
UrlInfo.countyCodes=countyCodes;
}
}