diff --git a/novel-admin/src/main/java/com/java2nb/books/service/impl/BookCrawlServiceImpl.java b/novel-admin/src/main/java/com/java2nb/books/service/impl/BookCrawlServiceImpl.java index 425bd6d..bc6ad05 100644 --- a/novel-admin/src/main/java/com/java2nb/books/service/impl/BookCrawlServiceImpl.java +++ b/novel-admin/src/main/java/com/java2nb/books/service/impl/BookCrawlServiceImpl.java @@ -26,624 +26,877 @@ import org.springframework.web.client.RestTemplate; @Service public class BookCrawlServiceImpl implements BookCrawlService { - private boolean isInteruptBiquDaoCrawl;//是否中断笔趣岛爬虫程序 + private boolean isInteruptBiquDaoCrawl;//是否中断笔趣岛爬虫程序 - private boolean isInteruptBiquTaCrawl;//是否中断笔趣塔爬虫程序 + private boolean isInteruptBiquTaCrawl;//是否中断笔趣塔爬虫程序 - private RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8"); + private RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8"); - @Autowired - private BookCrawlDao bookCrawlDao; + @Autowired + private BookCrawlDao bookCrawlDao; - @Autowired - private BookDao bookDao; + @Autowired + private BookDao bookDao; - @Autowired - private BookIndexDao bookIndexDao; + @Autowired + private BookIndexDao bookIndexDao; - @Autowired - private BookContentDao bookContentDao; - - @Override - public BookCrawlDO get(Long id){ - return bookCrawlDao.get(id); - } - - @Override - public List list(Map map){ - return bookCrawlDao.list(map); - } - - @Override - public int count(Map map){ - return bookCrawlDao.count(map); - } - - @Override - public int save(BookCrawlDO bookCrawl){ - return bookCrawlDao.save(bookCrawl); - } - - @Override - public int update(BookCrawlDO bookCrawl){ - return bookCrawlDao.update(bookCrawl); - } - - @Override - public int remove(Long id){ - return bookCrawlDao.remove(id); - } - - @Override - public int batchRemove(Long[] ids){ - return bookCrawlDao.batchRemove(ids); - } + @Autowired + private BookContentDao bookContentDao; - @Override - public void updateStatus(BookCrawlDO bookCrawl) { - bookCrawlDao.update(bookCrawl); + @Override + public BookCrawlDO get(Long id) { + return bookCrawlDao.get(id); + } - if(bookCrawl.getStatus() == 0){ - switch (bookCrawl.getCrawlWebCode()) { - case 1: { - isInteruptBiquDaoCrawl = true; - break; - } - case 2: { - isInteruptBiquTaCrawl = true; - break; - } - } - }else{ - crawlBook(bookCrawl.getCrawlWebCode()); - } + @Override + public List list(Map map) { + return bookCrawlDao.list(map); + } + + @Override + public int count(Map map) { + return bookCrawlDao.count(map); + } + + @Override + public int save(BookCrawlDO bookCrawl) { + return bookCrawlDao.save(bookCrawl); + } + + @Override + public int update(BookCrawlDO bookCrawl) { + return bookCrawlDao.update(bookCrawl); + } + + @Override + public int remove(Long id) { + return bookCrawlDao.remove(id); + } + + @Override + public int batchRemove(Long[] ids) { + return bookCrawlDao.batchRemove(ids); + } + + @Override + public void updateStatus(BookCrawlDO bookCrawl) { + bookCrawlDao.update(bookCrawl); + + if (bookCrawl.getStatus() == 0) { + switch (bookCrawl.getCrawlWebCode()) { + case 1: { + isInteruptBiquDaoCrawl = true; + break; + } + case 2: { + isInteruptBiquTaCrawl = true; + break; + } + } + } else { + crawlBook(bookCrawl); + } - } + } - private void crawlBook(int status){ - for (int i = 1; i <= 7; i++) { + private void crawlBook(BookCrawlDO bookCrawl) { + for (int i = 1; i <= 7; i++) { - int finalI = i; - new Thread( - () -> { + int finalI = i; + new Thread( + () -> { - try { - switch (status) { - case 1: { - crawBiqudaoBooks(finalI); - break; - } - case 2: { - crawBiquTaBooks(finalI); - break; - } - } - } catch (Exception e) { - e.printStackTrace(); - } + try { - } - ).start(); - } + switch (bookCrawl.getCrawlWebCode()) { + case 1: { + while (true) { + if (isInteruptBiquDaoCrawl) { + return; + } + crawBiqudaoBooks(finalI); + Thread.sleep(1000 * 60 * 60 * 24); + } + } + case 2: { + while (true) { + if (isInteruptBiquTaCrawl) { + return; + } + crawBiquTaBooks(finalI); + Thread.sleep(1000 * 60 * 60 * 24); + } + } - } + } + } catch (Exception e) { + e.printStackTrace(); + bookCrawl.setStatus(0); + bookCrawlDao.update(bookCrawl); + } - private void crawBiquTaBooks(int i) { - String baseUrl = "https://m.biquta.com"; - String catBookListUrlBase = baseUrl + "/class/"; - //拼接分类URL - int page = 1;//起始页码 - int totalPage = page; - String catBookListUrl = catBookListUrlBase + i + "/" + page + ".html"; - String forObject = getByHttpClient(catBookListUrl); - if (forObject != null) { - //匹配分页数 - Pattern pattern = Pattern.compile("value=\"(\\d+)/(\\d+)\""); - Matcher matcher = pattern.matcher(forObject); - boolean isFind = matcher.find(); - System.out.println("匹配分页数" + isFind); - if (isFind) { - int currentPage = Integer.parseInt(matcher.group(1)); - totalPage = Integer.parseInt(matcher.group(2)); - //解析第一页书籍的数据 - Pattern bookPatten = Pattern.compile("href=\"/(\\d+_\\d+)/\""); - parseBiquTaBook(bookPatten, forObject, i, baseUrl); - while (currentPage < totalPage) { - if(isInteruptBiquTaCrawl){ - return; - } - - catBookListUrl = catBookListUrlBase + i + "/" + (currentPage + 1) + ".html"; - forObject = getByHttpClient(catBookListUrl); - if (forObject != null) { - //匹配分页数 - matcher = pattern.matcher(forObject); - isFind = matcher.find(); - - if (isFind) { - currentPage = Integer.parseInt(matcher.group(1)); - totalPage = Integer.parseInt(matcher.group(2)); - parseBiquTaBook(bookPatten, forObject, i, baseUrl); - } - } else { - currentPage++; - } - } - } - } - } - - private void parseBiquTaBook(Pattern bookPatten, String forObject, int catNum, String baseUrl) { - Matcher matcher2 = bookPatten.matcher(forObject); - boolean isFind = matcher2.find(); - Pattern scorePatten = Pattern.compile("(\\d+\\.\\d+)分"); - Matcher scoreMatch = scorePatten.matcher(forObject); - boolean scoreFind = scoreMatch.find(); - - Pattern bookNamePatten = Pattern.compile("

([^/]+)

"); - Matcher bookNameMatch = bookNamePatten.matcher(forObject); - boolean isBookNameMatch = bookNameMatch.find(); - - Pattern authorPatten = Pattern.compile(">作者:([^/]+)<"); - Matcher authoreMatch = authorPatten.matcher(forObject); - boolean isFindAuthor = authoreMatch.find(); + } + ).start(); + } - System.out.println("匹配书籍url" + isFind); - System.out.println("匹配分数" + scoreFind); - while (isFind && scoreFind && isBookNameMatch && isFindAuthor) { - if(isInteruptBiquTaCrawl){ - return; - } + for (int j = 21; j <= 29; j++) { + int finalJ = j; + new Thread(() -> { - try { - Float score = Float.parseFloat(scoreMatch.group(1)); + for (int i = 1; i <= 499; i++) { + if(isInteruptBiquTaCrawl || isInteruptBiquDaoCrawl){ + return; + } + System.out.println("==============分类============:" + finalJ); + System.out.println("==============页码============:" + i); + int catId = finalJ; + int page = i; + + String bookListUrl = "http://book.sfacg.com/List/default.aspx?&tid=" + catId + "&if=1&PageIndex=" + page; + + String forObject = getByHttpClient(bookListUrl); + + if (forObject != null) { + Pattern bookPatten = Pattern.compile("href=\"/Novel/(\\d+)/\""); + Matcher bookMatcher = bookPatten.matcher(forObject); + boolean isFindBook = bookMatcher.find(); + + while (isFindBook) { + try { + if(isInteruptBiquTaCrawl || isInteruptBiquDaoCrawl){ + return; + } + long bookNum = Long.parseLong(bookMatcher.group(1)); + String bookUrl = "http://book.sfacg.com/Novel/" + bookNum; + String forObject1 = getByHttpClient(bookUrl); + if (forObject1 != null) { + Pattern updateTimePatten = Pattern.compile("更新:(\\d+/\\d+/\\d+ \\d+:\\d+:\\d+)"); + Matcher updateTimeMatch = updateTimePatten.matcher(forObject1); + boolean isFindUpdateTime = updateTimeMatch.find(); + if (isFindUpdateTime) { + String updateTimeStr = updateTimeMatch.group(1); + String dateStr = updateTimeStr; + int firstPos = dateStr.indexOf("/"); + String year = dateStr.substring(0, firstPos); + dateStr = dateStr.substring(firstPos + 1); + firstPos = dateStr.indexOf("/"); + String month = dateStr.substring(0, firstPos); + dateStr = dateStr.substring(firstPos + 1); + firstPos = dateStr.indexOf(" "); + String day = dateStr.substring(0, firstPos); + dateStr = dateStr.substring(firstPos + 1); + firstPos = dateStr.indexOf(":"); + String hour = dateStr.substring(0, firstPos); + dateStr = dateStr.substring(firstPos + 1); + firstPos = dateStr.indexOf(":"); + String minus = dateStr.substring(0, firstPos); + String second = dateStr.substring(firstPos + 1); + if (month.length() == 1) { + month = "0" + month; + } + if (day.length() == 1) { + day = "0" + day; + } + if (hour.length() == 1) { + hour = "0" + hour; + } + if (minus.length() == 1) { + minus = "0" + minus; + } + if (second.length() == 1) { + second = "0" + second; + } + + + Date updateTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(updateTimeStr); + + + //Date updateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(year+"-"+month+"-"+hour+" "+minus+" "+minus); + Pattern bookNamePatten = Pattern.compile("

\\s*" + + "([^<]+)\\s*" + + "\\s*" + + "([^<]+)\\s*" + + ""); + Matcher authorMatcher = authorPatten.matcher(forObject1); + boolean isFindAuthor = authorMatcher.find(); + if (isFindAuthor) { + String author = authorMatcher.group(1); + + Pattern picPtten = Pattern.compile("src=\"(http://rs.sfacg.com/web/novel/images/NovelCover/Big/[^\"]+)\""); + Matcher picMatcher = picPtten.matcher(forObject1); + if (picMatcher.find()) { + String pic = picMatcher.group(1); + + Pattern visitPatten = Pattern.compile(">点击:(\\d+)<"); + Matcher visitMatcher = visitPatten.matcher(forObject1); + boolean isFindVisit = visitMatcher.find(); + if (isFindVisit) { + String visit = visitMatcher.group(1); + + Pattern statusPatten = Pattern.compile(">字数:\\d+字\\[([^<]+)\\]<"); + Matcher statusMatcher = statusPatten.matcher(forObject1); + boolean isFindStatus = statusMatcher.find(); + if (isFindStatus) { + String status = statusMatcher.group(1); + + if ("已完结".equals(status)) {//先爬已完结的 + + status = "已完成"; + } + + + Pattern scorePatten = Pattern.compile("
\\s*" + + "(\\d+\\.\\d+)\\s*" + + "
"); + Matcher scoreMather = scorePatten.matcher(forObject1); + boolean isFindScore = scoreMather.find(); + if (isFindScore) { + + float score = Float.parseFloat(scoreMather.group(1)); + //if (score >= 7.0) { + + Pattern descPatten = Pattern.compile("

\\s*" + + "([^<]+)\\s*

"); + Matcher descMatcher = descPatten.matcher(forObject1); + boolean isFindDesc = descMatcher.find(); + if (isFindDesc) { + String desc = descMatcher.group(1); + + Pattern tagPatten = Pattern.compile("
  • \\s*" + + "[^<]+([^<]+)\\s*" + + "
  • "); + Matcher tagMatch = tagPatten.matcher(forObject1); + String tag = ""; + boolean isFindTag = tagMatch.find(); + while (isFindTag) { + tag += ("," + tagMatch.group(1)); + isFindTag = tagMatch.find(); + } + + if (tag.length() > 0) { + tag = tag.substring(1); + } + + + BookDO book = new BookDO(); + book.setAuthor(author); + book.setCatid(8); + book.setBookDesc(desc); + book.setBookName(bookName); + book.setSoftTag(tag); + book.setSoftCat(catId); + book.setScore(score > 10 ? 8.0f : score); + book.setVisitCount(Long.parseLong(visit)); + book.setPicUrl(pic); + book.setBookStatus(status); + book.setUpdateTime(updateTime); + + List indexList = new ArrayList<>(); + List contentList = new ArrayList<>(); + + //读取目录 + String indexUrl = "http://book.sfacg.com/Novel/" + bookNum + "/MainIndex/"; + String forObject2 = getByHttpClient(indexUrl); + if (forObject2 != null) { + Pattern indexListPatten = Pattern.compile("href=\"(/Novel/\\d+/\\d+/\\d+/)\"\\s+title=\"([^\"]+)\\s*"); + Matcher indexListMatch = indexListPatten.matcher(forObject2); + + boolean isFindIndex = indexListMatch.find(); + + int indexNum = 0; + + //查询该书籍已存在目录号 + List hasIndexNum = queryIndexCountByBookNameAndBAuthor(bookName, author); + + while (isFindIndex) { + if(isInteruptBiquTaCrawl || isInteruptBiquDaoCrawl){ + return; + } + if (!hasIndexNum.contains(indexNum)) { + + String contentUrl = "http://book.sfacg.com" + indexListMatch.group(1); + String indexName = indexListMatch.group(2); + + + //查询章节内容 + String forObject3 = getByHttpClient(contentUrl); + if (forObject3 != null && !forObject3.contains("内容整改中,请等待")) { + String content = forObject3.substring(forObject3.indexOf("
    ") + 6); + //TODO插入章节目录和章节内容 + BookIndexDO bookIndex = new BookIndexDO(); + bookIndex.setIndexName(indexName); + bookIndex.setIndexNum(indexNum); + indexList.add(bookIndex); + BookContentDO bookContent = new BookContentDO(); + bookContent.setContent(content); + bookContent.setIndexNum(indexNum); + contentList.add(bookContent); + + + } else { + break; + } + + } + + indexNum++; + isFindIndex = indexListMatch.find(); + } + if (indexList.size() == contentList.size() && indexList.size() > 0) { + saveBookAndIndexAndContent(book, indexList, contentList); + } + + } + } + } + + } + } + } + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + isFindBook = bookMatcher.find(); + } + } + } + } + + }).start(); + + + } + + } + + private void crawBiquTaBooks(int i) { + String baseUrl = "https://m.biquta.com"; + String catBookListUrlBase = baseUrl + "/class/"; + //拼接分类URL + int page = 1;//起始页码 + int totalPage = page; + String catBookListUrl = catBookListUrlBase + i + "/" + page + ".html"; + String forObject = getByHttpClient(catBookListUrl); + if (forObject != null) { + //匹配分页数 + Pattern pattern = Pattern.compile("value=\"(\\d+)/(\\d+)\""); + Matcher matcher = pattern.matcher(forObject); + boolean isFind = matcher.find(); + System.out.println("匹配分页数" + isFind); + if (isFind) { + int currentPage = Integer.parseInt(matcher.group(1)); + totalPage = Integer.parseInt(matcher.group(2)); + //解析第一页书籍的数据 + Pattern bookPatten = Pattern.compile("href=\"/(\\d+_\\d+)/\""); + parseBiquTaBook(bookPatten, forObject, i, baseUrl); + while (currentPage < totalPage) { + if (isInteruptBiquTaCrawl) { + return; + } + + catBookListUrl = catBookListUrlBase + i + "/" + (currentPage + 1) + ".html"; + forObject = getByHttpClient(catBookListUrl); + if (forObject != null) { + //匹配分页数 + matcher = pattern.matcher(forObject); + isFind = matcher.find(); + + if (isFind) { + currentPage = Integer.parseInt(matcher.group(1)); + totalPage = Integer.parseInt(matcher.group(2)); + parseBiquTaBook(bookPatten, forObject, i, baseUrl); + } + } else { + currentPage++; + } + } + } + } + } + + private void parseBiquTaBook(Pattern bookPatten, String forObject, int catNum, String baseUrl) { + Matcher matcher2 = bookPatten.matcher(forObject); + boolean isFind = matcher2.find(); + Pattern scorePatten = Pattern.compile("(\\d+\\.\\d+)分
    "); + Matcher scoreMatch = scorePatten.matcher(forObject); + boolean scoreFind = scoreMatch.find(); + + Pattern bookNamePatten = Pattern.compile("

    ([^/]+)

    "); + Matcher bookNameMatch = bookNamePatten.matcher(forObject); + boolean isBookNameMatch = bookNameMatch.find(); + + Pattern authorPatten = Pattern.compile(">作者:([^/]+)<"); + Matcher authoreMatch = authorPatten.matcher(forObject); + boolean isFindAuthor = authoreMatch.find(); + + + System.out.println("匹配书籍url" + isFind); + + System.out.println("匹配分数" + scoreFind); + while (isFind && scoreFind && isBookNameMatch && isFindAuthor) { + if (isInteruptBiquTaCrawl) { + return; + } + + try { + Float score = Float.parseFloat(scoreMatch.group(1)); /*if (score < lowestScore) {//数据库空间有限,暂时爬取8.0分以上的小说 // Thread.sleep(1000 * 60 * 60 * 24);//因为爬的是龙虎榜,所以遇到第一个8分以下的,之后的都是8分以下的 continue; }*/ - String bookName = bookNameMatch.group(1); - String author = authoreMatch.group(1); + String bookName = bookNameMatch.group(1); + String author = authoreMatch.group(1); /*//查询该书籍是否存在 boolean isExsit = bookService.isExsitBook(bookName, author); if (isExsit) { continue; }*/ - //System.out.println(new Date()+bookName + ":"); + //System.out.println(new Date()+bookName + ":"); - String bokNum = matcher2.group(1); - String bookUrl = baseUrl + "/" + bokNum + "/"; + String bokNum = matcher2.group(1); + String bookUrl = baseUrl + "/" + bokNum + "/"; - String body = getByHttpClient(bookUrl); - if (body != null) { - Pattern statusPatten = Pattern.compile("状态:([^/]+)"); - Matcher statusMatch = statusPatten.matcher(body); - if (statusMatch.find()) { - String status = statusMatch.group(1); - Pattern updateTimePatten = Pattern.compile("更新:(\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+)"); - Matcher updateTimeMatch = updateTimePatten.matcher(body); - if (updateTimeMatch.find()) { - String updateTimeStr = updateTimeMatch.group(1); - SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); - Date updateTime = format.parse(updateTimeStr); - Pattern picPatten = Pattern.compile("]+)\"\\s+onerror=\"this.src="); - Matcher picMather = picPatten.matcher(body); - if (picMather.find()) { - String picSrc = picMather.group(1); + String body = getByHttpClient(bookUrl); + if (body != null) { + Pattern statusPatten = Pattern.compile("状态:([^/]+)"); + Matcher statusMatch = statusPatten.matcher(body); + if (statusMatch.find()) { + String status = statusMatch.group(1); + Pattern updateTimePatten = Pattern.compile("更新:(\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+)"); + Matcher updateTimeMatch = updateTimePatten.matcher(body); + if (updateTimeMatch.find()) { + String updateTimeStr = updateTimeMatch.group(1); + SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); + Date updateTime = format.parse(updateTimeStr); + Pattern picPatten = Pattern.compile("]+)\"\\s+onerror=\"this.src="); + Matcher picMather = picPatten.matcher(body); + if (picMather.find()) { + String picSrc = picMather.group(1); - Pattern descPatten = Pattern.compile("class=\"review\">([^<]+)

    "); - Matcher descMatch = descPatten.matcher(body); - if (descMatch.find()) { - String desc = descMatch.group(1); + Pattern descPatten = Pattern.compile("class=\"review\">([^<]+)

    "); + Matcher descMatch = descPatten.matcher(body); + if (descMatch.find()) { + String desc = descMatch.group(1); - BookDO book = new BookDO(); - book.setAuthor(author); - book.setCatid(catNum); - book.setBookDesc(desc); - book.setBookName(bookName); - book.setScore(score > 10 ? 8.0f : score); - book.setPicUrl(picSrc); - book.setBookStatus(status); - book.setUpdateTime(updateTime); + BookDO book = new BookDO(); + book.setAuthor(author); + book.setCatid(catNum); + book.setBookDesc(desc); + book.setBookName(bookName); + book.setScore(score > 10 ? 8.0f : score); + book.setPicUrl(picSrc); + book.setBookStatus(status); + book.setUpdateTime(updateTime); - List indexList = new ArrayList<>(); - List contentList = new ArrayList<>(); + List indexList = new ArrayList<>(); + List contentList = new ArrayList<>(); - //读取目录 - Pattern indexPatten = Pattern.compile("查看完整目录"); - Matcher indexMatch = indexPatten.matcher(body); - if (indexMatch.find()) { - String indexUrl = baseUrl + indexMatch.group(1); - String body2 = getByHttpClient(indexUrl); - if (body2 != null) { - Pattern indexListPatten = Pattern.compile("([^/]+)"); - Matcher indexListMatch = indexListPatten.matcher(body2); + //读取目录 + Pattern indexPatten = Pattern.compile("查看完整目录"); + Matcher indexMatch = indexPatten.matcher(body); + if (indexMatch.find()) { + String indexUrl = baseUrl + indexMatch.group(1); + String body2 = getByHttpClient(indexUrl); + if (body2 != null) { + Pattern indexListPatten = Pattern.compile("([^/]+)"); + Matcher indexListMatch = indexListPatten.matcher(body2); - boolean isFindIndex = indexListMatch.find(); + boolean isFindIndex = indexListMatch.find(); - int indexNum = 0; - //查询该书籍已存在目录号 - List hasIndexNum = queryIndexCountByBookNameAndBAuthor(bookName, author); + int indexNum = 0; + //查询该书籍已存在目录号 + List hasIndexNum = queryIndexCountByBookNameAndBAuthor(bookName, author); - while (isFindIndex) { - if(isInteruptBiquTaCrawl){ - return; - } + while (isFindIndex) { + if (isInteruptBiquTaCrawl) { + return; + } - if (!hasIndexNum.contains(indexNum)) { + if (!hasIndexNum.contains(indexNum)) { - String contentUrl = baseUrl + indexListMatch.group(1); - String indexName = indexListMatch.group(2); + String contentUrl = baseUrl + indexListMatch.group(1); + String indexName = indexListMatch.group(2); - //查询章节内容 - String body3 = getByHttpClient(contentUrl); - if (body3 != null) { - Pattern contentPattten = Pattern.compile("章节错误,点此举报(.*)加入书签,方便阅读"); - String start = "『章节错误,点此举报』"; - String end = "『加入书签,方便阅读』"; - String content = body3.substring(body3.indexOf(start) + start.length(), body3.indexOf(end)); - //TODO插入章节目录和章节内容 - BookIndexDO bookIndex = new BookIndexDO(); - bookIndex.setIndexName(indexName); - bookIndex.setIndexNum(indexNum); - indexList.add(bookIndex); - BookContentDO bookContent = new BookContentDO(); - bookContent.setContent(content); - bookContent.setIndexNum(indexNum); - contentList.add(bookContent); - //System.out.println(indexName); + //查询章节内容 + String body3 = getByHttpClient(contentUrl); + if (body3 != null) { + Pattern contentPattten = Pattern.compile("章节错误,点此举报(.*)加入书签,方便阅读"); + String start = "『章节错误,点此举报』"; + String end = "『加入书签,方便阅读』"; + String content = body3.substring(body3.indexOf(start) + start.length(), body3.indexOf(end)); + //TODO插入章节目录和章节内容 + BookIndexDO bookIndex = new BookIndexDO(); + bookIndex.setIndexName(indexName); + bookIndex.setIndexNum(indexNum); + indexList.add(bookIndex); + BookContentDO bookContent = new BookContentDO(); + bookContent.setContent(content); + bookContent.setIndexNum(indexNum); + contentList.add(bookContent); + //System.out.println(indexName); - } else { - break; - } - } - indexNum++; - isFindIndex = indexListMatch.find(); - } + } else { + break; + } + } + indexNum++; + isFindIndex = indexListMatch.find(); + } - if (indexList.size() == contentList.size() && indexList.size() > 0) { - saveBookAndIndexAndContent(book, indexList, contentList); - } - } + if (indexList.size() == contentList.size() && indexList.size() > 0) { + saveBookAndIndexAndContent(book, indexList, contentList); + } + } - } + } - } + } - } - } + } + } - } + } - } + } - } catch (Exception e) { + } catch (Exception e) { - e.printStackTrace(); + e.printStackTrace(); - } finally { - matcher2.find(); - isFind = matcher2.find();//需要找两次,应为有两个一样的路径匹配 - scoreFind = scoreMatch.find(); - isBookNameMatch = bookNameMatch.find(); - isFindAuthor = authoreMatch.find(); + } finally { + matcher2.find(); + isFind = matcher2.find();//需要找两次,应为有两个一样的路径匹配 + scoreFind = scoreMatch.find(); + isBookNameMatch = bookNameMatch.find(); + isFindAuthor = authoreMatch.find(); - } + } - } - } + } + } - private void crawBiqudaoBooks(final int i) { - String baseUrl = "https://m.biqudao.com"; - String catBookListUrlBase = baseUrl + "/bqgelhb/"; - //拼接分类URL - int page = 1;//起始页码 - int totalPage = page; - String catBookListUrl = catBookListUrlBase + i + "/" + page + ".html"; - String forObject = getByHttpClient(catBookListUrl); - if (forObject != null) { - //匹配分页数 - Pattern pattern = Pattern.compile("value=\"(\\d+)/(\\d+)\""); - Matcher matcher = pattern.matcher(forObject); - boolean isFind = matcher.find(); - System.out.println("匹配分页数" + isFind); - if (isFind) { - int currentPage = Integer.parseInt(matcher.group(1)); - totalPage = Integer.parseInt(matcher.group(2)); - //解析第一页书籍的数据 - Pattern bookPatten = Pattern.compile("href=\"/(bqge\\d+)/\""); - parseBiqudaoBook(bookPatten, forObject, i, baseUrl); - while (currentPage < totalPage) { + private void crawBiqudaoBooks(final int i) { + String baseUrl = "https://m.biqudao.com"; + String catBookListUrlBase = baseUrl + "/bqgelhb/"; + //拼接分类URL + int page = 1;//起始页码 + int totalPage = page; + String catBookListUrl = catBookListUrlBase + i + "/" + page + ".html"; + String forObject = getByHttpClient(catBookListUrl); + if (forObject != null) { + //匹配分页数 + Pattern pattern = Pattern.compile("value=\"(\\d+)/(\\d+)\""); + Matcher matcher = pattern.matcher(forObject); + boolean isFind = matcher.find(); + System.out.println("匹配分页数" + isFind); + if (isFind) { + int currentPage = Integer.parseInt(matcher.group(1)); + totalPage = Integer.parseInt(matcher.group(2)); + //解析第一页书籍的数据 + Pattern bookPatten = Pattern.compile("href=\"/(bqge\\d+)/\""); + parseBiqudaoBook(bookPatten, forObject, i, baseUrl); + while (currentPage < totalPage) { - if(isInteruptBiquDaoCrawl){ - return; - } + if (isInteruptBiquDaoCrawl) { + return; + } - catBookListUrl = catBookListUrlBase + i + "/" + (currentPage + 1) + ".html"; - forObject = getByHttpClient(catBookListUrl); - if (forObject != null) { - //匹配分页数 - matcher = pattern.matcher(forObject); - isFind = matcher.find(); + catBookListUrl = catBookListUrlBase + i + "/" + (currentPage + 1) + ".html"; + forObject = getByHttpClient(catBookListUrl); + if (forObject != null) { + //匹配分页数 + matcher = pattern.matcher(forObject); + isFind = matcher.find(); - if (isFind) { - currentPage = Integer.parseInt(matcher.group(1)); - totalPage = Integer.parseInt(matcher.group(2)); - parseBiqudaoBook(bookPatten, forObject, i, baseUrl); - } - } else { - currentPage++; - } - } - } - } + if (isFind) { + currentPage = Integer.parseInt(matcher.group(1)); + totalPage = Integer.parseInt(matcher.group(2)); + parseBiqudaoBook(bookPatten, forObject, i, baseUrl); + } + } else { + currentPage++; + } + } + } + } - } + } - private void parseBiqudaoBook(Pattern bookPatten, String forObject, int catNum, String baseUrl) { + private void parseBiqudaoBook(Pattern bookPatten, String forObject, int catNum, String baseUrl) { - Matcher matcher2 = bookPatten.matcher(forObject); - boolean isFind = matcher2.find(); - Pattern scorePatten = Pattern.compile("(\\d+\\.\\d+)分"); - Matcher scoreMatch = scorePatten.matcher(forObject); - boolean scoreFind = scoreMatch.find(); + Matcher matcher2 = bookPatten.matcher(forObject); + boolean isFind = matcher2.find(); + Pattern scorePatten = Pattern.compile("(\\d+\\.\\d+)分"); + Matcher scoreMatch = scorePatten.matcher(forObject); + boolean scoreFind = scoreMatch.find(); - Pattern bookNamePatten = Pattern.compile("

    ([^/]+)

    "); - Matcher bookNameMatch = bookNamePatten.matcher(forObject); - boolean isBookNameMatch = bookNameMatch.find(); + Pattern bookNamePatten = Pattern.compile("

    ([^/]+)

    "); + Matcher bookNameMatch = bookNamePatten.matcher(forObject); + boolean isBookNameMatch = bookNameMatch.find(); - Pattern authorPatten = Pattern.compile(">作者:([^<]+)<"); - Matcher authoreMatch = authorPatten.matcher(forObject); - boolean isFindAuthor = authoreMatch.find(); + Pattern authorPatten = Pattern.compile(">作者:([^<]+)<"); + Matcher authoreMatch = authorPatten.matcher(forObject); + boolean isFindAuthor = authoreMatch.find(); - System.out.println("匹配书籍url" + isFind); + System.out.println("匹配书籍url" + isFind); - System.out.println("匹配分数" + scoreFind); - while (isFind && scoreFind && isBookNameMatch && isFindAuthor) { + System.out.println("匹配分数" + scoreFind); + while (isFind && scoreFind && isBookNameMatch && isFindAuthor) { - try { - if(isInteruptBiquDaoCrawl){ - return; - } + try { + if (isInteruptBiquDaoCrawl) { + return; + } - Float score = Float.parseFloat(scoreMatch.group(1)); + Float score = Float.parseFloat(scoreMatch.group(1)); /*if (score < lowestScore) {//数据库空间有限,暂时爬取8.0分以上的小说 Thread.sleep(1000 * 60 * 60 * 24);//因为爬的是龙虎榜,所以遇到第一个8分以下的,之后的都是8分以下的 continue; }*/ - String bookName = bookNameMatch.group(1); - String author = authoreMatch.group(1); + String bookName = bookNameMatch.group(1); + String author = authoreMatch.group(1); /*//查询该书籍是否存在 boolean isExsit = bookService.isExsitBook(bookName, author); if (isExsit) { continue; }*/ - //System.out.println(new Date()+bookName + ":"); + //System.out.println(new Date()+bookName + ":"); - String bokNum = matcher2.group(1); - String bookUrl = baseUrl + "/" + bokNum + "/"; + String bokNum = matcher2.group(1); + String bookUrl = baseUrl + "/" + bokNum + "/"; - String body = getByHttpClient(bookUrl); - if (body != null) { - Pattern statusPatten = Pattern.compile("状态:([^/]+)"); - Matcher statusMatch = statusPatten.matcher(body); - if (statusMatch.find()) { - String status = statusMatch.group(1); - Pattern updateTimePatten = Pattern.compile("更新:(\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+)"); - Matcher updateTimeMatch = updateTimePatten.matcher(body); - if (updateTimeMatch.find()) { - String updateTimeStr = updateTimeMatch.group(1); - SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); - Date updateTime = format.parse(updateTimeStr); - Pattern picPatten = Pattern.compile("]+)\"\\s+onerror=\"this.src="); - Matcher picMather = picPatten.matcher(body); - if (picMather.find()) { - String picSrc = picMather.group(1); + String body = getByHttpClient(bookUrl); + if (body != null) { + Pattern statusPatten = Pattern.compile("状态:([^/]+)"); + Matcher statusMatch = statusPatten.matcher(body); + if (statusMatch.find()) { + String status = statusMatch.group(1); + Pattern updateTimePatten = Pattern.compile("更新:(\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+)"); + Matcher updateTimeMatch = updateTimePatten.matcher(body); + if (updateTimeMatch.find()) { + String updateTimeStr = updateTimeMatch.group(1); + SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); + Date updateTime = format.parse(updateTimeStr); + Pattern picPatten = Pattern.compile("]+)\"\\s+onerror=\"this.src="); + Matcher picMather = picPatten.matcher(body); + if (picMather.find()) { + String picSrc = picMather.group(1); - Pattern descPatten = Pattern.compile("class=\"review\">([^<]+)

    "); - Matcher descMatch = descPatten.matcher(body); - if (descMatch.find()) { - String desc = descMatch.group(1); + Pattern descPatten = Pattern.compile("class=\"review\">([^<]+)

    "); + Matcher descMatch = descPatten.matcher(body); + if (descMatch.find()) { + String desc = descMatch.group(1); - BookDO book = new BookDO(); - book.setAuthor(author); - book.setCatid(catNum); - book.setBookDesc(desc); - book.setBookName(bookName); - book.setScore(score > 10 ? 8.0f : score); - book.setPicUrl(picSrc); - book.setBookStatus(status); - book.setUpdateTime(updateTime); + BookDO book = new BookDO(); + book.setAuthor(author); + book.setCatid(catNum); + book.setBookDesc(desc); + book.setBookName(bookName); + book.setScore(score > 10 ? 8.0f : score); + book.setPicUrl(picSrc); + book.setBookStatus(status); + book.setUpdateTime(updateTime); - List indexList = new ArrayList<>(); - List contentList = new ArrayList<>(); + List indexList = new ArrayList<>(); + List contentList = new ArrayList<>(); - //读取目录 - Pattern indexPatten = Pattern.compile("查看完整目录"); - Matcher indexMatch = indexPatten.matcher(body); - if (indexMatch.find()) { - String indexUrl = baseUrl + indexMatch.group(1); - String body2 = getByHttpClient(indexUrl); - if (body2 != null) { - Pattern indexListPatten = Pattern.compile("([^/]+)"); - Matcher indexListMatch = indexListPatten.matcher(body2); + //读取目录 + Pattern indexPatten = Pattern.compile("查看完整目录"); + Matcher indexMatch = indexPatten.matcher(body); + if (indexMatch.find()) { + String indexUrl = baseUrl + indexMatch.group(1); + String body2 = getByHttpClient(indexUrl); + if (body2 != null) { + Pattern indexListPatten = Pattern.compile("([^/]+)"); + Matcher indexListMatch = indexListPatten.matcher(body2); - boolean isFindIndex = indexListMatch.find(); + boolean isFindIndex = indexListMatch.find(); - int indexNum = 0; - //查询该书籍已存在目录号 - List hasIndexNum = queryIndexCountByBookNameAndBAuthor(bookName, author); + int indexNum = 0; + //查询该书籍已存在目录号 + List hasIndexNum = queryIndexCountByBookNameAndBAuthor(bookName, author); - while (isFindIndex) { - if(isInteruptBiquDaoCrawl){ - return; - } - if (!hasIndexNum.contains(indexNum)) { + while (isFindIndex) { + if (isInteruptBiquDaoCrawl) { + return; + } + if (!hasIndexNum.contains(indexNum)) { - String contentUrl = baseUrl + indexListMatch.group(1); - String indexName = indexListMatch.group(2); + String contentUrl = baseUrl + indexListMatch.group(1); + String indexName = indexListMatch.group(2); - //查询章节内容 - String body3 = getByHttpClient(contentUrl); - if (body3 != null) { - Pattern contentPattten = Pattern.compile("章节错误,点此举报(.*)加入书签,方便阅读"); - String start = "『章节错误,点此举报』"; - String end = "『加入书签,方便阅读』"; - String content = body3.substring(body3.indexOf(start) + start.length(), body3.indexOf(end)); - //TODO插入章节目录和章节内容 - BookIndexDO bookIndex = new BookIndexDO(); - bookIndex.setIndexName(indexName); - bookIndex.setIndexNum(indexNum); - indexList.add(bookIndex); - BookContentDO bookContent = new BookContentDO(); - bookContent.setContent(content); - bookContent.setIndexNum(indexNum); - contentList.add(bookContent); - //System.out.println(indexName); + //查询章节内容 + String body3 = getByHttpClient(contentUrl); + if (body3 != null) { + Pattern contentPattten = Pattern.compile("章节错误,点此举报(.*)加入书签,方便阅读"); + String start = "『章节错误,点此举报』"; + String end = "『加入书签,方便阅读』"; + String content = body3.substring(body3.indexOf(start) + start.length(), body3.indexOf(end)); + //TODO插入章节目录和章节内容 + BookIndexDO bookIndex = new BookIndexDO(); + bookIndex.setIndexName(indexName); + bookIndex.setIndexNum(indexNum); + indexList.add(bookIndex); + BookContentDO bookContent = new BookContentDO(); + bookContent.setContent(content); + bookContent.setIndexNum(indexNum); + contentList.add(bookContent); + //System.out.println(indexName); - } else { - break; - } - } - indexNum++; - isFindIndex = indexListMatch.find(); - } + } else { + break; + } + } + indexNum++; + isFindIndex = indexListMatch.find(); + } - if (indexList.size() == contentList.size() && indexList.size() > 0) { - saveBookAndIndexAndContent(book, indexList, contentList); - } - } + if (indexList.size() == contentList.size() && indexList.size() > 0) { + saveBookAndIndexAndContent(book, indexList, contentList); + } + } - } + } - } + } - } - } + } + } - } + } - } + } - } catch (Exception e) { + } catch (Exception e) { - e.printStackTrace(); + e.printStackTrace(); - } finally { - matcher2.find(); - isFind = matcher2.find();//需要找两次,应为有两个一样的路径匹配 - scoreFind = scoreMatch.find(); - isBookNameMatch = bookNameMatch.find(); - isFindAuthor = authoreMatch.find(); - } + } finally { + matcher2.find(); + isFind = matcher2.find();//需要找两次,应为有两个一样的路径匹配 + scoreFind = scoreMatch.find(); + isBookNameMatch = bookNameMatch.find(); + isFindAuthor = authoreMatch.find(); + } - } + } - } + } - public void saveBookAndIndexAndContent(BookDO book, List bookIndex, List bookContent) { - boolean isUpdate = false; - Long bookId = -1l; - book.setBookName(book.getBookName().trim()); - book.setAuthor(book.getAuthor().trim()); - Map bookExample = new HashMap<>(); - bookExample.put("bookName",book.getBookName()); - bookExample.put("author",book.getAuthor()); - List books = bookDao.list(bookExample); - if (books.size() > 0) { - //更新 - bookId = books.get(0).getId(); - book.setId(bookId); - bookDao.update(book); - isUpdate = true; + public void saveBookAndIndexAndContent(BookDO book, List bookIndex, List bookContent) { + boolean isUpdate = false; + Long bookId = -1l; + book.setBookName(book.getBookName().trim()); + book.setAuthor(book.getAuthor().trim()); + Map bookExample = new HashMap<>(); + bookExample.put("bookName", book.getBookName()); + bookExample.put("author", book.getAuthor()); + List books = bookDao.list(bookExample); + if (books.size() > 0) { + //更新 + bookId = books.get(0).getId(); + book.setId(bookId); + bookDao.update(book); + isUpdate = true; - } else { - if (book.getVisitCount() == null) { - Long visitCount = generateVisiteCount(book.getScore()); - book.setVisitCount(visitCount); - } - //插入 - int rows = bookDao.save(book); - if (rows > 0) { - bookId = book.getId(); - } + } else { + if (book.getVisitCount() == null) { + Long visitCount = generateVisiteCount(book.getScore()); + book.setVisitCount(visitCount); + } + //插入 + int rows = bookDao.save(book); + if (rows > 0) { + bookId = book.getId(); + } - } + } - if (bookId >= 0) { - //查询目录已存在数量 + if (bookId >= 0) { + //查询目录已存在数量 /* BookIndexExample bookIndexExample = new BookIndexExample(); bookIndexExample.createCriteria().andBookIdEqualTo(bookId); int indexCount = bookIndexMapper.countByExample(bookIndexExample);*/ - List newBookIndexList = new ArrayList<>(); - List newContentList = new ArrayList<>(); - for (int i = 0; i < bookIndex.size(); i++) { - BookContentDO bookContentItem = bookContent.get(i); - if (!bookContentItem.getContent().contains("正在手打中,请稍等片刻,内容更新后,需要重新刷新页面,才能获取最新更新")) { + List newBookIndexList = new ArrayList<>(); + List newContentList = new ArrayList<>(); + for (int i = 0; i < bookIndex.size(); i++) { + BookContentDO bookContentItem = bookContent.get(i); + if (!bookContentItem.getContent().contains("正在手打中,请稍等片刻,内容更新后,需要重新刷新页面,才能获取最新更新")) { - BookIndexDO bookIndexItem = bookIndex.get(i); - bookIndexItem.setBookId(bookId); - bookContentItem.setBookId(bookId); - //bookContentItem.setIndexId(bookIndexItem.getId());暂时使用bookId和IndexNum查询content - bookContentItem.setIndexNum(bookIndexItem.getIndexNum()); + BookIndexDO bookIndexItem = bookIndex.get(i); + bookIndexItem.setBookId(bookId); + bookContentItem.setBookId(bookId); + //bookContentItem.setIndexId(bookIndexItem.getId());暂时使用bookId和IndexNum查询content + bookContentItem.setIndexNum(bookIndexItem.getIndexNum()); - newBookIndexList.add(bookIndexItem); - newContentList.add(bookContentItem); - } - } + newBookIndexList.add(bookIndexItem); + newContentList.add(bookContentItem); + } + } - if (newBookIndexList.size() > 0) { - bookIndexDao.insertBatch(newBookIndexList); + if (newBookIndexList.size() > 0) { + bookIndexDao.insertBatch(newBookIndexList); - bookContentDao.insertBatch(newContentList); - } + bookContentDao.insertBatch(newContentList); + } + } - } + } + private Long generateVisiteCount(Float score) { + int baseNum = (int) (Math.pow(score * 10, (int) (score - 5)) / 2); + return Long.parseLong(baseNum + new Random().nextInt(1000) + ""); + } - } - - private Long generateVisiteCount(Float score) { - int baseNum = (int) (Math.pow(score * 10, (int) (score - 5)) / 2); - return Long.parseLong(baseNum + new Random().nextInt(1000) + ""); - } - - private String getByHttpClient(String catBookListUrl) { - try { + private String getByHttpClient(String catBookListUrl) { + try { /*HttpClient httpClient = new DefaultHttpClient(); // 设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000) @@ -659,43 +912,43 @@ public class BookCrawlServiceImpl implements BookCrawlService { } else { return null; }*/ - //经测试restTemplate比httpClient效率高出很多倍,所有选择restTemplate - ResponseEntity forEntity = restTemplate.getForEntity(catBookListUrl, String.class); - if (forEntity.getStatusCode() == HttpStatus.OK) { - return forEntity.getBody(); - } else { - return null; - } - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } + //经测试restTemplate比httpClient效率高出很多倍,所有选择restTemplate + ResponseEntity forEntity = restTemplate.getForEntity(catBookListUrl, String.class); + if (forEntity.getStatusCode() == HttpStatus.OK) { + return forEntity.getBody(); + } else { + return null; + } + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } - /** - * 查询该书籍目录数量 - */ - private List queryIndexCountByBookNameAndBAuthor(String bookName, String author) { - List result = new ArrayList<>(); - Map bookExample = new HashMap<>(); - bookExample.put("bookName",bookName); - bookExample.put("author",author); - List books = bookDao.list(bookExample); - if (books.size() > 0) { + /** + * 查询该书籍目录数量 + */ + private List queryIndexCountByBookNameAndBAuthor(String bookName, String author) { + List result = new ArrayList<>(); + Map bookExample = new HashMap<>(); + bookExample.put("bookName", bookName); + bookExample.put("author", author); + List books = bookDao.list(bookExample); + if (books.size() > 0) { - Long bookId = books.get(0).getId(); - Map bookIndexExample = new HashMap<>(); - bookExample.put("bookId",bookId); - List bookIndices = bookIndexDao.list(bookIndexExample); - if (bookIndices != null && bookIndices.size() > 0) { - for (BookIndexDO bookIndex : bookIndices) { - result.add(bookIndex.getIndexNum()); - } - } + Long bookId = books.get(0).getId(); + Map bookIndexExample = new HashMap<>(); + bookExample.put("bookId", bookId); + List bookIndices = bookIndexDao.list(bookIndexExample); + if (bookIndices != null && bookIndices.size() > 0) { + for (BookIndexDO bookIndex : bookIndices) { + result.add(bookIndex.getIndexNum()); + } + } - } + } - return result; + return result; - } + } } diff --git a/novel-admin/src/main/resources/application.yml b/novel-admin/src/main/resources/application.yml index fca8311..c6d3c32 100644 --- a/novel-admin/src/main/resources/application.yml +++ b/novel-admin/src/main/resources/application.yml @@ -35,8 +35,8 @@ spring: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/books?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai - username: root - password: test123456 + username: books + password: books #password: initialSize: 1 minIdle: 3 diff --git a/novel-admin/src/main/resources/crawl.properties b/novel-admin/src/main/resources/crawl.properties new file mode 100644 index 0000000..addb32e --- /dev/null +++ b/novel-admin/src/main/resources/crawl.properties @@ -0,0 +1,10 @@ +#߳ +threadCount=1 +#ȡȼ 1 2ʱ +priority=1 +#С˵֣0ʾ +lowestScore=0 +#С˵Сʱ +minUptTime=2000-01-01 00:00:00 +#ȡ,0ʾ +maxNumber=0 diff --git a/novel-admin/src/main/resources/static/js/appjs/books/bookCrawl/bookCrawl.js b/novel-admin/src/main/resources/static/js/appjs/books/bookCrawl/bookCrawl.js index ec4c571..a46aaa5 100644 --- a/novel-admin/src/main/resources/static/js/appjs/books/bookCrawl/bookCrawl.js +++ b/novel-admin/src/main/resources/static/js/appjs/books/bookCrawl/bookCrawl.js @@ -159,14 +159,15 @@ function detail(id) { }); } -function edit(id) { +function edit(){ + console.log('打开配置页面'); layer.open({ - type: 2, - title: '编辑', - maxmin: true, - shadeClose: false, // 点击遮罩关闭层 - area: ['800px', '520px'], - content: prefix + '/edit/' + id // iframe的url + type : 2, + title : '增加', + maxmin : true, + shadeClose : false, + area : [ '800px', '520px' ], + content : prefix + '/edit' }); } diff --git a/novel-admin/src/main/resources/templates/books/bookCrawl/bookCrawl.html b/novel-admin/src/main/resources/templates/books/bookCrawl/bookCrawl.html index fc1a224..7ccbe21 100644 --- a/novel-admin/src/main/resources/templates/books/bookCrawl/bookCrawl.html +++ b/novel-admin/src/main/resources/templates/books/bookCrawl/bookCrawl.html @@ -9,14 +9,9 @@
    - -
    diff --git a/novel-front/src/main/resources/application.yml b/novel-front/src/main/resources/application.yml index 9a41764..6a5b0a6 100644 --- a/novel-front/src/main/resources/application.yml +++ b/novel-front/src/main/resources/application.yml @@ -1,5 +1,5 @@ server: - port: 80 + port: 8080 spring: datasource: diff --git a/novel-front/src/main/resources/templates/books/book_content.html b/novel-front/src/main/resources/templates/books/book_content.html index 169f903..8909da4 100644 --- a/novel-front/src/main/resources/templates/books/book_content.html +++ b/novel-front/src/main/resources/templates/books/book_content.html @@ -309,35 +309,16 @@ // 弹幕定时器 var timers = []; // 控制弹幕显隐变量 + //监听键盘事件 + $("#screenBulletText").keypress(function (even) { + if (even.which == 13) { + //enter键按下 + sendBullet(); + } + }); // 监听发送按钮 $(".send").on("click", function () { - var bullet = $("#screenBulletText").val(); - var contentId = $("#contentIdHidden").val(); - if (bullet && contentId) { - $.ajax({ - type: "POST", - url: "/book/sendBullet", - contentType: 'application/x-www-form-urlencoded;charset=utf-8', - data: {contentId: contentId, bullet: bullet}, - dataType: "json", - success: function (data) { - console.log(data); - var bullet = $("#screenBulletText").val("") - }, - error: function (e) { - console.log(e); - layer.alert("发送失败"); - return; - } - }); - } else { - layer.alert("发送内容不能为空"); - return; - } - // 创建弹幕 - var jqueryDom = createScreenbullet(bullet); - // 添加定时任务 - addInterval(jqueryDom); + sendBullet(); }); // 监听关闭弹幕按钮 $("[lay-skin='_switch']").click(function () { @@ -488,6 +469,36 @@ while(true) if(new Date().getTime()-start > n) break; } + //发送弹幕 + function sendBullet(){ + var bullet = $("#screenBulletText").val(); + var contentId = $("#contentIdHidden").val(); + if (bullet && contentId) { + $.ajax({ + type: "POST", + url: "/book/sendBullet", + contentType: 'application/x-www-form-urlencoded;charset=utf-8', + data: {contentId: contentId, bullet: bullet}, + dataType: "json", + success: function (data) { + console.log(data); + var bullet = $("#screenBulletText").val("") + }, + error: function (e) { + console.log(e); + layer.alert("发送失败"); + return; + } + }); + } else { + layer.alert("发送内容不能为空"); + return; + } + // 创建弹幕 + var jqueryDom = createScreenbullet(bullet); + // 添加定时任务 + addInterval(jqueryDom); + } \ No newline at end of file