mirror of
https://github.com/headporter81/specialsource-homepage-backend.git
synced 2026-12-06 07:41:09 +09:00
feat : 메인 헤드라인을 네이버 뉴스 IT/과학 섹션 헤드라인으로 변경
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 1m4s
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 1m4s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f4b571747d
commit
6e27519ff6
1 file changed
+9
-7
@@ -3,7 +3,6 @@ package company.specialsource.controller;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.jsoup.nodes.Element;
|
import org.jsoup.nodes.Element;
|
||||||
import org.jsoup.parser.Parser;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@@ -12,14 +11,16 @@ import java.time.Instant;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 메인 화면 IT 헤드라인: GeekNews Atom 피드를 읽어 최근 글 제목·링크를 내려준다.
|
* 메인 화면 헤드라인: 네이버 뉴스 IT/과학 섹션 페이지의 "헤드라인" 묶음에서 제목·링크를 뽑아 내려준다.
|
||||||
* 브라우저에서 직접 읽으면 CORS로 막혀서 백엔드가 대신 가져온다. 30분 동안 메모리에 보관.
|
* 브라우저에서 직접 읽으면 CORS로 막혀서 백엔드가 대신 가져온다. 30분 동안 메모리에 보관.
|
||||||
|
* 섹션 번호: 100 정치, 101 경제, 102 사회, 103 생활/문화, 104 세계, 105 IT/과학
|
||||||
|
* ponytail: 공식 API가 없어 화면(HTML)을 읽음 → 네이버가 화면 구조(class 이름)를 바꾸면 빈 목록이 됨. 그때 선택자만 고치면 됨
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
public class HeadlineController {
|
public class HeadlineController {
|
||||||
|
|
||||||
private static final String FEED_URL = "https://news.hada.io/rss/news";
|
private static final String FEED_URL = "https://news.naver.com/section/105";
|
||||||
private static final Duration CACHE_TTL = Duration.ofMinutes(30);
|
private static final Duration CACHE_TTL = Duration.ofMinutes(30);
|
||||||
private static final int LIMIT = 10;
|
private static final int LIMIT = 10;
|
||||||
|
|
||||||
@@ -36,16 +37,17 @@ public class HeadlineController {
|
|||||||
// 실패해도 시각은 갱신 → 피드 장애 때 요청마다 다시 시도하지 않고, 직전 목록을 그대로 보여줌
|
// 실패해도 시각은 갱신 → 피드 장애 때 요청마다 다시 시도하지 않고, 직전 목록을 그대로 보여줌
|
||||||
fetchedAt = Instant.now();
|
fetchedAt = Instant.now();
|
||||||
try {
|
try {
|
||||||
cached = Jsoup.connect(FEED_URL) // feedburner로 리다이렉트됨 (Jsoup이 자동으로 따라감)
|
List<Headline> list = Jsoup.connect(FEED_URL)
|
||||||
.userAgent("Mozilla/5.0")
|
.userAgent("Mozilla/5.0")
|
||||||
.timeout(10_000)
|
.timeout(10_000)
|
||||||
.parser(Parser.xmlParser())
|
|
||||||
.get()
|
.get()
|
||||||
.select("entry")
|
.select(".as_section_headline a.sa_text_title") // 섹션 맨 위 "헤드라인" 묶음의 기사 제목 링크
|
||||||
.stream()
|
.stream()
|
||||||
.limit(LIMIT)
|
.limit(LIMIT)
|
||||||
.map(e -> new Headline(text(e, "title"), text(e, "id"))) // id = utm 없는 깨끗한 글 주소
|
.map(a -> new Headline(text(a, ".sa_text_strong"), a.attr("abs:href")))
|
||||||
|
.filter(h -> !h.title().isEmpty())
|
||||||
.toList();
|
.toList();
|
||||||
|
if (!list.isEmpty()) cached = list; // 화면 구조가 바뀌어 0건이면 직전 목록 유지
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("헤드라인 피드를 가져오지 못했습니다: {}", e.getMessage());
|
log.warn("헤드라인 피드를 가져오지 못했습니다: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user