Search in sources :

Example 1 with HunterPrintWriter

use of me.zhyd.hunter.util.HunterPrintWriter in project OneBlog by zhangyd-c.

the class RemoverServiceImpl method run.

@Transactional(rollbackFor = Exception.class)
@Override
public void run(Long typeId, @Validated HunterConfig config, PrintWriter writer) {
    HunterPrintWriter writerUtil = new HunterPrintWriter(writer);
    long start = System.currentTimeMillis();
    if (null == typeId) {
        writerUtil.print("校验不通过!请选择文章分类......", String.format("共耗时 %s ms.", (System.currentTimeMillis() - start)));
        writerUtil.shutdown();
        return;
    }
    HunterProcessor hunter = new BlogHunterProcessor(config, writerUtil, String.valueOf(SessionUtil.getUser().getId()));
    CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
    if (CollectionUtils.isEmpty(list)) {
        writerUtil.print(String.format("未抓取到任何内容,请确保连接[<a href=\"%s\" target=\"_blank\">%s</a>]是否正确并能正常访问!共耗时 %s ms.", config.getEntryUrls().get(0), config.getEntryUrls().get(0), (System.currentTimeMillis() - start))).shutdown();
        return;
    }
    writerUtil.print("Congratulation ! 此次共整理到" + list.size() + "篇文章");
    saveArticles(typeId, config, writerUtil, list);
    writerUtil.print(String.format("搬家完成!耗时 %s ms.", (System.currentTimeMillis() - start)));
    writerUtil.shutdown();
}
Also used : VirtualArticle(me.zhyd.hunter.entity.VirtualArticle) HunterPrintWriter(me.zhyd.hunter.util.HunterPrintWriter) BlogHunterProcessor(me.zhyd.hunter.processor.BlogHunterProcessor) HunterProcessor(me.zhyd.hunter.processor.HunterProcessor) BlogHunterProcessor(me.zhyd.hunter.processor.BlogHunterProcessor) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with HunterPrintWriter

use of me.zhyd.hunter.util.HunterPrintWriter in project OneBlog by zhangyd-c.

the class RemoverServiceImpl method crawlSingle.

@Transactional(rollbackFor = Exception.class)
@Override
public void crawlSingle(Long typeId, String[] urls, boolean convertImg, PrintWriter writer) {
    HunterPrintWriter writerUtil = new HunterPrintWriter(writer);
    for (String url : urls) {
        HunterProcessor hunter = new BlogHunterProcessor(url, convertImg, writerUtil);
        CopyOnWriteArrayList<VirtualArticle> list = hunter.execute();
        this.saveArticles(typeId, hunter.getConfig(), writerUtil, list);
    }
    writerUtil.shutdown();
}
Also used : VirtualArticle(me.zhyd.hunter.entity.VirtualArticle) HunterPrintWriter(me.zhyd.hunter.util.HunterPrintWriter) BlogHunterProcessor(me.zhyd.hunter.processor.BlogHunterProcessor) HunterProcessor(me.zhyd.hunter.processor.HunterProcessor) BlogHunterProcessor(me.zhyd.hunter.processor.BlogHunterProcessor) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with HunterPrintWriter

use of me.zhyd.hunter.util.HunterPrintWriter in project OneBlog by zhangyd-c.

the class RemoverServiceImpl method saveArticles.

private void saveArticles(Long typeId, HunterConfig config, HunterPrintWriter writerUtil, CopyOnWriteArrayList<VirtualArticle> list) {
    // 获取数据库中的标签列表
    List<Tags> tags = tagsService.listAll();
    Map<String, Long> originalTags = tags.stream().collect(Collectors.toMap(tag -> tag.getName().toUpperCase(), Tags::getId));
    User user = SessionUtil.getUser();
    // 添加文章到数据库
    Article article = null;
    for (VirtualArticle spiderVirtualArticle : list) {
        article = this.saveArticle(typeId, config.isConvertImg(), writerUtil, user, spiderVirtualArticle);
        this.saveTags(writerUtil, originalTags, article, spiderVirtualArticle);
    }
}
Also used : ArticleStatusEnum(com.zyd.blog.business.enums.ArticleStatusEnum) java.util(java.util) User(com.zyd.blog.business.entity.User) Autowired(org.springframework.beans.factory.annotation.Autowired) Hunter(me.zhyd.hunter.Hunter) HunterConfig(me.zhyd.hunter.config.HunterConfig) Service(org.springframework.stereotype.Service) BizTagsService(com.zyd.blog.business.service.BizTagsService) BizArticleService(com.zyd.blog.business.service.BizArticleService) PrintWriter(java.io.PrintWriter) Validated(org.springframework.validation.annotation.Validated) SessionUtil(com.zyd.blog.util.SessionUtil) ImageLink(me.zhyd.hunter.entity.ImageLink) HunterPrintWriter(me.zhyd.hunter.util.HunterPrintWriter) BlogHunterProcessor(me.zhyd.hunter.processor.BlogHunterProcessor) Article(com.zyd.blog.business.entity.Article) Collectors(java.util.stream.Collectors) VirtualArticle(me.zhyd.hunter.entity.VirtualArticle) BizArticleTagsService(com.zyd.blog.business.service.BizArticleTagsService) RemoverService(com.zyd.blog.business.service.RemoverService) Tags(com.zyd.blog.business.entity.Tags) ImageDownloadUtil(com.zyd.blog.business.util.ImageDownloadUtil) CollectionUtils(org.springframework.util.CollectionUtils) HunterProcessor(me.zhyd.hunter.processor.HunterProcessor) Transactional(org.springframework.transaction.annotation.Transactional) StringUtils(org.springframework.util.StringUtils) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) VirtualArticle(me.zhyd.hunter.entity.VirtualArticle) User(com.zyd.blog.business.entity.User) Article(com.zyd.blog.business.entity.Article) VirtualArticle(me.zhyd.hunter.entity.VirtualArticle) Tags(com.zyd.blog.business.entity.Tags)

Aggregations

VirtualArticle (me.zhyd.hunter.entity.VirtualArticle)3 BlogHunterProcessor (me.zhyd.hunter.processor.BlogHunterProcessor)3 HunterProcessor (me.zhyd.hunter.processor.HunterProcessor)3 HunterPrintWriter (me.zhyd.hunter.util.HunterPrintWriter)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Article (com.zyd.blog.business.entity.Article)1 Tags (com.zyd.blog.business.entity.Tags)1 User (com.zyd.blog.business.entity.User)1 ArticleStatusEnum (com.zyd.blog.business.enums.ArticleStatusEnum)1 BizArticleService (com.zyd.blog.business.service.BizArticleService)1 BizArticleTagsService (com.zyd.blog.business.service.BizArticleTagsService)1 BizTagsService (com.zyd.blog.business.service.BizTagsService)1 RemoverService (com.zyd.blog.business.service.RemoverService)1 ImageDownloadUtil (com.zyd.blog.business.util.ImageDownloadUtil)1 SessionUtil (com.zyd.blog.util.SessionUtil)1 PrintWriter (java.io.PrintWriter)1 java.util (java.util)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Collectors (java.util.stream.Collectors)1 Hunter (me.zhyd.hunter.Hunter)1