Search in sources :

Example 1 with Sakura

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

the class SakuraController method findAll.

@Transactional
@GetMapping(value = "/api/sakuras", produces = MEDIA_TYPE)
public String findAll(@RequestParam(name = "public", defaultValue = "true") boolean isPublic) {
    @SuppressWarnings("unchecked") List<Sakura> sakuras = dao.query(session -> {
        Criteria criteria = session.createCriteria(Sakura.class);
        if (isPublic) {
            criteria.add(Restrictions.ne("viewType", ViewType.PrivateList));
            criteria.add(Restrictions.eq("enabled", true));
        }
        return criteria.list();
    });
    JSONArray result = new JSONArray();
    sakuras.forEach(sakura -> {
        result.put(sakura.toJSON());
    });
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[获取多个列表成功][列表数量={}]", sakuras.size());
    }
    return objectResult(result);
}
Also used : JSONArray(org.json.JSONArray) Criteria(org.hibernate.Criteria) Sakura(mingzuozhibi.persist.disc.Sakura) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Sakura

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

the class SakuraController method dropDiscs.

@Transactional
@PreAuthorize("hasRole('BASIC')")
@DeleteMapping(value = "/api/sakuras/{id}/discs/{discId}", produces = MEDIA_TYPE)
public String dropDiscs(@PathVariable("id") Long id, @PathVariable("discId") Long discId, @RequestParam(name = "discColumns", defaultValue = DISC_COLUMNS) String discColumns) {
    Sakura sakura = dao.get(Sakura.class, id);
    if (sakura == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[从列表移除碟片失败][指定的列表Id不存在][Id={}]", id);
        }
        return errorMessage("指定的列表Id不存在");
    }
    Disc disc = sakura.getDiscs().stream().filter(d -> d.getId().equals(discId)).findFirst().orElse(null);
    if (disc == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[从列表移除碟片失败][指定的碟片不存在于列表][Id={}]", discId);
        }
        return errorMessage("指定的碟片不存在于列表");
    }
    sakura.getDiscs().remove(disc);
    if (LOGGER.isInfoEnabled()) {
        infoRequest("[从列表移除碟片成功][ASIN={}][列表={}]", disc.getAsin(), sakura.getTitle());
    }
    return objectResult(disc.toJSON(getColumns(discColumns)));
}
Also used : Disc(mingzuozhibi.persist.disc.Disc) Sakura(mingzuozhibi.persist.disc.Sakura) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Sakura

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

the class SakuraController method delOne.

@Transactional
@PreAuthorize("hasRole('ADMIN')")
@DeleteMapping(value = "/api/sakuras/{id}", produces = MEDIA_TYPE)
public String delOne(@PathVariable("id") Long id) {
    Sakura sakura = dao.get(Sakura.class, id);
    if (dao.get(Sakura.class, id) == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[删除列表失败][指定的列表Id不存在][Key={}]", id);
        }
        return errorMessage("指定的列表Id不存在");
    }
    JSONObject before = sakura.toJSON();
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[删除列表开始][删除前={}]", before);
    }
    sakura.getDiscs().clear();
    dao.delete(sakura);
    JSONObject result = sakura.toJSON();
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[删除列表成功][修改后={}]", result);
    }
    return objectResult(result);
}
Also used : JSONObject(org.json.JSONObject) Sakura(mingzuozhibi.persist.disc.Sakura) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Sakura

use of mingzuozhibi.persist.disc.Sakura 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 5 with Sakura

use of mingzuozhibi.persist.disc.Sakura 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)

Aggregations

Sakura (mingzuozhibi.persist.disc.Sakura)13 Transactional (org.springframework.transaction.annotation.Transactional)8 Disc (mingzuozhibi.persist.disc.Disc)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 JSONObject (org.json.JSONObject)5 LocalDateTime (java.time.LocalDateTime)4 UpdateType (mingzuozhibi.persist.disc.Disc.UpdateType)3 ViewType (mingzuozhibi.persist.disc.Sakura.ViewType)3 SakuraHelper.noExpiredSakura (mingzuozhibi.support.SakuraHelper.noExpiredSakura)3 LocalDate (java.time.LocalDate)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Dao (mingzuozhibi.support.Dao)2 Criteria (org.hibernate.Criteria)2 Restrictions (org.hibernate.criterion.Restrictions)2 JSONArray (org.json.JSONArray)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2