Search in sources :

Example 6 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class AmazonScheduler method startFullUpdate.

private void startFullUpdate() {
    LOGGER.info("[开始更新Amazon(ALL)数据]");
    updateFullUpdateTime();
    LinkedHashSet<Disc> discs = new LinkedHashSet<>();
    LinkedHashMap<String, Integer> results = new LinkedHashMap<>();
    dao.execute(session -> {
        findActiveSakura(session).forEach(sakura -> {
            findAmazonDiscs(sakura).filter(needUpdate()).forEach(discs::add);
        });
    });
    if (discs.size() > 0) {
        AtomicInteger updateCount = new AtomicInteger(discs.size());
        LOGGER.info("[正在更新Amazon(ALL)数据][共{}个]", discs.size());
        discs.stream().sorted().forEach(disc -> {
            service.createRankTask(disc.getAsin(), fullUpdateCB(discs, results, updateCount));
        });
    } else {
        LOGGER.info("[结束更新Amazon(ALL)数据][未找到可以更新的Amazon(ALL)数据]");
        amazonFetchStatus = AmazonFetchStatus.waitingForUpdate;
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disc(mingzuozhibi.persist.disc.Disc) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 7 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class AmazonScheduler method finishTheUpdate.

private void finishTheUpdate(Set<Disc> discs, Map<String, Integer> results) {
    LOGGER.info("[正在写入Amazon(ALL)数据]]");
    dao.execute(session -> {
        discs.forEach(disc -> {
            Integer rank = results.get(disc.getAsin());
            dao.refresh(disc);
            disc.setPrevRank(disc.getThisRank());
            if (rank != null) {
                disc.setThisRank(rank);
                disc.setUpdateTime(fullUpdateTime.get());
            }
            if (!Objects.equals(disc.getThisRank(), disc.getPrevRank())) {
                disc.setModifyTime(fullUpdateTime.get());
            }
        });
        dao.findAll(Sakura.class).stream().filter(sakura -> sakura.getViewType() != ViewType.SakuraList).forEach(sakura -> {
            sakura.setModifyTime(fullUpdateTime.get());
        });
    });
    LOGGER.info("[成功更新Amazon(ALL)数据]");
    service.infoStatus();
    amazonFetchStatus = AmazonFetchStatus.waitingForUpdate;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) java.util(java.util) Logger(org.slf4j.Logger) Restrictions(org.hibernate.criterion.Restrictions) Predicate(java.util.function.Predicate) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) Session(org.hibernate.Session) Autowired(org.springframework.beans.factory.annotation.Autowired) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) Disc(mingzuozhibi.persist.disc.Disc) AmazonTask(mingzuozhibi.service.amazon.AmazonTask) AmazonTaskService(mingzuozhibi.service.amazon.AmazonTaskService) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) Dao(mingzuozhibi.support.Dao) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ViewType(mingzuozhibi.persist.disc.Sakura.ViewType) UpdateType(mingzuozhibi.persist.disc.Disc.UpdateType) Sakura(mingzuozhibi.persist.disc.Sakura) DocumentReader(mingzuozhibi.service.amazon.DocumentReader) Sakura(mingzuozhibi.persist.disc.Sakura)

Example 8 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class SakuraSpeedSpider method updateSakuraDiscs.

private void updateSakuraDiscs(Sakura sakura, Stream<Element> tableRows) {
    Set<Disc> toAdd = new LinkedHashSet<>(sakura.getDiscs().size());
    boolean isTop100 = "9999-99".equals(sakura.getKey());
    tableRows.forEach(tr -> {
        String href = tr.child(5).child(0).attr("href");
        String asin = href.substring(href.length() - 10);
        Disc disc = getOrCreateDisc(asin, tr);
        if (disc.getUpdateType() == UpdateType.Both && !isTop100) {
            disc.setUpdateType(UpdateType.Sakura);
        }
        if (disc.getUpdateType() == UpdateType.Sakura) {
            String[] sakuraRank = tr.child(0).text().split("/");
            disc.setThisRank(parseInteger(sakuraRank[0]));
            disc.setPrevRank(parseInteger(sakuraRank[1]));
            disc.setTotalPt(parseInteger(tr.child(2).text()));
            disc.setNicoBook(parseInteger(tr.child(3).text()));
            disc.setUpdateTime(sakura.getModifyTime());
            if (!Objects.equals(disc.getThisRank(), disc.getPrevRank())) {
                disc.setModifyTime(sakura.getModifyTime());
            }
        }
        toAdd.add(disc);
    });
    if (isTop100) {
        sakura.setDiscs(new LinkedList<>(toAdd));
    } else {
        boolean noExpiredSakura = noExpiredSakura(sakura);
        sakura.getDiscs().stream().filter(disc -> {
            return disc.getUpdateType() != UpdateType.Sakura || noExpiredSakura;
        }).filter(disc -> !toAdd.contains(disc)).forEach(disc -> {
            if (disc.getUpdateType() == UpdateType.Sakura) {
                disc.setUpdateType(UpdateType.Both);
            }
            toAdd.add(disc);
        });
        sakura.setDiscs(toAdd.stream().sorted().collect(Collectors.toList()));
    }
    LOGGER.debug("成功更新[{}]列表", sakura.getTitle());
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) Util(mingzuozhibi.service.SakuraSpeedSpider.Util) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Disc(mingzuozhibi.persist.disc.Disc) SakuraHelper.noExpiredSakura(mingzuozhibi.support.SakuraHelper.noExpiredSakura) Stream(java.util.stream.Stream) Dao(mingzuozhibi.support.Dao) ViewType(mingzuozhibi.persist.disc.Sakura.ViewType) Service(org.springframework.stereotype.Service) UpdateType(mingzuozhibi.persist.disc.Disc.UpdateType) Document(org.jsoup.nodes.Document) Element(org.jsoup.nodes.Element) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Sakura(mingzuozhibi.persist.disc.Sakura) Jsoup(org.jsoup.Jsoup) Elements(org.jsoup.select.Elements) DiscType(mingzuozhibi.persist.disc.Disc.DiscType) Disc(mingzuozhibi.persist.disc.Disc)

