private void scan(Date now, Scan s) throws NbaException { if (startScan(s.getId())) { debug(s.toString()); if (s.isInit()) { initScan(s); } if (s.getLastScanTime() == null || now.compareTo( new Date( s.getLastScanTime().getTime() + TimeUnit.SECONDS.toMillis(s.getScanIntervalSec()))) > 0) { URI url = null; try { url = new URI(s.getUrl()); } catch (URISyntaxException e) { throw new NbaException(ExType.ER_0000, e); } String responce = getUrlContentAsString(url, connectTimeout, socketTimeout, utf_8); debug(responce); String nbaData = substringMessage(responce, s.getParseBegin(), s.getParseEnd()); debug(nbaData); List<String> seasons = getNbaSeasons(nbaData, "ZA÷", "¬~"); parse.test(s, nbaData, seasons); ScanTypeEnum scanType = ScanTypeEnum.valueOf(s.getType()); Pageable topOne = new PageRequest(0, 1); Page<Match> page = matchRepository.findOneByLeagueAndSeasonAndFinishedOrderByDateDesc( s.getLeague(), s.getSeason(), scanType.isFinished(), topOne); String lastPageId = page.getContent().get(0).getUrlPageId(); if (lastPageId != null) { s.setLastScanId(lastPageId); s.setLastScanTime(now); s.setInit(false); s.setScanIntervalSec((int) TimeUnit.HOURS.toSeconds(12)); scanRepository.save(s); } } stopScan(s.getId()); } }
@Transactional public void initCascadeDB() { // int i = 0; System.out.println("init"); for (SeasonEnum se : SeasonEnum.values()) { System.out.println(se); GameType gt = null; LeagueEnum le = se.getLeague(); League l = leagueRepository.findLeagueByName(le.getValue()); if (l != null) { } else { GameTypeEnum gte = le.getGameType(); gt = gameTypeRepository.findByType(gte.getType()); if (gt == null) { gt = new GameType(); gt.setType(gte.getType()); gt.setDesc(gte.getDesc()); gt = gameTypeRepository.save(gt); } CountryEnum ce = le.getCountry(); Country c = countryRepository.findCountryByName(ce.getValue()); if (c == null) { c = new Country(); c.setCode(ce.getCode()); c.setName(ce.getValue()); c = countryRepository.save(c); } l = new League(); l.setCountry(c); l.setName(le.getValue()); l.setType(gt); if (gt.getId() != null) { l.setType(gameTypeRepository.findOne(gt.getId())); } l = leagueRepository.save(l); } Season season = seasonRepository.findOneByName(se.getName()); if (season == null) { season = new Season(); // seasonRepository.f season.setName(se.getName()); season.setDesc(se.getName()); season.setResultsUrl(se.getResultsUrl()); season.setDetailsUrl(se.getDetailsUrl()); // System.out.println(season); season = seasonRepository.save(season); } // String name = "Los Angeles Clippers"; // Team t1 = teamRepository.findOneByName(name); // if (t1 == null) { // t1 = new Team(); // t1.setName(name); // teamRepository.save(t1); // } // name = "Detroit Pistons"; // Team t2 = teamRepository.findOneByName(name); // if (t2 == null) { // t2 = new Team(); // t2.setName(name); // teamRepository.save(t2); // } // String pageId = "id-1"; // Match m = matchRepository.findOneByUrlPageId(pageId); // if (m == null) { // m = new Match(); // m.setUrlPageId("id-1"); // m.setAway(t1); // m.setHome(t2); // m.setLeague(l); // m.setSeason(season); // m.setHomeScore(10); // m.setAwayScore(23); // m.setDate(new Date()); // matchRepository.save(m); // } Scan sc = scanRepository.findOneByLeagueAndSeasonAndType(l, season, se.getScanType().name()); if (sc == null) { sc = new Scan(); sc.setLeague(l); sc.setSeason(season); sc.setUrl(se.getResultsUrl()); sc.setParseBegin(se.getParseBegin()); sc.setParseEnd(se.getParseEnd()); sc.setType(se.getScanType().name()); sc.setInit(se.isInit()); sc.setInitCount(se.getInitCount()); sc.setInitFileName(se.getInitFileName()); sc.setInitFilePath(se.getInitFilePath()); scanRepository.save(sc); System.err.println("save"); } } }