232 lines
7.8 KiB
Rust
232 lines
7.8 KiB
Rust
//! Naver Map web search (unofficial, verified 2026-11-04).
|
|
//!
|
|
//! Primary endpoint: `https://map.naver.com/p/api/search/instant-search?query=...&coords=lat,lng`
|
|
//! Fallback endpoint: `https://m.place.naver.com/place/list?query=...&x=lng&y=lat` (parses `window.__APOLLO_STATE__`).
|
|
|
|
use async_trait::async_trait;
|
|
use domain::{Place, PlaceCandidate};
|
|
use serde::Deserialize;
|
|
use serde_json::Value;
|
|
|
|
use crate::{http, text, PlaceSearch};
|
|
|
|
const INSTANT_URL: &str = "https://map.naver.com/p/api/search/instant-search";
|
|
const MPLACE_LIST_URL: &str = "https://m.place.naver.com/place/list";
|
|
|
|
pub struct NaverMapSearch {
|
|
client: reqwest::Client,
|
|
}
|
|
|
|
impl Default for NaverMapSearch {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl NaverMapSearch {
|
|
pub fn new() -> Self {
|
|
Self { client: http::scraping_client() }
|
|
}
|
|
|
|
/// Search places on Naver Map, optionally biased around `(lat, lng)`.
|
|
pub async fn search_near(&self, query: &str, near: Option<(f64, f64)>) -> anyhow::Result<Vec<PlaceCandidate>> {
|
|
let mut out = self.instant_search(query, near).await.unwrap_or_default();
|
|
if out.is_empty() {
|
|
if let Ok(fallback) = self.mobile_list_search(query, near).await {
|
|
out = fallback;
|
|
}
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
async fn instant_search(&self, query: &str, near: Option<(f64, f64)>) -> anyhow::Result<Vec<PlaceCandidate>> {
|
|
let (lat, lng) = near.unwrap_or((37.5665, 126.9780));
|
|
let coords = format!("{lat},{lng}");
|
|
let resp = self
|
|
.client
|
|
.get(INSTANT_URL)
|
|
.header(reqwest::header::REFERER, "https://map.naver.com/")
|
|
.query(&[("query", query), ("coords", &coords)])
|
|
.send()
|
|
.await?
|
|
.error_for_status()?;
|
|
let body: InstantResp = resp.json().await?;
|
|
Ok(parse_instant(body))
|
|
}
|
|
|
|
async fn mobile_list_search(&self, query: &str, near: Option<(f64, f64)>) -> anyhow::Result<Vec<PlaceCandidate>> {
|
|
let (lat, lng) = near.unwrap_or((37.5665, 126.9780));
|
|
let html = self
|
|
.client
|
|
.get(MPLACE_LIST_URL)
|
|
.header(reqwest::header::USER_AGENT, http::MOBILE_UA)
|
|
.query(&[("query", query), ("x", &lng.to_string()), ("y", &lat.to_string())])
|
|
.send()
|
|
.await?
|
|
.error_for_status()?
|
|
.text()
|
|
.await?;
|
|
Ok(parse_apollo_place_list(&html))
|
|
}
|
|
|
|
/// Find the Naver place id matching `place` (name similarity + coordinate distance).
|
|
pub async fn resolve_place_id(&self, place: &Place) -> anyhow::Result<Option<String>> {
|
|
let queries = [
|
|
format!("{} {}", place.name, place.region_hint()),
|
|
place.name.clone(),
|
|
];
|
|
for q in queries {
|
|
let candidates = self.search_near(q.trim(), Some((place.lat, place.lng))).await.unwrap_or_default();
|
|
if let Some(id) = pick_best_match(place, &candidates) {
|
|
tracing::debug!(place = %place.name, naver_id = %id, "resolved naver place id");
|
|
return Ok(Some(id));
|
|
}
|
|
}
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
pub fn pick_best_match(place: &Place, candidates: &[PlaceCandidate]) -> Option<String> {
|
|
candidates
|
|
.iter()
|
|
.filter_map(|c| {
|
|
let sim = text::name_similarity(&place.name, &c.name);
|
|
let dist = text::haversine_m(place.lat, place.lng, c.lat, c.lng);
|
|
// Accept either strong name similarity within 200m, or very close (<50m) with moderate name match.
|
|
if (sim >= 0.55 && dist <= 200.0) || (sim >= 0.35 && dist <= 50.0) {
|
|
let score = sim * 100.0 - (dist as f32) * 0.2;
|
|
Some((score, c.provider_id.clone()))
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.max_by(|a, b| a.0.total_cmp(&b.0))
|
|
.map(|(_, id)| id)
|
|
}
|
|
|
|
fn parse_instant(body: InstantResp) -> Vec<PlaceCandidate> {
|
|
body.place
|
|
.into_iter()
|
|
.filter_map(|p| {
|
|
let lat: f64 = p.y.parse().ok()?;
|
|
let lng: f64 = p.x.parse().ok()?;
|
|
Some(PlaceCandidate {
|
|
provider: "naver".into(),
|
|
place_url: format!("https://m.place.naver.com/place/{}/home", p.id),
|
|
provider_id: p.id,
|
|
name: p.title,
|
|
category: p.ctg,
|
|
address: p.jibun_address,
|
|
road_address: p.road_address,
|
|
phone: String::new(),
|
|
lat,
|
|
lng,
|
|
})
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub fn parse_apollo_place_list(html: &str) -> Vec<PlaceCandidate> {
|
|
let Some(state) = extract_apollo_state(html) else {
|
|
return Vec::new();
|
|
};
|
|
let Some(obj) = state.as_object() else {
|
|
return Vec::new();
|
|
};
|
|
let mut out = Vec::new();
|
|
for (k, v) in obj {
|
|
if !k.starts_with("PlaceListBusinessesItem:") && !k.starts_with("PlaceSummary:") {
|
|
continue;
|
|
}
|
|
let Some(id) = v.get("id").and_then(Value::as_str) else { continue };
|
|
let Some(name) = v.get("name").and_then(Value::as_str) else { continue };
|
|
let lat: f64 = v.get("y").and_then(Value::as_str).and_then(|s| s.parse().ok()).unwrap_or_default();
|
|
let lng: f64 = v.get("x").and_then(Value::as_str).and_then(|s| s.parse().ok()).unwrap_or_default();
|
|
if lat == 0.0 || lng == 0.0 {
|
|
continue;
|
|
}
|
|
let category = v.get("category").and_then(Value::as_str).unwrap_or_default().to_string();
|
|
let address = v
|
|
.get("fullAddress")
|
|
.or_else(|| v.get("address"))
|
|
.and_then(Value::as_str)
|
|
.unwrap_or_default()
|
|
.to_string();
|
|
let road_address = v.get("roadAddress").and_then(Value::as_str).unwrap_or_default().to_string();
|
|
let phone = v
|
|
.get("phone")
|
|
.or_else(|| v.get("virtualPhone"))
|
|
.and_then(Value::as_str)
|
|
.unwrap_or_default()
|
|
.to_string();
|
|
out.push(PlaceCandidate {
|
|
provider: "naver".into(),
|
|
provider_id: id.to_string(),
|
|
name: name.to_string(),
|
|
category,
|
|
address,
|
|
road_address,
|
|
phone,
|
|
lat,
|
|
lng,
|
|
place_url: format!("https://m.place.naver.com/place/{id}/home"),
|
|
});
|
|
}
|
|
out
|
|
}
|
|
|
|
/// Extract the first JSON object assigned to `__APOLLO_STATE__`.
|
|
pub fn extract_apollo_state(html: &str) -> Option<Value> {
|
|
let marker = "__APOLLO_STATE__";
|
|
let idx = html.find(marker)?;
|
|
let rest = &html[idx + marker.len()..];
|
|
let brace = rest.find('{')?;
|
|
let mut de = serde_json::Deserializer::from_str(&rest[brace..]).into_iter::<Value>();
|
|
de.next()?.ok()
|
|
}
|
|
|
|
#[async_trait]
|
|
impl PlaceSearch for NaverMapSearch {
|
|
fn name(&self) -> &'static str {
|
|
"naver"
|
|
}
|
|
|
|
async fn search(&self, query: &str) -> anyhow::Result<Vec<PlaceCandidate>> {
|
|
self.search_near(query, None).await
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct InstantResp {
|
|
#[serde(default)]
|
|
place: Vec<InstantPlace>,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct InstantPlace {
|
|
id: String,
|
|
title: String,
|
|
#[serde(default)]
|
|
ctg: String,
|
|
#[serde(default)]
|
|
jibun_address: String,
|
|
#[serde(default)]
|
|
road_address: String,
|
|
x: String,
|
|
y: String,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn parses_apollo_snippet() {
|
|
let html = r#"<script>window.__APOLLO_STATE__ = {"PlaceListBusinessesItem:37910590":{"id":"37910590","name":"성수동대림창고갤러리","category":"카페,디저트","fullAddress":"서울특별시 성동구 성수이로 78","roadAddress":"성수이로 78","virtualPhone":"0507-1390-9669","x":"127.0563952","y":"37.5417036"}};</script>"#;
|
|
let list = parse_apollo_place_list(html);
|
|
assert_eq!(list.len(), 1);
|
|
assert_eq!(list[0].provider_id, "37910590");
|
|
}
|
|
}
|