Example 9 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class SakuraSpeedSpider method getOrCreateDisc.

private Disc getOrCreateDisc(String asin, Element tr) {
    Disc disc = dao.lookup(Disc.class, "asin", asin);
    if (disc == null) {
        String title = titleOfDisc(tr.child(5).text());
        String typeIcon = tr.child(1).text();
        String dateText = tr.child(4).text();
        LocalDate releaseDate = parseDate(fixYear(dateText));
        disc = new Disc(asin, title, parseDiscType(typeIcon), UpdateType.Sakura, isAmazonLimit(title), releaseDate);
        dao.save(disc);
        LOGGER.info("发现了新的碟片, title={}", disc.getTitle());
    }
    return disc;
}
Also used : Disc(mingzuozhibi.persist.disc.Disc) LocalDate(java.time.LocalDate)

Example 10 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class SakuraController method findDiscs.

@Transactional
@GetMapping(value = "/api/sakuras/key/{key}/discs", produces = MEDIA_TYPE)
public String findDiscs(@PathVariable String key, @RequestParam(defaultValue = DISC_COLUMNS) String discColumns, @RequestParam(name = "public", defaultValue = "true") boolean isPublic) {
    Sakura sakura = dao.lookup(Sakura.class, "key", key);
    if (sakura == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[获取列表碟片失败][指定的列表索引不存在][key={}]", key);
        }
        return errorMessage("指定的列表索引不存在");
    }
    JSONObject result = sakura.toJSON();
    JSONArray discs = new JSONArray();
    sakura.getDiscs().stream().filter(disc -> !isPublic || disc.getUpdateType() != UpdateType.None).forEach(disc -> discs.put(disc.toJSON(getColumns(discColumns))));
    result.put("discs", discs);
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[获取列表碟片成功][列表标题={}][碟片数量={}]", sakura.getTitle(), sakura.getDiscs().size());
    }
    return objectResult(result);
}
Also used : Criteria(org.hibernate.Criteria) Restrictions(org.hibernate.criterion.Restrictions) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Set(java.util.Set) Disc(mingzuozhibi.persist.disc.Disc) List(java.util.List) JSONObject(org.json.JSONObject) ViewType(mingzuozhibi.persist.disc.Sakura.ViewType) UpdateType(mingzuozhibi.persist.disc.Disc.UpdateType) Sakura(mingzuozhibi.persist.disc.Sakura) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) JsonArg(mingzuozhibi.support.JsonArg) DiscController.buildSet(mingzuozhibi.action.DiscController.buildSet) JSONArray(org.json.JSONArray) Transactional(org.springframework.transaction.annotation.Transactional) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Sakura(mingzuozhibi.persist.disc.Sakura) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Disc (mingzuozhibi.persist.disc.Disc)16 Transactional (org.springframework.transaction.annotation.Transactional)9 JSONObject (org.json.JSONObject)7 Sakura (mingzuozhibi.persist.disc.Sakura)6 LocalDate (java.time.LocalDate)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 LocalDateTime (java.time.LocalDateTime)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 UpdateType (mingzuozhibi.persist.disc.Disc.UpdateType)3 ViewType (mingzuozhibi.persist.disc.Sakura.ViewType)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 PutMapping (org.springframework.web.bind.annotation.PutMapping)3 java.util (java.util)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 DiscType (mingzuozhibi.persist.disc.Disc.DiscType)2 Dao (mingzuozhibi.support.Dao)2 Restrictions (org.hibernate.criterion.Restrictions)2 JSONArray (org.json.JSONArray)2