Files
o2o-site-AEO/solution/backend/tests/test_crawler_detect.py
T
skkim 603189d3a1 [feat] solution/backend,postgres-init: AI 크롤러 요청 기록과 통계 API — 사이트관리 통계의 수집·조회 쪽
크롤러는 JS 를 실행하지 않아 브라우저 트래커로는 안 잡힌다. nginx mirror 가 /s/·robots.txt·
sitemap.xml 요청의 사본을 /internal/track/hit 로 넘기고, 백엔드가 UA 로 봇만 골라
site_crawler_hits 에 1건 = 1행으로 쌓는다(Umami 와 같은 원본 이벤트 방식, 기한 없이 보관).

- crawler_detect: AEORanking 과 같은 19개 봇을 training/search/user-fetch/search-engine 으로 분류,
  목록 밖이지만 스스로 봇이라 밝힌 UA 는 other. HeadlessChrome 은 우리 목업 검증 스크립트라 뺀다
- track_service: 슬러그→site_id 는 발행 때와 같은 publish_slug() 로 만든다 — domain 이 빈
  사이트는 <이름>-<place_id 앞 8자> 로 발행되므로 domain 칼럼만 보면 못 찾는다. 퍼센트 인코딩·NFC 정규화
- /internal/track/hit: /v1 밖이라 nginx 가 밖으로 노출하지 않는다. 저장은 응답을 보낸 뒤 백그라운드 —
  nginx 는 mirror 가 끝나기 전까지 같은 연결의 다음 요청을 읽지 않아서, DB 를 기다렸다 204 를 주면
  DB 가 느릴 때 그 연결의 다른 요청까지 늦어진다
- GET /v1/ops/stats/crawlers · /v1/ops/sites/{place_id}/stats/crawlers (RequireDeveloper):
  일별(KST)×용도 추이, 봇별, 사이트 순위, robots·sitemap, 기타 UA, 많이 읽힌 페이지, 최근 50건
- 0024 마이그레이션과 init.sql 을 함께 수정(둘 중 하나만 고치면 새 DB 와 서버 DB 가 갈라진다)

크롤러 테스트 31건 추가(전체 43건 통과). 전체 스위트 1081 passed · 50 failed 는 LLM 키 미설정·
cp949·스케줄러·SNS·날씨 문구 등 이번 변경과 무관한 기존 실패로 수정 전후 목록이 같다.
2026-10-08 11:31:49 +09:00

53 lines
3.4 KiB
Python

import pytest
from services.crawler_detect import CrawlerMatch, classify
KNOWN = [
("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36; compatible; OAI-SearchBot/1.3; +https://openai.com/searchbot",
CrawlerMatch("OAI-SearchBot", "OpenAI", "search")),
("Mozilla/5.0 (compatible; Yeti/1.1; +https://naver.me/spd)", CrawlerMatch("Yeti", "Naver", "search-engine")),
("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko; compatible; Yeti/1.1; +https://naver.me/spd) Chrome/131.0.0.0 Safari/537.36",
CrawlerMatch("Yeti", "Naver", "search-engine")),
("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 (compatible; meta-externalagent/1.1 (+https://developers.facebook.com/docs/sharing/webmasters/crawler))",
CrawlerMatch("meta-externalagent", "Meta", "training")),
("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/116.0.1938.76 Safari/537.36",
CrawlerMatch("bingbot", "Microsoft", "search-engine")),
("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", CrawlerMatch("Googlebot", "Google", "search-engine")),
("Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.204 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
CrawlerMatch("Googlebot", "Google", "search-engine")),
("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.2; +https://openai.com/gptbot)", CrawlerMatch("GPTBot", "OpenAI", "training")),
("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ChatGPT-User/1.0; +https://openai.com/bot)", CrawlerMatch("ChatGPT-User", "OpenAI", "user-fetch")),
("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; PerplexityBot/1.0; +https://perplexity.ai/perplexitybot)", CrawlerMatch("PerplexityBot", "Perplexity", "search")),
("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)", CrawlerMatch("ClaudeBot", "Anthropic", "training")),
]
OTHER = [
"facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)",
"AIO2O-Bot/1.0 (+https://aio2o.com/bot)",
]
HUMAN_OR_TOOL = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"Mozilla/5.0 (Linux; Android 16; SM-F731N Build/BP4A.251005.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/140.0.0.0 Mobile Safari/537.36 BizWebView KAKAOTALK/26.1.0 (PFINAPP)",
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1 NAVER(inapp; search; 2100; 12.10.5; 16)",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/131.0.0.0 Safari/537.36",
"curl/8.5.0",
"",
None,
]
@pytest.mark.parametrize("ua,expected", KNOWN)
def test_목록_봇은_이름_회사_용도를_붙인다(ua, expected):
assert classify(ua) == expected
@pytest.mark.parametrize("ua", OTHER)
def test_목록_밖_봇은_기타로_남긴다(ua):
assert classify(ua) == CrawlerMatch(None, None, "other")
@pytest.mark.parametrize("ua", HUMAN_OR_TOOL)
def test_사람과_도구는_버린다(ua):
assert classify(ua) is None