private LotteryDraw nowPhaseResult() { String url = RESULT_LOCALITY_URL; LotteryDraw lotteryDraw = new LotteryDraw(); String data = null; String pageInfo = "结果页面" + url; String encoding = "utf-8"; String logHeader = "==" + lotteryScope + "==" + siteName + "==" + pageInfo + "==抓取==" + getLotteryType().getName() + "=="; try { data = CoreFetcherUtils.URLGet(url, null, encoding); } catch (Exception e) { logger.error("获取xml数据失败" + e.getMessage()); return null; } if (data == null || data.indexOf("404 Not Found") > 0 || data.isEmpty()) { logger.error(logHeader + "data is null or 404 Not Found"); return null; } List<LotteryDrawPrizeItem> lotteryDrawPrizeItemList = new ArrayList<LotteryDrawPrizeItem>(); SAXReader saxReader = new SAXReader(); Document document = DocumentFactory.getInstance().createDocument(); try { ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(encoding)); document = saxReader.read(bais); Iterator<?> it = document.getRootElement().elementIterator(); while (it.hasNext()) { Element element = (Element) it.next(); if (!element.getName().equals("po7oprize")) { continue; } Iterator<?> childIt = element.elementIterator(); Element childElement = null; String name = ""; String count = ""; String bonus = ""; while (childIt.hasNext()) { childElement = (Element) childIt.next(); if (childElement.getName().equals("code")) { lotteryDraw.setResult(childElement.getTextTrim()); } if (childElement.getName().equals("term")) { lotteryDraw.setPhase(childElement.getTextTrim()); lotteryDraw.setLotteryType(getLotteryType()); } if (childElement.getName().equals("drawOpenDate")) { lotteryDraw.setTimeDraw(childElement.getTextTrim() + " 00:00:00"); } if (childElement.getName().equals("drawSaleCount")) { lotteryDraw.setVolumeOfSales(childElement.getTextTrim().replace(",", "")); } if (childElement.getName().equals("drawPrizePoolCount")) { lotteryDraw.setJackpot(childElement.getTextTrim().replace(",", "")); } if (childElement.getName().equals("name")) { name = childElement.getTextTrim(); } if (childElement.getName().equals("count2")) { count = childElement.getTextTrim().replace(",", ""); } if (childElement.getName().equals("bonus")) { bonus = childElement.getTextTrim().replace(",", ""); } if (name != null && !"".equals(name) && bonus != null && !"".equals(bonus) && count != null && !"".equals(count)) { LotteryDrawPrizeItem lotteryDrawPrizeItem = new LotteryDrawPrizeItem(); lotteryDrawPrizeItem.setName(name); lotteryDrawPrizeItem.setWinningCount(count); lotteryDrawPrizeItem.setPrizeAmount(bonus); lotteryDrawPrizeItemList.add(lotteryDrawPrizeItem); name = ""; bonus = ""; count = ""; } } } lotteryDraw.setResultDetail(lotteryDrawPrizeItemList); } catch (Exception e) { logger.error("数据解析错误==" + e.getMessage(), e); return null; } return lotteryDraw; }
@Override public LotteryDraw fetchResultDetail(String phase) { LotteryDraw lotteryDraw = null; lotteryDraw = nowPhaseResult(); if (phase == null || "".equals(phase) || lotteryDraw.getPhase().equals(phase)) { return lotteryDraw; } else { lotteryDraw = null; } String url = RESULT_MORE_LOCALITY_URL; String data = null; String pageInfo = "结果页面" + url; String encoding = "utf-8"; String logHeader = "==" + lotteryScope + "==" + siteName + "==" + pageInfo + "==抓取==" + getLotteryType().getName() + "=="; try { data = CoreFetcherUtils.URLGet(url, null, encoding); } catch (Exception e) { logger.error("获取html数据失败" + e.getMessage()); return null; } if (data == null || data.indexOf("404 Not Found") > 0 || data.isEmpty()) { logger.error(logHeader + "data is null or 404 Not Found"); return null; } Parser parser = null; try { parser = Parser.createParser(data, encoding); } catch (Exception e) { logger.error("解析html页面失败" + e.getMessage()); return null; } NodeFilter filter = new HasAttributeFilter("class", "mytable"); NodeList nodeList = null; try { nodeList = parser.extractAllNodesThatMatch(filter); TableTag tableTag = (TableTag) nodeList.elementAt(0); TableRow[] tableRows = tableTag.getRows(); for (int i = 1; i < tableRows.length; i++) { TableColumn[] tableColumns = tableRows[i].getColumns(); String phaseTmp = tableColumns[0].toPlainTextString(); if (phaseTmp != null && !"".equals(phaseTmp) && phase.equals(phaseTmp)) { lotteryDraw = new LotteryDraw(); // 彩期 lotteryDraw.setPhase(phaseTmp); // 开奖结果 String strResult = tableColumns[1].toPlainTextString(); strResult = strResult.trim().replace(" ", ","); lotteryDraw.setResult(strResult); // 彩种 lotteryDraw.setLotteryType(super.getLotteryType()); break; } } } catch (ParserException e) { logger.error("数据解析错误==" + e.getMessage(), e); return null; } return lotteryDraw; }
@Override public List<JclqScheduleItem> fetchJclqSchedule(String officialDate) throws FetchFailedException { Map<String, String> headerParams = new HashMap<String, String>(); headerParams.put("Referer", "http://info.sporttery.cn"); headerParams.put( "User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19"); List<JclqScheduleItem> jclqScheduleItemList = new ArrayList<JclqScheduleItem>(); String encoding = "gbk"; JclqScheduleItem jclqScheduleItem = null; String siteName = "中国竞彩网网[竞彩篮球赛程]"; String logHeader = siteName + SCHEDULE_URL; try { String webInfo = CoreFetcherUtils.URLGetWithHeaderParams(SCHEDULE_URL, headerParams, null, encoding); if (webInfo == null || webInfo.indexOf("404 Not Found") > 0) { logger.error(logHeader + ",data is null or 404 Not Found"); throw new FetchFailedException("404 Not Found"); } Parser parser = Parser.createParser(webInfo, encoding); NodeList nodeList = parser.extractAllNodesThatMatch(new CssSelectorNodeFilter("div[class='box-tbl']")); if (null != nodeList && nodeList.size() > 0) { NodeFilter tableFilter = new TagNameFilter("table"); Parser parser2 = Parser.createParser(nodeList.toHtml(), encoding); NodeList tableNodeList = parser2.extractAllNodesThatMatch(tableFilter); if (tableNodeList != null && tableNodeList.size() > 0) { TableTag catchTableTag = new TableTag(); catchTableTag = (TableTag) tableNodeList.elementAt(0); if (catchTableTag != null) { TableRow[] catchRows = catchTableTag.getRows(); TableColumn[] catchColumns = null; for (int i = 2; i < catchRows.length; i++) { catchColumns = catchRows[i].getColumns(); if (catchColumns != null && catchColumns.length >= 6) { jclqScheduleItem = new JclqScheduleItem(); String officialNum = catchColumns[0].toPlainTextString().trim(); if (officialNum.length() < 5) { continue; } // 先解析比赛时间 String matchDateStr = catchColumns[3].toPlainTextString().trim(); String[] yearStr = matchDateStr.split("-"); if (yearStr[0].length() <= 2) { matchDateStr = "20" + matchDateStr + ":00"; } else { matchDateStr = matchDateStr + ":00"; } Date matchDate = CoreDateUtils.parseDate(matchDateStr, CoreDateUtils.DATETIME); Calendar matchDateCalendar = Calendar.getInstance(); matchDateCalendar.setTime(matchDate); matchDateCalendar.add(Calendar.MINUTE, 1); jclqScheduleItem.setMatchDate(matchDateCalendar.getTime()); // 根据周几、当前时间和比赛时间计算官方发布的日期 Calendar cd = Calendar.getInstance(); // 将时分秒等区域清零 cd.set(Calendar.HOUR_OF_DAY, 0); cd.set(Calendar.MINUTE, 0); cd.set(Calendar.SECOND, 0); cd.set(Calendar.MILLISECOND, 0); int nowWeekDay = cd.get(Calendar.DAY_OF_WEEK); int fetchWeekDay = weekDay.get(officialNum.substring(0, 2)); if (nowWeekDay != fetchWeekDay) { int m = fetchWeekDay - nowWeekDay; if (m < -1) { cd.add(Calendar.DATE, m + 7); } else { cd.add(Calendar.DATE, m); } } // 如果计算出来的日期超过了比赛时间,减去一周 if (cd.after(matchDateCalendar)) { cd.add(Calendar.DATE, -7); } // 如果计算出来的日期距离比赛时间相隔超过一周,加上一周的倍数 // 一周的毫秒数 long weekTimeInMillis = 3600 * 1000 * 24 * 7; long diffTimeInMillis = matchDateCalendar.getTimeInMillis() - cd.getTimeInMillis(); if (diffTimeInMillis > weekTimeInMillis) { // 计算相差几周 int diffWeekCount = (int) (diffTimeInMillis / weekTimeInMillis); cd.add(Calendar.DATE, 7 * diffWeekCount); } jclqScheduleItem.setMatchNum( CoreDateUtils.formatDate(cd.getTime(), "yyyyMMdd") + LotteryConstant.JCLQ_MATCH_NUM_CODE_DEFAULT + officialNum.substring(2)); jclqScheduleItem.setOfficialDate( CoreDateUtils.parseDate(CoreDateUtils.formatDate(cd.getTime()))); Integer oNum = null; try { oNum = Integer.valueOf(officialNum.substring(2)); } catch (Exception e) { logger.error("截取官方编码时,转换为Integer错误", e); } jclqScheduleItem.setOfficialNum(oNum); jclqScheduleItem.setMatchName( JclqUtil.convertMatchName( catchColumns[1].toPlainTextString().trim(), LotteryType.JCLQ_SF, FetcherType.T_PENGINEAPI)); String team = catchColumns[2].toPlainTextString().trim(); String[] teamStr = team.split("VS"); jclqScheduleItem.setAwayTeam(teamStr[0].trim()); jclqScheduleItem.setHomeTeam(teamStr[1].trim()); if ("已开售".equals(catchColumns[4].toPlainTextString().trim())) { jclqScheduleItem.setStatus(JclqRaceStatus.OPEN); } else { jclqScheduleItem.setStatus(JclqRaceStatus.UNOPEN); } if (catchColumns[5].toPlainTextString().trim().indexOf("胜负单关") > 0) { jclqScheduleItem.setDynamicSaleSfStatus(JclqDynamicSaleStatus.SALE_UNOPEN); } else { jclqScheduleItem.setDynamicSaleSfStatus(JclqDynamicSaleStatus.SALE_OPEN); } if (catchColumns[5].toPlainTextString().trim().indexOf("胜负过关") > 0) { jclqScheduleItem.setStaticSaleSfStatus(JclqStaticSaleStatus.SALE_UNOPEN); } else { jclqScheduleItem.setStaticSaleSfStatus(JclqStaticSaleStatus.SALE_OPEN); } if (catchColumns[5].toPlainTextString().trim().indexOf("让分胜负单关") > 0) { jclqScheduleItem.setDynamicSaleRfsfStatus(JclqDynamicSaleStatus.SALE_UNOPEN); } else { jclqScheduleItem.setDynamicSaleRfsfStatus(JclqDynamicSaleStatus.SALE_OPEN); } if (catchColumns[5].toPlainTextString().trim().indexOf("让分胜负过关") > 0) { jclqScheduleItem.setStaticSaleRfsfStatus(JclqStaticSaleStatus.SALE_UNOPEN); } else { jclqScheduleItem.setStaticSaleRfsfStatus(JclqStaticSaleStatus.SALE_OPEN); } if (catchColumns[5].toPlainTextString().trim().indexOf("胜分差单关") > 0) { jclqScheduleItem.setDynamicSaleSfcStatus(JclqDynamicSaleStatus.SALE_UNOPEN); } else { jclqScheduleItem.setDynamicSaleSfcStatus(JclqDynamicSaleStatus.SALE_OPEN); } if (catchColumns[5].toPlainTextString().trim().indexOf("胜分差过关") > 0) { jclqScheduleItem.setStaticSaleSfcStatus(JclqStaticSaleStatus.SALE_UNOPEN); } else { jclqScheduleItem.setStaticSaleSfcStatus(JclqStaticSaleStatus.SALE_OPEN); } if (catchColumns[5].toPlainTextString().trim().indexOf("大小分单关") > 0) { jclqScheduleItem.setDynamicSaleDxfStatus(JclqDynamicSaleStatus.SALE_UNOPEN); } else { jclqScheduleItem.setDynamicSaleDxfStatus(JclqDynamicSaleStatus.SALE_OPEN); } if (catchColumns[5].toPlainTextString().trim().indexOf("大小分过关") > 0) { jclqScheduleItem.setStaticSaleDxfStatus(JclqStaticSaleStatus.SALE_UNOPEN); } else { jclqScheduleItem.setStaticSaleDxfStatus(JclqStaticSaleStatus.SALE_OPEN); } jclqScheduleItemList.add(jclqScheduleItem); } } // end for catchRows } // end if catchTableTag!=null } // end if(tableNodeList!=null&&tableNodeList.size()>0) } else { logger.error(logHeader + "竞彩篮球赛程数据表格不存在,返回null"); throw new FetchFailedException("竞彩篮球赛程数据表格不存在"); } } catch (Exception e) { logger.error(logHeader + "竞彩篮球赛程错误" + e.getMessage(), e); throw new FetchFailedException(e.getMessage()); } return jclqScheduleItemList; }