|
| 1 | +package com.wafflestudio.team2server.crawler.service |
| 2 | + |
| 3 | +import com.wafflestudio.team2server.article.model.Article |
| 4 | +import com.wafflestudio.team2server.article.repository.ArticleRepository |
| 5 | +import com.wafflestudio.team2server.article.service.ArticleService |
| 6 | +import com.wafflestudio.team2server.crawler.BaseCrawler |
| 7 | +import com.wafflestudio.team2server.crawler.repository.CrawlerRepository |
| 8 | +import org.jsoup.Connection |
| 9 | +import org.jsoup.Jsoup |
| 10 | +import org.jsoup.nodes.Document |
| 11 | +import org.jsoup.nodes.Element |
| 12 | +import org.springframework.scheduling.annotation.Scheduled |
| 13 | +import org.springframework.stereotype.Service |
| 14 | +import java.time.Instant |
| 15 | +import java.time.LocalDate |
| 16 | +import java.time.ZoneId |
| 17 | +import java.time.format.DateTimeFormatter |
| 18 | +import java.util.regex.Pattern |
| 19 | + |
| 20 | +@Service |
| 21 | +class SnutiCrawlerService( |
| 22 | + crawlerRepository: CrawlerRepository, |
| 23 | + private val articleRepository: ArticleRepository, |
| 24 | + private val articleService: ArticleService, |
| 25 | +) : BaseCrawler(crawlerRepository, articleRepository, articleService) { |
| 26 | + override val listUrl = "https://snuti.snu.ac.kr" |
| 27 | + override val baseUrl = "https://snuti.snu.ac.kr" |
| 28 | + override val targetBoardId = 5L |
| 29 | + override val code = "snuti" |
| 30 | + override val crawlIntervalSeconds = 3600L |
| 31 | + |
| 32 | + val detailDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") |
| 33 | + |
| 34 | + override fun getPostList(document: Document): List<Element> = document.select("#notice1 > div > div > article") |
| 35 | + |
| 36 | + override fun getPostTitle(element: Element): String { |
| 37 | + val aTag = element.select("a") |
| 38 | + return aTag.text().trim() |
| 39 | + } |
| 40 | + |
| 41 | + override fun getPostLink(element: Element): String { |
| 42 | + val rawUrl = element.select("a").attr("href") |
| 43 | + return if (rawUrl.startsWith("http")) rawUrl else "$baseUrl$rawUrl" |
| 44 | + } |
| 45 | + |
| 46 | + override fun parseDetailAndGetArticle( |
| 47 | + boardId: Long, |
| 48 | + element: Element, |
| 49 | + detailDoc: Document, |
| 50 | + url: String, |
| 51 | + title: String, |
| 52 | + ): Article { |
| 53 | + val content = detailDoc.select("div.board_view_content").html() |
| 54 | + |
| 55 | + var author = detailDoc.select("span.writer").text().trim() |
| 56 | + if (author.isEmpty()) { |
| 57 | + author = "관리자" // fallback |
| 58 | + } |
| 59 | + |
| 60 | + val dateStr = detailDoc.select("span.date").text().trim() |
| 61 | + |
| 62 | + val publishedAt = |
| 63 | + try { |
| 64 | + val localDate = LocalDate.parse(dateStr, detailDateFormatter) |
| 65 | + localDate.atStartOfDay(ZoneId.of("Asia/Seoul")).toInstant() |
| 66 | + } catch (_: Exception) { |
| 67 | + Instant.now() |
| 68 | + } |
| 69 | + |
| 70 | + return Article( |
| 71 | + boardId = boardId, |
| 72 | + title = title, |
| 73 | + content = content, |
| 74 | + author = author, |
| 75 | + originLink = url, |
| 76 | + publishedAt = publishedAt, |
| 77 | + createdAt = Instant.now(), |
| 78 | + updatedAt = Instant.now(), |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + fun fetchDetail(url: String): Document { |
| 83 | + // 1. Fetch the initial page |
| 84 | + val firstPage = |
| 85 | + Jsoup |
| 86 | + .connect(url) |
| 87 | + .userAgent("Mozilla/5.0") |
| 88 | + .get() |
| 89 | + |
| 90 | + // 2. Locate the script containing the redirection call |
| 91 | + // We search for the script tag that calls go_board_view |
| 92 | + val scripts = firstPage.select("script") |
| 93 | + var bid = "" |
| 94 | + var secondPageUrl = "" |
| 95 | + |
| 96 | + // Regex to find: go_board_view('VALUE1', 'VALUE2') |
| 97 | + val pattern = Pattern.compile("go_board_view\\s*\\(\\s*'([^']*)'\\s*,\\s*'([^']*)'") |
| 98 | + |
| 99 | + for (script in scripts) { |
| 100 | + val scriptContent: String = script.data() |
| 101 | + if (scriptContent.contains("go_board_view")) { |
| 102 | + val matcher = pattern.matcher(scriptContent) |
| 103 | + if (matcher.find()) { |
| 104 | + bid = matcher.group(1) // '12698' |
| 105 | + secondPageUrl = matcher.group(2) // 'https://snuti.snu.ac.kr/...' |
| 106 | + break |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (bid.isEmpty() || secondPageUrl.isEmpty()) { |
| 112 | + throw IllegalArgumentException("bid or second page url can not be empty") |
| 113 | + } |
| 114 | + |
| 115 | + // 3. If we found the values, perform the POST request |
| 116 | + val realPage = |
| 117 | + Jsoup |
| 118 | + .connect(secondPageUrl) |
| 119 | + .data("board_mode", "VIEW") |
| 120 | + .data("bid", bid) |
| 121 | + .data("var_page", "1") |
| 122 | + .data("search_field", "ALL") |
| 123 | + .data("search_task", "ALL") |
| 124 | + .method(Connection.Method.POST) |
| 125 | + .post() |
| 126 | + return realPage |
| 127 | + } |
| 128 | + |
| 129 | + override fun crawl() { |
| 130 | + try { |
| 131 | + val listDoc = fetch(listUrl) |
| 132 | + |
| 133 | + val rows = getPostList(listDoc) |
| 134 | + |
| 135 | + for (row in rows) { |
| 136 | + val rawLink = getPostLink(row) |
| 137 | + val detailUrl = if (rawLink.startsWith("http")) rawLink else "$baseUrl$rawLink" |
| 138 | + |
| 139 | + if (articleRepository.existsByOriginLink(detailUrl)) { |
| 140 | + continue |
| 141 | + } |
| 142 | + |
| 143 | + val detailDoc = fetchDetail(detailUrl) |
| 144 | + |
| 145 | + val title = getPostTitle(row) |
| 146 | + |
| 147 | + val article = parseDetailAndGetArticle(targetBoardId, row, detailDoc, detailUrl, title) |
| 148 | + |
| 149 | + articleService.saveNewArticle(article) |
| 150 | + |
| 151 | + Thread.sleep(500) |
| 152 | + } |
| 153 | + } catch (_: Exception) { |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Scheduled(fixedRate = 3600000) |
| 158 | + fun runScheduled() { |
| 159 | + crawl() |
| 160 | + updateExecutionTime() |
| 161 | + } |
| 162 | +} |
0 commit comments