123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package com.sf;
- import com.sf.mapper.BookChapterMapper;
- import com.sf.mapper.BookContentMapper;
- import com.sf.mapper.BookInfoMapper;
- import com.sf.po.BookChapter;
- import com.sf.po.BookContent;
- import com.sf.po.BookInfo;
- import com.sf.util.EpubUtils;
- import com.sf.util.SpiderUtils;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.util.Pair;
- import java.time.LocalDateTime;
- import java.util.List;
- @SpringBootTest
- public class DBTests {
- @Autowired
- private BookInfoMapper bookInfoMapper;
- @Autowired
- private BookChapterMapper bookChapterMapper;
- @Autowired
- private BookContentMapper bookContentMapper;
- @Test
- public void test() {
- // select * from book_content where chapter_id in
- //(select id from book_chapter where book_id = '1431630596354977796')
- // book_info -> 获得book_id
- Pair<String, String> bookPair = Pair.of("长夜难明", """
- 该小说讲述了江阳、朱伟等人为了替侯贵平沉冤昭雪,十年来受到重重阻碍但不断收集案件相关证据,最终设局引发社会关注的故事。作者以弱势儿童群体的生存境遇为切口,用“性侵未成年少女”案件将矛头直指偏远地区法治落后的问题,反映出其对寻求司法公正和维护社会秩序的强烈期盼,折射出一个作家的社会责任感
- """);
- BookInfo bookInfo = toBookInfo(bookPair);
- bookInfoMapper.insert(bookInfo);
- // System.out.println(bookInfo.getId());
- List<Pair<String, String>> pairList = EpubUtils.getContent("epub/长夜难明.epub", "长夜难明");
- // try {
- // String url = "https://www.qidian.com/book/68223/";
- // List<Pair<String, String>> pairAnotherList = SpiderUtils.getContent(url);
- // System.out.println();
- // } catch (Exception e) {
- // throw new RuntimeException(e);
- // }
- // Long bookId = 1431630596354977799L;
- Long bookId = bookInfo.getId();
- int allWordCount = 0;
- long lastChapterId = 0;
- String lastChapterName = "";
- for (int i = 0; i < pairList.size(); i++) {
- Pair<String, String> pair = pairList.get(i);
- int wordCount = pair.getSecond().length();
- allWordCount += wordCount;
- BookChapter bookChapter = toBookChapter(bookId, i, pair.getFirst(), wordCount);
- bookChapterMapper.insert(bookChapter);
- BookContent bookContent = BookContent.builder()
- .chapterId(bookChapter.getId())
- .content(pair.getSecond())
- .createTime(LocalDateTime.now())
- .updateTime(LocalDateTime.now())
- .build();
- bookContentMapper.insert(bookContent);
- System.out.println();
- lastChapterId = bookChapter.getId();
- lastChapterName = pair.getFirst();
- }
- BookInfo newBookInfo = new BookInfo();
- newBookInfo.setId(bookId);
- newBookInfo.setWordCount(allWordCount);
- newBookInfo.setLastChapterId(lastChapterId);
- newBookInfo.setLastChapterName(lastChapterName);
- newBookInfo.setUpdateTime(LocalDateTime.now());
- bookInfoMapper.updateById(newBookInfo);
- System.out.println();
- }
- public BookInfo toBookInfo(Pair<String, String> pair) {
- BookInfo bookInfo = new BookInfo();
- // 设置作家信息
- bookInfo.setAuthorId(1L);
- bookInfo.setAuthorName("111");
- // 设置 出版物
- bookInfo.setWorkDirection((byte) 2);
- // 出版频道
- bookInfo.setCategoryId(8L);
- bookInfo.setCategoryName("出版频道");
- bookInfo.setBookName(pair.getFirst());
- bookInfo.setBookDesc(pair.getSecond());
- bookInfo.setPicUrl("/images/default.gif");
- bookInfo.setBookStatus((byte) 1);
- bookInfo.setIsVip((byte) 0);
- bookInfo.setScore((byte) 0);
- bookInfo.setCreateTime(LocalDateTime.now());
- bookInfo.setUpdateTime(LocalDateTime.now());
- return bookInfo;
- }
- public BookChapter toBookChapter(Long bookId, Integer chapterNum, String chapterName, Integer wordCount) {
- BookChapter bc = BookChapter.builder()
- .bookId(bookId)
- .chapterNum(chapterNum)
- .chapterName(chapterName)
- .wordCount(wordCount)
- .isVip((byte) 0)
- .createTime(LocalDateTime.now())
- .updateTime(LocalDateTime.now())
- .build();
- return bc;
- }
- }
|