-
Notifications
You must be signed in to change notification settings - Fork 9.9k
feat(route): add Naver search RSS route #22025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
slowmande
wants to merge
6
commits into
DIYgod:master
Choose a base branch
from
slowmande:route/naver-search
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8279e9f
route(naver): add Naver search RSS route
slowmande 644a483
fix: use string replaceAll instead of regex in cleanText
slowmande 463fc6f
fix: remove console.log and fix cleanText double-unescaping
slowmande 6c3bb32
fix(naver): address review comments and fix cafe/all category bugs
slowmande eda393d
refactor(naver): split extractItems to reduce cyclomatic complexity
slowmande 11851fd
refactor(naver): split functions to pass CodeFactor complexity check
slowmande File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { Namespace } from '@/types'; | ||
|
|
||
| export const namespace: Namespace = { | ||
| name: '네이버', | ||
| url: 'naver.com', | ||
| lang: 'ko', | ||
| zh: { | ||
| name: 'Naver', | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,301 @@ | ||
| import { load } from 'cheerio'; | ||
|
|
||
| import type { Route } from '@/types'; | ||
| import ofetch from '@/utils/ofetch'; | ||
|
|
||
| export const route: Route = { | ||
| path: '/search/:category/:keyword', | ||
| categories: ['other'], | ||
| example: '/naver/search/all/송소희', | ||
| parameters: { | ||
| category: { | ||
| description: '검색 카테고리. 기본값: all (통합검색)', | ||
| default: 'all', | ||
| options: [ | ||
| { value: 'all', label: '통합검색' }, | ||
| { value: 'blog', label: '블로그' }, | ||
| { value: 'cafe', label: '카페' }, | ||
| { value: 'news', label: '뉴스' }, | ||
| { value: 'video', label: '동영상' }, | ||
| ], | ||
| }, | ||
| keyword: '검색 키워드', | ||
| }, | ||
| features: { | ||
| requireConfig: false, | ||
| requirePuppeteer: false, | ||
| antiCrawler: false, | ||
| supportBT: false, | ||
| supportPodcast: false, | ||
| supportScihub: false, | ||
| }, | ||
| radar: [ | ||
| { | ||
| source: ['m.search.naver.com/search.naver'], | ||
| target: '/search/:category/:keyword', | ||
| }, | ||
| ], | ||
| name: '검색', | ||
| maintainers: ['slowmande'], | ||
| handler, | ||
| }; | ||
|
|
||
| const CATEGORY_CONFIG: Record<string, { url: (keyword: string) => string; templateIds: string[] }> = { | ||
| all: { | ||
| url: (keyword) => `https://m.search.naver.com/search.naver?ssc=tab.m.all&where=m&sm=mtb_opt&query=${encodeURIComponent(keyword)}&nso=so%3Add&nso_open=1`, | ||
| templateIds: ['webItem', 'ugcItem', 'newsItem', 'videoItem'], | ||
| }, | ||
| blog: { | ||
| url: (keyword) => `https://m.search.naver.com/search.naver?ssc=tab.m_blog.all&sm=mtb_jum&query=${encodeURIComponent(keyword)}&nso=so%3Add`, | ||
| templateIds: ['ugcItem'], | ||
| }, | ||
| cafe: { | ||
| url: (keyword) => `https://m.search.naver.com/search.naver?ssc=tab.m_cafe.all&sm=mtb_jum&query=${encodeURIComponent(keyword)}&nso=so%3Add`, | ||
| templateIds: ['webItem', 'ugcItem', 'newsItem', 'videoItem'], | ||
| }, | ||
| news: { | ||
| url: (keyword) => `https://m.search.naver.com/search.naver?ssc=tab.m_news.all&where=m_news&sm=mtb_jum&query=${encodeURIComponent(keyword)}&nso=so%3Add`, | ||
| templateIds: ['newsItem'], | ||
| }, | ||
| video: { | ||
| url: (keyword) => `https://m.search.naver.com/search.naver?ssc=tab.m_video.all&where=m_video&sm=mtb_jum&query=${encodeURIComponent(keyword)}&nso=so%3Add`, | ||
| templateIds: ['videoItem'], | ||
| }, | ||
| }; | ||
|
|
||
| const CATEGORY_NAMES: Record<string, string> = { | ||
| all: '통합검색', | ||
| blog: '블로그', | ||
| cafe: '카페', | ||
| news: '뉴스', | ||
| video: '동영상', | ||
| }; | ||
|
|
||
| async function handler(ctx) { | ||
| const keyword = ctx.req.param('keyword'); | ||
| const category = ctx.req.param('category') || 'all'; | ||
| const config = CATEGORY_CONFIG[category] || CATEGORY_CONFIG.all; | ||
| const url = config.url(keyword); | ||
|
|
||
| const response = await ofetch(url); | ||
|
|
||
| const items = category === 'cafe' ? extractCafeItems(response) : config.templateIds.flatMap((tid) => extractItems(response, tid)); | ||
|
|
||
| return { | ||
| title: `${keyword} - 네이버 ${CATEGORY_NAMES[category] || CATEGORY_NAMES.all}`, | ||
| description: `${keyword}의 네이버 ${CATEGORY_NAMES[category] || CATEGORY_NAMES.all} 검색 결과입니다.`, | ||
| link: url, | ||
| item: items, | ||
| }; | ||
| } | ||
|
|
||
| function extractItems(response: string, templateId: string) { | ||
| const segments = response.split(`"templateId":"${templateId}"`); | ||
| return segments | ||
| .slice(0, -1) | ||
| .map((segment) => (templateId === 'videoItem' ? extractVideoItem(segment) : extractGenericItem(segment, templateId))) | ||
| .filter(Boolean); | ||
| } | ||
|
|
||
| function extractVideoItem(segment: string) { | ||
| const htmlMatch = segment.match(/"html":"((?:[^"\\]|\\.)*)"/); | ||
| const title = htmlMatch ? cleanText(htmlMatch[1]) : ''; | ||
| const authorMatch = segment.match(/"authorHtml":"((?:[^"\\]|\\.)*)"/); | ||
| const author = authorMatch ? cleanText(authorMatch[1]) : ''; | ||
| const hrefMatches = [...segment.matchAll(/"href":"((?:[^"\\]|\\.)*)"/g)]; | ||
| const links = hrefMatches.map((m) => m[1]); | ||
| const mediaDomains = /^(https?:\/\/)?(m\.youtube\.com|youtu\.be|www\.youtube\.com|www\.tiktok\.com|tv\.naver\.com|m\.blog\.naver\.com|m\.cafe\.naver\.com)\//; | ||
| const link = links.find((l) => mediaDomains.test(l)) || links[0] || ''; | ||
| const dateMatch = segment.match(/"createdAt":"((?:[^"\\]|\\.)*)"/); | ||
| const timeText = dateMatch?.[1] || ''; | ||
| const viewMatch = segment.match(/"viewCount":"((?:[^"\\]|\\.)*)"/); | ||
| const viewCount = viewMatch ? cleanText(viewMatch[1]) : ''; | ||
| const durationMatch = segment.match(/"playDuration":(\d+)/); | ||
| const duration = durationMatch ? `${durationMatch[1]}초` : ''; | ||
|
|
||
| const hasVideoFields = segment.includes('"videoPlayerType"') || segment.includes('"playDuration"'); | ||
| if (!title || !link || !timeText || !hasVideoFields) { | ||
| return null; | ||
| } | ||
|
|
||
| const pubDate = parseKoreanRelativeTime(timeText); | ||
| return buildVideoResult(title, link, viewCount, duration, author, pubDate); | ||
| } | ||
|
|
||
| function buildVideoResult(title: string, link: string, viewCount: string, duration: string, author: string, pubDate: Date | undefined) { | ||
| return { | ||
| title, | ||
| link, | ||
| description: buildVideoDescription(viewCount, duration), | ||
| author: author || undefined, | ||
| ...(pubDate && { pubDate }), | ||
| }; | ||
| } | ||
|
|
||
| function buildVideoDescription(viewCount: string, duration: string): string { | ||
| const parts: string[] = []; | ||
| if (viewCount) { | ||
| parts.push(`조회수: ${viewCount}`); | ||
| } | ||
| if (duration && duration !== '0초') { | ||
| parts.push(`재생시간: ${duration}`); | ||
| } | ||
| return parts.map((p) => `<p>${p}</p>`).join(''); | ||
| } | ||
|
|
||
| function extractLink(segment: string, templateId: string): string { | ||
| if (templateId === 'webItem') { | ||
| const hrefMatch = segment.match(/"href":"((?:[^"\\]|\\.)*)"/); | ||
| return hrefMatch?.[1] || ''; | ||
| } | ||
| const hrefMatches = [...segment.matchAll(/"titleHref":"((?:[^"\\]|\\.)*)"/g)]; | ||
| if (hrefMatches.length > 0) { | ||
| return hrefMatches.at(-1)![1]; | ||
| } | ||
| const hrefMatch = segment.match(/"href":"((?:[^"\\]|\\.)*)"/); | ||
| return hrefMatch?.[1] || ''; | ||
| } | ||
|
|
||
| function extractGenericItem(segment: string, templateId: string) { | ||
| const titleMatches = [...segment.matchAll(/"title":"((?:[^"\\]|\\.)*)"/g)]; | ||
| const titles = titleMatches.map((m) => cleanText(m[1])); | ||
| const title = titles.at(-1) || ''; | ||
| const sourceName = titles.at(-2) || ''; | ||
| const link = extractLink(segment, templateId); | ||
|
|
||
| let bodyText = ''; | ||
| const bodyMatch = segment.match(/"bodyText":"((?:[^"\\]|\\.)*)"/) || segment.match(/"content":"((?:[^"\\]|\\.)*)"/); | ||
| if (bodyMatch) { | ||
| bodyText = cleanText(bodyMatch[1]); | ||
| } | ||
|
|
||
| if (!title || !link) { | ||
| return null; | ||
| } | ||
|
|
||
| if (title === '더보기' || title === '관련도순' || title === '최신순') { | ||
| return null; | ||
| } | ||
|
|
||
| return buildItemFromTemplate(segment, templateId, title, link, bodyText, sourceName); | ||
| } | ||
|
|
||
| function buildItemFromTemplate(segment: string, templateId: string, title: string, link: string, bodyText: string, sourceName: string) { | ||
| if (templateId === 'ugcItem') { | ||
| const dateMatch = segment.match(/"createdDate":"([^"]*)"/); | ||
| if (dateMatch?.[1]) { | ||
| return { title, link, description: bodyText, author: sourceName || undefined, pubDate: new Date(dateMatch[1]) }; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| let timeText = ''; | ||
| if (templateId === 'webItem') { | ||
| const timeMatch = segment.match(/\[{"text":"([^"]*)"}/); | ||
| timeText = timeMatch?.[1] || ''; | ||
| } else { | ||
| const textMatch = segment.match(/"text":"([^"]*)"/); | ||
| timeText = textMatch?.[1] || ''; | ||
| } | ||
|
|
||
| const pubDate = parseKoreanRelativeTime(timeText); | ||
| return { title, link, description: bodyText, author: sourceName || undefined, ...(pubDate && { pubDate }) }; | ||
| } | ||
|
|
||
| function extractCafeItems(html: string) { | ||
| const $ = load(html); | ||
| const items: Array<{ | ||
| title: string; | ||
| link: string; | ||
| description: string; | ||
| author?: string; | ||
| pubDate?: Date; | ||
| }> = []; | ||
|
|
||
| $('li.bx').each((_i, el) => { | ||
| const $el = $(el); | ||
| const titleEl = $el.find('.title_link'); | ||
| const title = titleEl.text().trim(); | ||
| const link = titleEl.attr('href') || ''; | ||
| const author = $el.find('.name').first().text().trim(); | ||
| const timeText = $el.find('.sub').first().text().trim(); | ||
| const descEl = $el.find('.dsc_link'); | ||
| const description = descEl.length ? descEl.text().trim() : ''; | ||
|
|
||
| if (!title || !link) { | ||
| return; | ||
| } | ||
|
|
||
| const pubDate = parseKoreanRelativeTime(timeText); | ||
|
|
||
| items.push({ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Array.prototype.map instead of Array.prototype.push |
||
| title, | ||
| link, | ||
| description, | ||
| author: author || undefined, | ||
| ...(pubDate && { pubDate }), | ||
| }); | ||
| }); | ||
|
|
||
| return items; | ||
| } | ||
|
|
||
| function cleanText(text: string): string { | ||
| return text | ||
| .replaceAll(/&(amp|lt|gt|quot);/g, (match) => { | ||
| switch (match) { | ||
| case '&': | ||
| return '&'; | ||
| case '<': | ||
| return '<'; | ||
| case '>': | ||
| return '>'; | ||
| case '"': | ||
| return '"'; | ||
| default: | ||
| return match; | ||
| } | ||
| }) | ||
|
Comment on lines
+245
to
+258
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use entities instead. |
||
| .replaceAll('<mark>', '') | ||
| .replaceAll('</mark>', ''); | ||
| } | ||
|
|
||
| function parseKoreanRelativeTime(timeText: string): Date | undefined { | ||
| const now = new Date(); | ||
| if (!timeText) { | ||
| return; | ||
| } | ||
|
|
||
| // Try absolute date formats first (e.g. "2025.12.01.", "2026-05-13T23:15:00+09:00") | ||
| const absDate = new Date(timeText); | ||
| if (!Number.isNaN(absDate.getTime())) { | ||
| return absDate; | ||
| } | ||
|
|
||
| const match = timeText.match(/(\d+)\s*(시간|분|일|주) 전|(\d+)분 이내|(\d+)시간 이내|방금/); | ||
| if (!match) { | ||
| return; | ||
| } | ||
|
|
||
| if (match[0] === '방금') { | ||
| return; | ||
| } | ||
|
|
||
| const num = Number.parseInt(match[1] || match[3] || match[4] || '0', 10); | ||
| const unit = match[2] || ''; | ||
|
|
||
| if (unit.includes('분')) { | ||
| return new Date(now.getTime() - num * 60 * 1000); | ||
| } | ||
| if (unit.includes('시간')) { | ||
| return new Date(now.getTime() - num * 60 * 60 * 1000); | ||
| } | ||
| if (unit.includes('일')) { | ||
| return new Date(now.getTime() - num * 24 * 60 * 60 * 1000); | ||
| } | ||
| if (unit.includes('주')) { | ||
| return new Date(now.getTime() - num * 7 * 24 * 60 * 60 * 1000); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not reinvent the wheels
RSSHub/lib/types.ts
Line 33 in 230749f