feat: 초기 프로젝트 설정 및 LunchPick 앱 구현
LunchPick(오늘 뭐 먹Z?) Flutter 앱의 초기 구현입니다. 주요 기능: - 네이버 지도 연동 맛집 추가 - 랜덤 메뉴 추천 시스템 - 날씨 기반 거리 조정 - 방문 기록 관리 - Bluetooth 맛집 공유 - 다크모드 지원 기술 스택: - Flutter 3.8.1+ - Riverpod 상태 관리 - Hive 로컬 DB - Clean Architecture 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
126
lib/data/api/converters/naver_data_converter.dart
Normal file
126
lib/data/api/converters/naver_data_converter.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../../../domain/entities/restaurant.dart';
|
||||
import '../naver/naver_local_search_api.dart';
|
||||
import '../../../core/utils/category_mapper.dart';
|
||||
|
||||
/// 네이버 데이터 변환기
|
||||
///
|
||||
/// 네이버 API 응답을 도메인 엔티티로 변환합니다.
|
||||
class NaverDataConverter {
|
||||
static const _uuid = Uuid();
|
||||
|
||||
/// NaverLocalSearchResult를 Restaurant 엔티티로 변환
|
||||
static Restaurant fromLocalSearchResult(
|
||||
NaverLocalSearchResult result, {
|
||||
String? id,
|
||||
}) {
|
||||
// 좌표 변환 (네이버 지도 좌표계 -> WGS84)
|
||||
final convertedCoords = _convertNaverMapCoordinates(
|
||||
result.mapx,
|
||||
result.mapy,
|
||||
);
|
||||
|
||||
// 카테고리 파싱 및 정규화
|
||||
final categoryParts = result.category.split('>').map((s) => s.trim()).toList();
|
||||
final mainCategory = categoryParts.isNotEmpty ? categoryParts.first : '음식점';
|
||||
final subCategory = categoryParts.length > 1 ? categoryParts.last : mainCategory;
|
||||
|
||||
// CategoryMapper를 사용한 정규화
|
||||
final normalizedCategory = CategoryMapper.normalizeNaverCategory(mainCategory, subCategory);
|
||||
|
||||
return Restaurant(
|
||||
id: id ?? _uuid.v4(),
|
||||
name: result.title,
|
||||
category: normalizedCategory,
|
||||
subCategory: subCategory,
|
||||
description: result.description.isNotEmpty ? result.description : null,
|
||||
phoneNumber: result.telephone.isNotEmpty ? result.telephone : null,
|
||||
roadAddress: result.roadAddress.isNotEmpty
|
||||
? result.roadAddress
|
||||
: result.address,
|
||||
jibunAddress: result.address,
|
||||
latitude: convertedCoords['latitude'] ?? 37.5665,
|
||||
longitude: convertedCoords['longitude'] ?? 126.9780,
|
||||
naverUrl: result.link.isNotEmpty ? result.link : null,
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
source: DataSource.NAVER,
|
||||
);
|
||||
}
|
||||
|
||||
/// GraphQL 응답을 Restaurant 엔티티로 변환
|
||||
static Restaurant fromGraphQLResponse(
|
||||
Map<String, dynamic> placeData, {
|
||||
String? id,
|
||||
String? naverUrl,
|
||||
}) {
|
||||
// 영업시간 파싱
|
||||
String? businessHours;
|
||||
if (placeData['businessHours'] != null) {
|
||||
final hours = placeData['businessHours'] as List;
|
||||
businessHours = hours
|
||||
.where((h) => h['businessHours'] != null)
|
||||
.map((h) => h['businessHours'])
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
// 좌표 추출
|
||||
double? latitude;
|
||||
double? longitude;
|
||||
if (placeData['location'] != null) {
|
||||
latitude = placeData['location']['latitude']?.toDouble();
|
||||
longitude = placeData['location']['longitude']?.toDouble();
|
||||
}
|
||||
|
||||
// 카테고리 파싱 및 정규화
|
||||
final rawCategory = placeData['category'] ?? '음식점';
|
||||
final categoryParts = rawCategory.split('>').map((s) => s.trim()).toList();
|
||||
final mainCategory = categoryParts.isNotEmpty ? categoryParts.first : '음식점';
|
||||
final subCategory = categoryParts.length > 1 ? categoryParts.last : mainCategory;
|
||||
|
||||
// CategoryMapper를 사용한 정규화
|
||||
final normalizedCategory = CategoryMapper.normalizeNaverCategory(mainCategory, subCategory);
|
||||
|
||||
return Restaurant(
|
||||
id: id ?? _uuid.v4(),
|
||||
name: placeData['name'] ?? '이름 없음',
|
||||
category: normalizedCategory,
|
||||
subCategory: subCategory,
|
||||
description: placeData['description'],
|
||||
phoneNumber: placeData['phone'],
|
||||
roadAddress: placeData['address']?['roadAddress'] ?? '',
|
||||
jibunAddress: placeData['address']?['jibunAddress'] ?? '',
|
||||
latitude: latitude ?? 37.5665,
|
||||
longitude: longitude ?? 126.9780,
|
||||
businessHours: businessHours,
|
||||
naverUrl: naverUrl,
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
source: DataSource.NAVER,
|
||||
);
|
||||
}
|
||||
|
||||
/// 네이버 지도 좌표를 WGS84로 변환
|
||||
static Map<String, double?> _convertNaverMapCoordinates(
|
||||
double? mapx,
|
||||
double? mapy,
|
||||
) {
|
||||
if (mapx == null || mapy == null) {
|
||||
return {'latitude': null, 'longitude': null};
|
||||
}
|
||||
|
||||
// 네이버 지도 좌표계는 KATEC을 사용
|
||||
// 간단한 변환 공식 (정확도는 떨어지지만 실용적)
|
||||
// 실제로는 더 정교한 변환이 필요할 수 있음
|
||||
final longitude = mapx / 10000000.0;
|
||||
final latitude = mapy / 10000000.0;
|
||||
|
||||
return {
|
||||
'latitude': latitude,
|
||||
'longitude': longitude,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
167
lib/data/api/naver/naver_graphql_api.dart
Normal file
167
lib/data/api/naver/naver_graphql_api.dart
Normal file
@@ -0,0 +1,167 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../core/network/network_client.dart';
|
||||
import '../../../core/errors/network_exceptions.dart';
|
||||
|
||||
/// 네이버 GraphQL API 클라이언트
|
||||
///
|
||||
/// 네이버 지도의 GraphQL API를 호출하여 상세 정보를 가져옵니다.
|
||||
class NaverGraphQLApi {
|
||||
final NetworkClient _networkClient;
|
||||
|
||||
static const String _graphqlEndpoint = 'https://pcmap-api.place.naver.com/graphql';
|
||||
|
||||
NaverGraphQLApi({NetworkClient? networkClient})
|
||||
: _networkClient = networkClient ?? NetworkClient();
|
||||
|
||||
/// GraphQL 쿼리 실행
|
||||
Future<Map<String, dynamic>> fetchGraphQL({
|
||||
required String operationName,
|
||||
required String query,
|
||||
Map<String, dynamic>? variables,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _networkClient.post<Map<String, dynamic>>(
|
||||
_graphqlEndpoint,
|
||||
data: {
|
||||
'operationName': operationName,
|
||||
'query': query,
|
||||
'variables': variables ?? {},
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Referer': 'https://map.naver.com/',
|
||||
'Origin': 'https://map.naver.com',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.data == null) {
|
||||
throw ParseException(
|
||||
message: 'GraphQL 응답이 비어있습니다',
|
||||
);
|
||||
}
|
||||
|
||||
return response.data!;
|
||||
} on DioException catch (e) {
|
||||
debugPrint('fetchGraphQL error: $e');
|
||||
throw ServerException(
|
||||
message: 'GraphQL 요청 중 오류가 발생했습니다',
|
||||
statusCode: e.response?.statusCode ?? 500,
|
||||
originalError: e,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 장소 상세 정보 가져오기 (한국어 텍스트)
|
||||
Future<Map<String, dynamic>> fetchKoreanTextsFromPcmap(String placeId) async {
|
||||
const query = '''
|
||||
query getKoreanTexts(\$id: String!) {
|
||||
place(input: { id: \$id }) {
|
||||
id
|
||||
name
|
||||
category
|
||||
businessHours {
|
||||
description
|
||||
isDayOff
|
||||
openTime
|
||||
closeTime
|
||||
dayOfWeek
|
||||
businessHours
|
||||
}
|
||||
phone
|
||||
address {
|
||||
roadAddress
|
||||
jibunAddress
|
||||
}
|
||||
description
|
||||
menuInfo {
|
||||
menus {
|
||||
name
|
||||
price
|
||||
description
|
||||
images {
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
keywords
|
||||
priceCategory
|
||||
imageCount
|
||||
visitorReviewCount
|
||||
visitorReviewScore
|
||||
}
|
||||
}
|
||||
''';
|
||||
|
||||
try {
|
||||
final response = await fetchGraphQL(
|
||||
operationName: 'getKoreanTexts',
|
||||
query: query,
|
||||
variables: {'id': placeId},
|
||||
);
|
||||
|
||||
if (response['errors'] != null) {
|
||||
debugPrint('GraphQL errors: ${response['errors']}');
|
||||
throw ParseException(
|
||||
message: 'GraphQL 오류: ${response['errors']}',
|
||||
);
|
||||
}
|
||||
|
||||
return response['data']?['place'] ?? {};
|
||||
} catch (e) {
|
||||
debugPrint('fetchKoreanTextsFromPcmap error: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// 장소 기본 정보 가져오기
|
||||
Future<Map<String, dynamic>> fetchPlaceBasicInfo(String placeId) async {
|
||||
const query = '''
|
||||
query getPlaceBasicInfo(\$id: String!) {
|
||||
place(input: { id: \$id }) {
|
||||
id
|
||||
name
|
||||
category
|
||||
phone
|
||||
address {
|
||||
roadAddress
|
||||
jibunAddress
|
||||
}
|
||||
location {
|
||||
latitude
|
||||
longitude
|
||||
}
|
||||
homepageUrl
|
||||
bookingUrl
|
||||
}
|
||||
}
|
||||
''';
|
||||
|
||||
try {
|
||||
final response = await fetchGraphQL(
|
||||
operationName: 'getPlaceBasicInfo',
|
||||
query: query,
|
||||
variables: {'id': placeId},
|
||||
);
|
||||
|
||||
if (response['errors'] != null) {
|
||||
throw ParseException(
|
||||
message: 'GraphQL 오류: ${response['errors']}',
|
||||
);
|
||||
}
|
||||
|
||||
return response['data']?['place'] ?? {};
|
||||
} catch (e) {
|
||||
debugPrint('fetchPlaceBasicInfo error: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
// 필요시 리소스 정리
|
||||
}
|
||||
}
|
||||
52
lib/data/api/naver/naver_graphql_queries.dart
Normal file
52
lib/data/api/naver/naver_graphql_queries.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
/// \ub124\uc774\ubc84 \uc9c0\ub3c4 GraphQL \ucffc\ub9ac \ubaa8\uc74c
|
||||
///
|
||||
/// \ub124\uc774\ubc84 \uc9c0\ub3c4 API\uc5d0\uc11c \uc0ac\uc6a9\ud558\ub294 GraphQL \ucffc\ub9ac\ub4e4\uc744 \uad00\ub9ac\ud569\ub2c8\ub2e4.
|
||||
class NaverGraphQLQueries {
|
||||
NaverGraphQLQueries._();
|
||||
|
||||
/// \uc7a5\uc18c \uc0c1\uc138 \uc815\ubcf4 \ucffc\ub9ac - places \uc0ac\uc6a9
|
||||
static const String placeDetailQuery = '''
|
||||
query getPlaceDetail(\$id: String!) {
|
||||
places(id: \$id) {
|
||||
id
|
||||
name
|
||||
category
|
||||
address
|
||||
roadAddress
|
||||
phone
|
||||
virtualPhone
|
||||
businessHours {
|
||||
description
|
||||
}
|
||||
description
|
||||
location {
|
||||
lat
|
||||
lng
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
|
||||
/// \uc7a5\uc18c \uc0c1\uc138 \uc815\ubcf4 \ucffc\ub9ac - nxPlaces \uc0ac\uc6a9 (\ud3f4\ubc31)
|
||||
static const String nxPlaceDetailQuery = '''
|
||||
query getPlaceDetail(\$id: String!) {
|
||||
nxPlaces(id: \$id) {
|
||||
id
|
||||
name
|
||||
category
|
||||
address
|
||||
roadAddress
|
||||
phone
|
||||
virtualPhone
|
||||
businessHours {
|
||||
description
|
||||
}
|
||||
description
|
||||
location {
|
||||
lat
|
||||
lng
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
}
|
||||
197
lib/data/api/naver/naver_local_search_api.dart
Normal file
197
lib/data/api/naver/naver_local_search_api.dart
Normal file
@@ -0,0 +1,197 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../core/constants/api_keys.dart';
|
||||
import '../../../core/network/network_client.dart';
|
||||
import '../../../core/errors/network_exceptions.dart';
|
||||
|
||||
/// 네이버 로컬 검색 API 결과 모델
|
||||
class NaverLocalSearchResult {
|
||||
final String title;
|
||||
final String link;
|
||||
final String category;
|
||||
final String description;
|
||||
final String telephone;
|
||||
final String address;
|
||||
final String roadAddress;
|
||||
final double? mapx;
|
||||
final double? mapy;
|
||||
|
||||
NaverLocalSearchResult({
|
||||
required this.title,
|
||||
required this.link,
|
||||
required this.category,
|
||||
required this.description,
|
||||
required this.telephone,
|
||||
required this.address,
|
||||
required this.roadAddress,
|
||||
this.mapx,
|
||||
this.mapy,
|
||||
});
|
||||
|
||||
factory NaverLocalSearchResult.fromJson(Map<String, dynamic> json) {
|
||||
// HTML 태그 제거 헬퍼 함수
|
||||
String removeHtmlTags(String text) {
|
||||
return text
|
||||
.replaceAll(RegExp(r'<[^>]*>'), '')
|
||||
.replaceAll('&', '&')
|
||||
.replaceAll('<', '<')
|
||||
.replaceAll('>', '>')
|
||||
.replaceAll('"', '"')
|
||||
.replaceAll(''', "'")
|
||||
.replaceAll(' ', ' ');
|
||||
}
|
||||
|
||||
return NaverLocalSearchResult(
|
||||
title: removeHtmlTags(json['title'] ?? ''),
|
||||
link: json['link'] ?? '',
|
||||
category: json['category'] ?? '',
|
||||
description: removeHtmlTags(json['description'] ?? ''),
|
||||
telephone: json['telephone'] ?? '',
|
||||
address: json['address'] ?? '',
|
||||
roadAddress: json['roadAddress'] ?? '',
|
||||
mapx: json['mapx'] != null ? double.tryParse(json['mapx'].toString()) : null,
|
||||
mapy: json['mapy'] != null ? double.tryParse(json['mapy'].toString()) : null,
|
||||
);
|
||||
}
|
||||
|
||||
/// link 필드에서 Place ID 추출
|
||||
///
|
||||
/// link가 비어있거나 Place ID가 없으면 null 반환
|
||||
String? extractPlaceId() {
|
||||
if (link.isEmpty) return null;
|
||||
|
||||
// 네이버 지도 URL 패턴에서 Place ID 추출
|
||||
// 예: https://map.naver.com/p/entry/place/1638379069
|
||||
final placeIdMatch = RegExp(r'/place/(\d+)').firstMatch(link);
|
||||
if (placeIdMatch != null) {
|
||||
return placeIdMatch.group(1);
|
||||
}
|
||||
|
||||
// 다른 패턴 시도: restaurant/1638379069
|
||||
final restaurantIdMatch = RegExp(r'/restaurant/(\d+)').firstMatch(link);
|
||||
if (restaurantIdMatch != null) {
|
||||
return restaurantIdMatch.group(1);
|
||||
}
|
||||
|
||||
// ID만 있는 경우 (10자리 숫자)
|
||||
final idOnlyMatch = RegExp(r'(\d{10})').firstMatch(link);
|
||||
if (idOnlyMatch != null) {
|
||||
return idOnlyMatch.group(1);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// 네이버 로컬 검색 API 클라이언트
|
||||
///
|
||||
/// 네이버 검색 API를 통해 장소 정보를 검색합니다.
|
||||
class NaverLocalSearchApi {
|
||||
final NetworkClient _networkClient;
|
||||
|
||||
NaverLocalSearchApi({NetworkClient? networkClient})
|
||||
: _networkClient = networkClient ?? NetworkClient();
|
||||
|
||||
/// 로컬 검색 API 호출
|
||||
///
|
||||
/// 검색어와 좌표를 기반으로 주변 식당을 검색합니다.
|
||||
Future<List<NaverLocalSearchResult>> searchLocal({
|
||||
required String query,
|
||||
double? latitude,
|
||||
double? longitude,
|
||||
int display = 20,
|
||||
int start = 1,
|
||||
String sort = 'random', // random, comment
|
||||
}) async {
|
||||
// API 키 확인
|
||||
if (!ApiKeys.areKeysConfigured()) {
|
||||
throw ApiKeyException();
|
||||
}
|
||||
|
||||
try {
|
||||
final response = await _networkClient.get<Map<String, dynamic>>(
|
||||
ApiKeys.naverLocalSearchEndpoint,
|
||||
queryParameters: {
|
||||
'query': query,
|
||||
'display': display,
|
||||
'start': start,
|
||||
'sort': sort,
|
||||
if (latitude != null && longitude != null) ...{
|
||||
'coordinate': '$longitude,$latitude', // 경도,위도 순서
|
||||
},
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'X-Naver-Client-Id': ApiKeys.naverClientId,
|
||||
'X-Naver-Client-Secret': ApiKeys.naverClientSecret,
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
final data = response.data;
|
||||
if (data == null || data['items'] == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final items = data['items'] as List;
|
||||
return items
|
||||
.map((item) => NaverLocalSearchResult.fromJson(item))
|
||||
.toList();
|
||||
} on DioException catch (e) {
|
||||
debugPrint('NaverLocalSearchApi Error: ${e.message}');
|
||||
debugPrint('Error type: ${e.type}');
|
||||
debugPrint('Error response: ${e.response?.data}');
|
||||
|
||||
if (e.error is NetworkException) {
|
||||
throw e.error!;
|
||||
}
|
||||
throw ServerException(
|
||||
message: '네이버 검색 중 오류가 발생했습니다',
|
||||
statusCode: e.response?.statusCode ?? 500,
|
||||
originalError: e,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 특정 식당 상세 정보 검색
|
||||
Future<NaverLocalSearchResult?> searchRestaurantDetails({
|
||||
required String name,
|
||||
required String address,
|
||||
double? latitude,
|
||||
double? longitude,
|
||||
}) async {
|
||||
try {
|
||||
// 주소와 이름을 조합한 검색어
|
||||
final query = '$name $address';
|
||||
final results = await searchLocal(
|
||||
query: query,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
display: 5,
|
||||
sort: 'comment', // 정확도순
|
||||
);
|
||||
|
||||
if (results.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 가장 정확한 결과 찾기
|
||||
for (final result in results) {
|
||||
if (result.title.contains(name) || name.contains(result.title)) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// 정확한 매칭이 없으면 첫 번째 결과 반환
|
||||
return results.first;
|
||||
} catch (e) {
|
||||
debugPrint('searchRestaurantDetails error: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
// 필요시 리소스 정리
|
||||
}
|
||||
}
|
||||
101
lib/data/api/naver/naver_proxy_client.dart
Normal file
101
lib/data/api/naver/naver_proxy_client.dart
Normal file
@@ -0,0 +1,101 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../core/network/network_client.dart';
|
||||
import '../../../core/network/network_config.dart';
|
||||
import '../../../core/errors/network_exceptions.dart';
|
||||
|
||||
/// 네이버 프록시 클라이언트
|
||||
///
|
||||
/// 웹 환경에서 CORS 문제를 해결하기 위한 프록시 클라이언트입니다.
|
||||
class NaverProxyClient {
|
||||
final NetworkClient _networkClient;
|
||||
|
||||
NaverProxyClient({NetworkClient? networkClient})
|
||||
: _networkClient = networkClient ?? NetworkClient();
|
||||
|
||||
/// 웹 환경에서 프록시를 통해 HTML 가져오기
|
||||
Future<String> fetchViaProxy(String url) async {
|
||||
if (!kIsWeb) {
|
||||
throw UnsupportedError('프록시는 웹 환경에서만 사용 가능합니다');
|
||||
}
|
||||
|
||||
try {
|
||||
final proxyUrl = NetworkConfig.getCorsProxyUrl(url);
|
||||
debugPrint('Using proxy URL: $proxyUrl');
|
||||
|
||||
final response = await _networkClient.get<String>(
|
||||
proxyUrl,
|
||||
options: Options(
|
||||
responseType: ResponseType.plain,
|
||||
headers: {
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
||||
'Accept-Language': 'ko-KR,ko;q=0.9,en;q=0.8',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.data == null || response.data!.isEmpty) {
|
||||
throw ParseException(
|
||||
message: '프록시 응답이 비어있습니다',
|
||||
);
|
||||
}
|
||||
|
||||
return response.data!;
|
||||
} on DioException catch (e) {
|
||||
debugPrint('Proxy fetch error: ${e.message}');
|
||||
debugPrint('Status code: ${e.response?.statusCode}');
|
||||
debugPrint('Response: ${e.response?.data}');
|
||||
|
||||
if (e.response?.statusCode == 403) {
|
||||
throw ServerException(
|
||||
message: 'CORS 프록시 접근이 거부되었습니다. 잠시 후 다시 시도해주세요.',
|
||||
statusCode: 403,
|
||||
originalError: e,
|
||||
);
|
||||
}
|
||||
|
||||
throw ServerException(
|
||||
message: '프록시를 통한 페이지 로드에 실패했습니다',
|
||||
statusCode: e.response?.statusCode ?? 500,
|
||||
originalError: e,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 프록시 상태 확인
|
||||
Future<bool> checkProxyStatus() async {
|
||||
if (!kIsWeb) {
|
||||
return true; // 웹이 아니면 프록시 불필요
|
||||
}
|
||||
|
||||
try {
|
||||
final testUrl = 'https://map.naver.com';
|
||||
final proxyUrl = NetworkConfig.getCorsProxyUrl(testUrl);
|
||||
|
||||
final response = await _networkClient.head(
|
||||
proxyUrl,
|
||||
options: Options(
|
||||
validateStatus: (status) => status! < 500,
|
||||
),
|
||||
);
|
||||
|
||||
return response.statusCode == 200;
|
||||
} catch (e) {
|
||||
debugPrint('Proxy status check failed: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 프록시 URL 생성
|
||||
String getProxyUrl(String originalUrl) {
|
||||
if (!kIsWeb) {
|
||||
return originalUrl;
|
||||
}
|
||||
return NetworkConfig.getCorsProxyUrl(originalUrl);
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
// 필요시 리소스 정리
|
||||
}
|
||||
}
|
||||
151
lib/data/api/naver/naver_url_resolver.dart
Normal file
151
lib/data/api/naver/naver_url_resolver.dart
Normal file
@@ -0,0 +1,151 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../core/network/network_client.dart';
|
||||
import '../../../core/network/network_config.dart';
|
||||
|
||||
/// 네이버 URL 리졸버
|
||||
///
|
||||
/// 네이버 단축 URL을 실제 URL로 변환하고 최종 리다이렉트 URL을 추적합니다.
|
||||
class NaverUrlResolver {
|
||||
final NetworkClient _networkClient;
|
||||
|
||||
NaverUrlResolver({NetworkClient? networkClient})
|
||||
: _networkClient = networkClient ?? NetworkClient();
|
||||
|
||||
/// 단축 URL을 실제 URL로 변환
|
||||
Future<String> resolveShortUrl(String shortUrl) async {
|
||||
try {
|
||||
// 웹 환경에서는 CORS 프록시 사용
|
||||
if (kIsWeb) {
|
||||
return await _resolveShortUrlViaProxy(shortUrl);
|
||||
}
|
||||
|
||||
// 모바일 환경에서는 직접 HEAD 요청
|
||||
final response = await _networkClient.head(
|
||||
shortUrl,
|
||||
options: Options(
|
||||
followRedirects: false,
|
||||
validateStatus: (status) => status! < 400,
|
||||
),
|
||||
);
|
||||
|
||||
// Location 헤더에서 리다이렉트 URL 추출
|
||||
final location = response.headers.value('location');
|
||||
if (location != null) {
|
||||
return location;
|
||||
}
|
||||
|
||||
// 리다이렉트가 없으면 원본 URL 반환
|
||||
return shortUrl;
|
||||
} on DioException catch (e) {
|
||||
debugPrint('resolveShortUrl error: $e');
|
||||
|
||||
// 리다이렉트 응답인 경우 Location 헤더 확인
|
||||
if (e.response?.statusCode == 301 || e.response?.statusCode == 302) {
|
||||
final location = e.response?.headers.value('location');
|
||||
if (location != null) {
|
||||
return location;
|
||||
}
|
||||
}
|
||||
|
||||
// 오류 발생 시 원본 URL 반환
|
||||
return shortUrl;
|
||||
}
|
||||
}
|
||||
|
||||
/// 프록시를 통한 단축 URL 해결 (웹 환경)
|
||||
Future<String> _resolveShortUrlViaProxy(String shortUrl) async {
|
||||
try {
|
||||
final proxyUrl = NetworkConfig.getCorsProxyUrl(shortUrl);
|
||||
|
||||
final response = await _networkClient.get(
|
||||
proxyUrl,
|
||||
options: Options(
|
||||
followRedirects: false,
|
||||
validateStatus: (status) => true,
|
||||
responseType: ResponseType.plain,
|
||||
),
|
||||
);
|
||||
|
||||
// 응답에서 URL 정보 추출
|
||||
final responseData = response.data.toString();
|
||||
|
||||
// meta refresh 태그에서 URL 추출
|
||||
final metaRefreshRegex = RegExp(
|
||||
'<meta[^>]+http-equiv="refresh"[^>]+content="0;url=([^"]+)"[^>]*>',
|
||||
caseSensitive: false,
|
||||
);
|
||||
final metaMatch = metaRefreshRegex.firstMatch(responseData);
|
||||
if (metaMatch != null) {
|
||||
return metaMatch.group(1) ?? shortUrl;
|
||||
}
|
||||
|
||||
// og:url 메타 태그에서 URL 추출
|
||||
final ogUrlRegex = RegExp(
|
||||
'<meta[^>]+property="og:url"[^>]+content="([^"]+)"[^>]*>',
|
||||
caseSensitive: false,
|
||||
);
|
||||
final ogMatch = ogUrlRegex.firstMatch(responseData);
|
||||
if (ogMatch != null) {
|
||||
return ogMatch.group(1) ?? shortUrl;
|
||||
}
|
||||
|
||||
// Location 헤더 확인
|
||||
final location = response.headers.value('location');
|
||||
if (location != null) {
|
||||
return location;
|
||||
}
|
||||
|
||||
return shortUrl;
|
||||
} catch (e) {
|
||||
debugPrint('_resolveShortUrlViaProxy error: $e');
|
||||
return shortUrl;
|
||||
}
|
||||
}
|
||||
|
||||
/// 최종 리다이렉트 URL 가져오기
|
||||
///
|
||||
/// 여러 단계의 리다이렉트를 거쳐 최종 URL을 반환합니다.
|
||||
Future<String> getFinalRedirectUrl(String url) async {
|
||||
try {
|
||||
String currentUrl = url;
|
||||
int redirectCount = 0;
|
||||
const maxRedirects = 5;
|
||||
|
||||
while (redirectCount < maxRedirects) {
|
||||
final response = await _networkClient.head(
|
||||
currentUrl,
|
||||
options: Options(
|
||||
followRedirects: false,
|
||||
validateStatus: (status) => status! < 400,
|
||||
),
|
||||
);
|
||||
|
||||
final location = response.headers.value('location');
|
||||
if (location == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
// 절대 URL로 변환
|
||||
if (location.startsWith('/')) {
|
||||
final uri = Uri.parse(currentUrl);
|
||||
currentUrl = '${uri.scheme}://${uri.host}$location';
|
||||
} else {
|
||||
currentUrl = location;
|
||||
}
|
||||
|
||||
redirectCount++;
|
||||
}
|
||||
|
||||
return currentUrl;
|
||||
} catch (e) {
|
||||
debugPrint('getFinalRedirectUrl error: $e');
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
// 필요시 리소스 정리
|
||||
}
|
||||
}
|
||||
217
lib/data/api/naver_api_client.dart
Normal file
217
lib/data/api/naver_api_client.dart
Normal file
@@ -0,0 +1,217 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../core/network/network_client.dart';
|
||||
import '../../core/errors/network_exceptions.dart';
|
||||
import '../../domain/entities/restaurant.dart';
|
||||
import 'naver/naver_local_search_api.dart';
|
||||
import 'naver/naver_url_resolver.dart';
|
||||
import 'naver/naver_graphql_api.dart';
|
||||
import 'naver/naver_proxy_client.dart';
|
||||
import 'converters/naver_data_converter.dart';
|
||||
import '../datasources/remote/naver_html_extractor.dart';
|
||||
|
||||
/// 네이버 API 통합 클라이언트
|
||||
///
|
||||
/// 네이버 오픈 API와 지도 서비스를 위한 통합 클라이언트입니다.
|
||||
/// 내부적으로 각 기능별로 분리된 API 클라이언트를 사용합니다.
|
||||
class NaverApiClient {
|
||||
final NetworkClient _networkClient;
|
||||
|
||||
// 분리된 API 클라이언트들
|
||||
late final NaverLocalSearchApi _localSearchApi;
|
||||
late final NaverUrlResolver _urlResolver;
|
||||
late final NaverGraphQLApi _graphqlApi;
|
||||
late final NaverProxyClient _proxyClient;
|
||||
|
||||
NaverApiClient({NetworkClient? networkClient})
|
||||
: _networkClient = networkClient ?? NetworkClient() {
|
||||
// 각 API 클라이언트 초기화
|
||||
_localSearchApi = NaverLocalSearchApi(networkClient: _networkClient);
|
||||
_urlResolver = NaverUrlResolver(networkClient: _networkClient);
|
||||
_graphqlApi = NaverGraphQLApi(networkClient: _networkClient);
|
||||
_proxyClient = NaverProxyClient(networkClient: _networkClient);
|
||||
}
|
||||
|
||||
/// 네이버 로컬 검색 API 호출
|
||||
///
|
||||
/// 검색어와 좌표를 기반으로 주변 식당을 검색합니다.
|
||||
Future<List<NaverLocalSearchResult>> searchLocal({
|
||||
required String query,
|
||||
double? latitude,
|
||||
double? longitude,
|
||||
int display = 20,
|
||||
int start = 1,
|
||||
String sort = 'random',
|
||||
}) async {
|
||||
return _localSearchApi.searchLocal(
|
||||
query: query,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
display: display,
|
||||
start: start,
|
||||
sort: sort,
|
||||
);
|
||||
}
|
||||
|
||||
/// 단축 URL을 실제 URL로 변환
|
||||
Future<String> resolveShortUrl(String shortUrl) async {
|
||||
return _urlResolver.resolveShortUrl(shortUrl);
|
||||
}
|
||||
|
||||
/// 네이버 지도 페이지 HTML 가져오기
|
||||
Future<String> fetchMapPageHtml(String url) async {
|
||||
try {
|
||||
// 웹 환경에서는 프록시 사용
|
||||
if (kIsWeb) {
|
||||
return await _proxyClient.fetchViaProxy(url);
|
||||
}
|
||||
|
||||
// 모바일 환경에서는 직접 요청
|
||||
final response = await _networkClient.get<String>(
|
||||
url,
|
||||
options: Options(
|
||||
responseType: ResponseType.plain,
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
||||
'Accept-Language': 'ko-KR,ko;q=0.9,en;q=0.8',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.data == null || response.data!.isEmpty) {
|
||||
throw ParseException(
|
||||
message: 'HTML 응답이 비어있습니다',
|
||||
);
|
||||
}
|
||||
|
||||
return response.data!;
|
||||
} on DioException catch (e) {
|
||||
debugPrint('fetchMapPageHtml error: $e');
|
||||
|
||||
if (e.error is NetworkException) {
|
||||
throw e.error!;
|
||||
}
|
||||
|
||||
throw ServerException(
|
||||
message: '페이지를 불러올 수 없습니다',
|
||||
statusCode: e.response?.statusCode ?? 500,
|
||||
originalError: e,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// GraphQL API 호출
|
||||
Future<Map<String, dynamic>> fetchGraphQL({
|
||||
required String operationName,
|
||||
required String query,
|
||||
Map<String, dynamic>? variables,
|
||||
}) async {
|
||||
return _graphqlApi.fetchGraphQL(
|
||||
operationName: operationName,
|
||||
query: query,
|
||||
variables: variables,
|
||||
);
|
||||
}
|
||||
|
||||
/// pcmap URL에서 한글 텍스트 리스트 가져오기
|
||||
///
|
||||
/// restaurant/{ID}/home 형식의 URL에서 모든 한글 텍스트를 추출합니다.
|
||||
Future<Map<String, dynamic>> fetchKoreanTextsFromPcmap(String placeId) async {
|
||||
// restaurant 타입 URL 사용
|
||||
final pcmapUrl = 'https://pcmap.place.naver.com/restaurant/$placeId/home';
|
||||
|
||||
try {
|
||||
debugPrint('========== 네이버 pcmap 한글 추출 시작 ==========');
|
||||
debugPrint('요청 URL: $pcmapUrl');
|
||||
debugPrint('Place ID: $placeId');
|
||||
|
||||
String html;
|
||||
if (kIsWeb) {
|
||||
// 웹 환경에서는 프록시 사용
|
||||
html = await _proxyClient.fetchViaProxy(pcmapUrl);
|
||||
} else {
|
||||
// 모바일 환경에서는 직접 요청
|
||||
final response = await _networkClient.get<String>(
|
||||
pcmapUrl,
|
||||
options: Options(
|
||||
responseType: ResponseType.plain,
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1',
|
||||
'Accept': 'text/html,application/xhtml+xml',
|
||||
'Accept-Language': 'ko-KR,ko;q=0.9,en;q=0.8',
|
||||
'Referer': 'https://map.naver.com/',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200 || response.data == null) {
|
||||
debugPrint(
|
||||
'NaverApiClient: pcmap 페이지 로드 실패 - status: ${response.statusCode}',
|
||||
);
|
||||
return {
|
||||
'success': false,
|
||||
'error': 'HTTP ${response.statusCode}',
|
||||
'koreanTexts': <String>[],
|
||||
};
|
||||
}
|
||||
|
||||
html = response.data!;
|
||||
}
|
||||
|
||||
// 모든 한글 텍스트 추출
|
||||
final koreanTexts = NaverHtmlExtractor.extractAllValidKoreanTexts(html);
|
||||
|
||||
// JSON-LD 데이터 추출 시도
|
||||
final jsonLdName = NaverHtmlExtractor.extractPlaceNameFromJsonLd(html);
|
||||
|
||||
// Apollo State 데이터 추출 시도
|
||||
final apolloName = NaverHtmlExtractor.extractPlaceNameFromApolloState(html);
|
||||
|
||||
debugPrint('========== 추출 결과 ==========');
|
||||
debugPrint('총 한글 텍스트 수: ${koreanTexts.length}');
|
||||
debugPrint('JSON-LD 상호명: $jsonLdName');
|
||||
debugPrint('Apollo State 상호명: $apolloName');
|
||||
debugPrint('=====================================');
|
||||
|
||||
return {
|
||||
'success': true,
|
||||
'placeId': placeId,
|
||||
'url': pcmapUrl,
|
||||
'koreanTexts': koreanTexts,
|
||||
'jsonLdName': jsonLdName,
|
||||
'apolloStateName': apolloName,
|
||||
'extractedAt': DateTime.now().toIso8601String(),
|
||||
};
|
||||
} catch (e) {
|
||||
debugPrint('NaverApiClient: pcmap 페이지 파싱 실패 - $e');
|
||||
return {
|
||||
'success': false,
|
||||
'error': e.toString(),
|
||||
'koreanTexts': <String>[],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// 최종 리다이렉트 URL 가져오기
|
||||
Future<String> getFinalRedirectUrl(String url) async {
|
||||
return _urlResolver.getFinalRedirectUrl(url);
|
||||
}
|
||||
|
||||
/// 리소스 정리
|
||||
void dispose() {
|
||||
_localSearchApi.dispose();
|
||||
_urlResolver.dispose();
|
||||
_graphqlApi.dispose();
|
||||
_proxyClient.dispose();
|
||||
_networkClient.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// NaverLocalSearchResult를 Restaurant으로 변환하는 확장 메서드
|
||||
extension NaverLocalSearchResultExtension on NaverLocalSearchResult {
|
||||
Restaurant toRestaurant({required String id}) {
|
||||
return NaverDataConverter.fromLocalSearchResult(this, id: id);
|
||||
}
|
||||
}
|
||||
553
lib/data/api/naver_api_client.dart.backup
Normal file
553
lib/data/api/naver_api_client.dart.backup
Normal file
@@ -0,0 +1,553 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../core/constants/api_keys.dart';
|
||||
import '../../core/network/network_client.dart';
|
||||
import '../../core/network/network_config.dart';
|
||||
import '../../core/errors/network_exceptions.dart';
|
||||
import '../../domain/entities/restaurant.dart';
|
||||
import '../datasources/remote/naver_html_extractor.dart';
|
||||
|
||||
/// 네이버 API 클라이언트
|
||||
///
|
||||
/// 네이버 오픈 API와 지도 서비스를 위한 통합 클라이언트입니다.
|
||||
class NaverApiClient {
|
||||
final NetworkClient _networkClient;
|
||||
|
||||
NaverApiClient({NetworkClient? networkClient})
|
||||
: _networkClient = networkClient ?? NetworkClient();
|
||||
|
||||
/// 네이버 로컬 검색 API 호출
|
||||
///
|
||||
/// 검색어와 좌표를 기반으로 주변 식당을 검색합니다.
|
||||
Future<List<NaverLocalSearchResult>> searchLocal({
|
||||
required String query,
|
||||
double? latitude,
|
||||
double? longitude,
|
||||
int display = 20,
|
||||
int start = 1,
|
||||
String sort = 'random', // random, comment
|
||||
}) async {
|
||||
// API 키 확인
|
||||
if (!ApiKeys.areKeysConfigured()) {
|
||||
throw ApiKeyException();
|
||||
}
|
||||
|
||||
try {
|
||||
final response = await _networkClient.get<Map<String, dynamic>>(
|
||||
ApiKeys.naverLocalSearchEndpoint,
|
||||
queryParameters: {
|
||||
'query': query,
|
||||
'display': display,
|
||||
'start': start,
|
||||
'sort': sort,
|
||||
if (latitude != null && longitude != null) ...{
|
||||
'coordinate': '$longitude,$latitude', // 경도,위도 순서
|
||||
},
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'X-Naver-Client-Id': ApiKeys.naverClientId,
|
||||
'X-Naver-Client-Secret': ApiKeys.naverClientSecret,
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 && response.data != null) {
|
||||
final items = response.data!['items'] as List<dynamic>?;
|
||||
if (items == null || items.isEmpty) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return items
|
||||
.map(
|
||||
(item) =>
|
||||
NaverLocalSearchResult.fromJson(item as Map<String, dynamic>),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
throw ParseException(message: '검색 결과를 파싱할 수 없습니다');
|
||||
} on DioException catch (e) {
|
||||
// 에러는 NetworkClient에서 이미 변환됨
|
||||
throw e.error ??
|
||||
ServerException(message: '네이버 API 호출 실패', statusCode: 500);
|
||||
}
|
||||
}
|
||||
|
||||
/// 네이버 단축 URL 리다이렉션 처리
|
||||
///
|
||||
/// naver.me 단축 URL을 실제 지도 URL로 변환합니다.
|
||||
Future<String> resolveShortUrl(String shortUrl) async {
|
||||
if (!shortUrl.contains('naver.me')) {
|
||||
debugPrint('NaverApiClient: 단축 URL이 아님, 원본 반환 - $shortUrl');
|
||||
return shortUrl;
|
||||
}
|
||||
|
||||
try {
|
||||
debugPrint('NaverApiClient: 단축 URL 리디렉션 처리 시작 - $shortUrl');
|
||||
|
||||
// 웹 환경에서는 CORS 프록시 사용
|
||||
if (kIsWeb) {
|
||||
return await _resolveShortUrlViaProxy(shortUrl);
|
||||
}
|
||||
|
||||
// 모바일 환경에서는 여러 단계의 리다이렉션 처리
|
||||
String currentUrl = shortUrl;
|
||||
int redirectCount = 0;
|
||||
const maxRedirects = 10;
|
||||
|
||||
while (redirectCount < maxRedirects) {
|
||||
debugPrint(
|
||||
'NaverApiClient: 리다이렉션 시도 #${redirectCount + 1} - $currentUrl',
|
||||
);
|
||||
|
||||
final response = await _networkClient.get(
|
||||
currentUrl,
|
||||
options: Options(
|
||||
followRedirects: false,
|
||||
validateStatus: (status) => true, // 모든 상태 코드 허용
|
||||
headers: {'User-Agent': NetworkConfig.userAgent},
|
||||
),
|
||||
useCache: false,
|
||||
);
|
||||
|
||||
debugPrint('NaverApiClient: 응답 상태 코드 - ${response.statusCode}');
|
||||
|
||||
// 리다이렉션 체크 (301, 302, 307, 308)
|
||||
if ([301, 302, 307, 308].contains(response.statusCode)) {
|
||||
final location = response.headers['location']?.firstOrNull;
|
||||
if (location != null) {
|
||||
debugPrint('NaverApiClient: Location 헤더 발견 - $location');
|
||||
|
||||
// 상대 경로인 경우 절대 경로로 변환
|
||||
if (!location.startsWith('http')) {
|
||||
final Uri baseUri = Uri.parse(currentUrl);
|
||||
currentUrl = baseUri.resolve(location).toString();
|
||||
} else {
|
||||
currentUrl = location;
|
||||
}
|
||||
|
||||
// 목표 URL에 도달했는지 확인
|
||||
if (currentUrl.contains('pcmap.place.naver.com') ||
|
||||
currentUrl.contains('map.naver.com/p/')) {
|
||||
debugPrint('NaverApiClient: 최종 URL 도착 - $currentUrl');
|
||||
return currentUrl;
|
||||
}
|
||||
|
||||
redirectCount++;
|
||||
} else {
|
||||
debugPrint('NaverApiClient: Location 헤더 없음');
|
||||
break;
|
||||
}
|
||||
} else if (response.statusCode == 200) {
|
||||
// 200 OK인 경우 meta refresh 태그 확인
|
||||
debugPrint('NaverApiClient: 200 OK - meta refresh 태그 확인');
|
||||
|
||||
final String? html = response.data as String?;
|
||||
if (html != null &&
|
||||
html.contains('meta') &&
|
||||
html.contains('refresh')) {
|
||||
final metaRefreshRegex = RegExp(
|
||||
'<meta[^>]+http-equiv=["\']refresh["\'][^>]+content=["\']\\d+;\\s*url=([^"\'>]+)',
|
||||
caseSensitive: false,
|
||||
);
|
||||
|
||||
final match = metaRefreshRegex.firstMatch(html);
|
||||
if (match != null) {
|
||||
final redirectUrl = match.group(1)!;
|
||||
debugPrint('NaverApiClient: Meta refresh URL 발견 - $redirectUrl');
|
||||
|
||||
// 상대 경로 처리
|
||||
if (!redirectUrl.startsWith('http')) {
|
||||
final Uri baseUri = Uri.parse(currentUrl);
|
||||
currentUrl = baseUri.resolve(redirectUrl).toString();
|
||||
} else {
|
||||
currentUrl = redirectUrl;
|
||||
}
|
||||
|
||||
redirectCount++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// meta refresh가 없으면 현재 URL이 최종 URL
|
||||
debugPrint('NaverApiClient: 200 OK - 최종 URL - $currentUrl');
|
||||
return currentUrl;
|
||||
} else {
|
||||
debugPrint('NaverApiClient: 리다이렉션 아님 - 상태 코드 ${response.statusCode}');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 모든 시도 후 현재 URL 반환
|
||||
debugPrint('NaverApiClient: 최종 URL - $currentUrl');
|
||||
return currentUrl;
|
||||
} catch (e) {
|
||||
debugPrint('NaverApiClient: 단축 URL 리다이렉션 실패 - $e');
|
||||
return shortUrl;
|
||||
}
|
||||
}
|
||||
|
||||
/// 프록시를 통한 단축 URL 리다이렉션 (웹 환경)
|
||||
Future<String> _resolveShortUrlViaProxy(String shortUrl) async {
|
||||
try {
|
||||
final proxyUrl =
|
||||
'${NetworkConfig.corsProxyUrl}?url=${Uri.encodeComponent(shortUrl)}';
|
||||
|
||||
final response = await _networkClient.get<Map<String, dynamic>>(
|
||||
proxyUrl,
|
||||
options: Options(headers: {'Accept': 'application/json'}),
|
||||
useCache: false,
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 && response.data != null) {
|
||||
final data = response.data!;
|
||||
|
||||
// status.url 확인
|
||||
if (data['status'] != null &&
|
||||
data['status'] is Map &&
|
||||
data['status']['url'] != null) {
|
||||
final finalUrl = data['status']['url'] as String;
|
||||
debugPrint('NaverApiClient: 프록시 최종 URL - $finalUrl');
|
||||
return finalUrl;
|
||||
}
|
||||
|
||||
// contents에서 meta refresh 태그 찾기
|
||||
final contents = data['contents'] as String?;
|
||||
if (contents != null && contents.isNotEmpty) {
|
||||
final metaRefreshRegex = RegExp(
|
||||
'<meta\\s+http-equiv=["\']refresh["\']'
|
||||
'\\s+content=["\']0;\\s*url=([^"\']+)["\']',
|
||||
caseSensitive: false,
|
||||
);
|
||||
|
||||
final match = metaRefreshRegex.firstMatch(contents);
|
||||
if (match != null) {
|
||||
final redirectUrl = match.group(1)!;
|
||||
debugPrint('NaverApiClient: Meta refresh URL - $redirectUrl');
|
||||
return redirectUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return shortUrl;
|
||||
} catch (e) {
|
||||
debugPrint('NaverApiClient: 프록시 리다이렉션 실패 - $e');
|
||||
return shortUrl;
|
||||
}
|
||||
}
|
||||
|
||||
/// 네이버 지도 HTML 가져오기
|
||||
///
|
||||
/// 웹 환경에서는 CORS 프록시를 사용합니다.
|
||||
Future<String> fetchMapPageHtml(String url) async {
|
||||
try {
|
||||
if (kIsWeb) {
|
||||
return await _fetchViaProxy(url);
|
||||
}
|
||||
|
||||
// 모바일 환경에서는 직접 요청
|
||||
final response = await _networkClient.get<String>(
|
||||
url,
|
||||
options: Options(
|
||||
responseType: ResponseType.plain,
|
||||
headers: {
|
||||
'User-Agent': NetworkConfig.userAgent,
|
||||
'Referer': 'https://map.naver.com',
|
||||
},
|
||||
),
|
||||
useCache: false, // 네이버 지도는 동적 콘텐츠이므로 캐시 사용 안함
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 && response.data != null) {
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
throw ServerException(
|
||||
message: 'HTML을 가져올 수 없습니다',
|
||||
statusCode: response.statusCode ?? 500,
|
||||
);
|
||||
} on DioException catch (e) {
|
||||
throw e.error ??
|
||||
ServerException(message: 'HTML 가져오기 실패', statusCode: 500);
|
||||
}
|
||||
}
|
||||
|
||||
/// 프록시를 통한 HTML 가져오기 (웹 환경)
|
||||
Future<String> _fetchViaProxy(String url) async {
|
||||
final proxyUrl =
|
||||
'${NetworkConfig.corsProxyUrl}?url=${Uri.encodeComponent(url)}';
|
||||
|
||||
final response = await _networkClient.get<Map<String, dynamic>>(
|
||||
proxyUrl,
|
||||
options: Options(headers: {'Accept': 'application/json'}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 && response.data != null) {
|
||||
final data = response.data!;
|
||||
|
||||
// 상태 코드 확인
|
||||
if (data['status'] != null && data['status'] is Map) {
|
||||
final statusMap = data['status'] as Map<String, dynamic>;
|
||||
final httpCode = statusMap['http_code'];
|
||||
if (httpCode != null && httpCode != 200) {
|
||||
throw ServerException(
|
||||
message: '네이버 서버 응답 오류',
|
||||
statusCode: httpCode as int,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// contents 반환
|
||||
final contents = data['contents'];
|
||||
if (contents == null || contents.toString().isEmpty) {
|
||||
throw ParseException(message: '빈 응답을 받았습니다');
|
||||
}
|
||||
|
||||
return contents.toString();
|
||||
}
|
||||
|
||||
throw ServerException(
|
||||
message: '프록시 요청 실패',
|
||||
statusCode: response.statusCode ?? 500,
|
||||
);
|
||||
}
|
||||
|
||||
/// GraphQL 쿼리 실행
|
||||
///
|
||||
/// 네이버 지도 API의 GraphQL 엔드포인트에 요청을 보냅니다.
|
||||
Future<Map<String, dynamic>> fetchGraphQL({
|
||||
required String operationName,
|
||||
required Map<String, dynamic> variables,
|
||||
required String query,
|
||||
}) async {
|
||||
const String graphqlUrl = 'https://pcmap-api.place.naver.com/graphql';
|
||||
|
||||
try {
|
||||
final response = await _networkClient.post<Map<String, dynamic>>(
|
||||
graphqlUrl,
|
||||
data: {
|
||||
'operationName': operationName,
|
||||
'variables': variables,
|
||||
'query': query,
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Referer': 'https://map.naver.com/',
|
||||
'User-Agent': NetworkConfig.userAgent,
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 && response.data != null) {
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
throw ParseException(message: 'GraphQL 응답을 파싱할 수 없습니다');
|
||||
} on DioException catch (e) {
|
||||
throw e.error ??
|
||||
ServerException(message: 'GraphQL 요청 실패', statusCode: 500);
|
||||
}
|
||||
}
|
||||
|
||||
/// pcmap URL에서 한글 텍스트 리스트 가져오기
|
||||
///
|
||||
/// restaurant/{ID}/home 형식의 URL에서 모든 한글 텍스트를 추출합니다.
|
||||
Future<Map<String, dynamic>> fetchKoreanTextsFromPcmap(String placeId) async {
|
||||
// restaurant 타입 URL 사용
|
||||
final pcmapUrl = 'https://pcmap.place.naver.com/restaurant/$placeId/home';
|
||||
|
||||
try {
|
||||
debugPrint('========== 네이버 pcmap 한글 추출 시작 ==========');
|
||||
debugPrint('요청 URL: $pcmapUrl');
|
||||
debugPrint('Place ID: $placeId');
|
||||
|
||||
String html;
|
||||
if (kIsWeb) {
|
||||
// 웹 환경에서는 프록시 사용
|
||||
html = await _fetchViaProxy(pcmapUrl);
|
||||
} else {
|
||||
// 모바일 환경에서는 직접 요청
|
||||
final response = await _networkClient.get<String>(
|
||||
pcmapUrl,
|
||||
options: Options(
|
||||
responseType: ResponseType.plain,
|
||||
headers: {
|
||||
'User-Agent': NetworkConfig.userAgent,
|
||||
'Accept': 'text/html,application/xhtml+xml',
|
||||
'Accept-Language': 'ko-KR,ko;q=0.9,en;q=0.8',
|
||||
'Referer': 'https://map.naver.com/',
|
||||
},
|
||||
),
|
||||
useCache: false,
|
||||
);
|
||||
|
||||
if (response.statusCode != 200 || response.data == null) {
|
||||
debugPrint(
|
||||
'NaverApiClient: pcmap 페이지 로드 실패 - status: ${response.statusCode}',
|
||||
);
|
||||
return {
|
||||
'success': false,
|
||||
'error': 'HTTP ${response.statusCode}',
|
||||
'koreanTexts': <String>[],
|
||||
};
|
||||
}
|
||||
|
||||
html = response.data!;
|
||||
}
|
||||
|
||||
// 모든 한글 텍스트 추출
|
||||
final koreanTexts = NaverHtmlExtractor.extractAllValidKoreanTexts(html);
|
||||
|
||||
// JSON-LD 데이터 추출 시도
|
||||
final jsonLdName = NaverHtmlExtractor.extractPlaceNameFromJsonLd(html);
|
||||
|
||||
// Apollo State 데이터 추출 시도
|
||||
final apolloName = NaverHtmlExtractor.extractPlaceNameFromApolloState(html);
|
||||
|
||||
debugPrint('========== 추출 결과 ==========');
|
||||
debugPrint('총 한글 텍스트 수: ${koreanTexts.length}');
|
||||
debugPrint('JSON-LD 상호명: $jsonLdName');
|
||||
debugPrint('Apollo State 상호명: $apolloName');
|
||||
debugPrint('=====================================');
|
||||
|
||||
return {
|
||||
'success': true,
|
||||
'placeId': placeId,
|
||||
'url': pcmapUrl,
|
||||
'koreanTexts': koreanTexts,
|
||||
'jsonLdName': jsonLdName,
|
||||
'apolloStateName': apolloName,
|
||||
'extractedAt': DateTime.now().toIso8601String(),
|
||||
};
|
||||
} catch (e) {
|
||||
debugPrint('NaverApiClient: pcmap 페이지 파싱 실패 - $e');
|
||||
return {
|
||||
'success': false,
|
||||
'error': e.toString(),
|
||||
'koreanTexts': <String>[],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 최종 리디렉션 URL 획득
|
||||
///
|
||||
/// 주어진 URL이 리디렉션되는 최종 URL을 반환합니다.
|
||||
Future<String> getFinalRedirectUrl(String url) async {
|
||||
try {
|
||||
debugPrint('NaverApiClient: 최종 리디렉션 URL 획득 중 - $url');
|
||||
|
||||
// 429 에러 방지를 위한 지연
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
|
||||
final response = await _networkClient.get(
|
||||
url,
|
||||
options: Options(
|
||||
followRedirects: true,
|
||||
maxRedirects: 5,
|
||||
responseType: ResponseType.plain,
|
||||
),
|
||||
useCache: false,
|
||||
);
|
||||
|
||||
final finalUrl = response.realUri.toString();
|
||||
debugPrint('NaverApiClient: 최종 리디렉션 URL - $finalUrl');
|
||||
|
||||
return finalUrl;
|
||||
} catch (e) {
|
||||
debugPrint('NaverApiClient: 최종 리디렉션 URL 획득 실패 - $e');
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// 리소스 정리
|
||||
void dispose() {
|
||||
_networkClient.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// 네이버 로컬 검색 결과
|
||||
class NaverLocalSearchResult {
|
||||
final String title;
|
||||
final String link;
|
||||
final String category;
|
||||
final String description;
|
||||
final String telephone;
|
||||
final String address;
|
||||
final String roadAddress;
|
||||
final int mapx; // 경도 (x좌표)
|
||||
final int mapy; // 위도 (y좌표)
|
||||
|
||||
NaverLocalSearchResult({
|
||||
required this.title,
|
||||
required this.link,
|
||||
required this.category,
|
||||
required this.description,
|
||||
required this.telephone,
|
||||
required this.address,
|
||||
required this.roadAddress,
|
||||
required this.mapx,
|
||||
required this.mapy,
|
||||
});
|
||||
|
||||
factory NaverLocalSearchResult.fromJson(Map<String, dynamic> json) {
|
||||
return NaverLocalSearchResult(
|
||||
title: _removeHtmlTags(json['title'] ?? ''),
|
||||
link: json['link'] ?? '',
|
||||
category: json['category'] ?? '',
|
||||
description: _removeHtmlTags(json['description'] ?? ''),
|
||||
telephone: json['telephone'] ?? '',
|
||||
address: json['address'] ?? '',
|
||||
roadAddress: json['roadAddress'] ?? '',
|
||||
mapx: int.tryParse(json['mapx']?.toString() ?? '0') ?? 0,
|
||||
mapy: int.tryParse(json['mapy']?.toString() ?? '0') ?? 0,
|
||||
);
|
||||
}
|
||||
|
||||
/// HTML 태그 제거
|
||||
static String _removeHtmlTags(String text) {
|
||||
return text.replaceAll(RegExp(r'<[^>]+>'), '');
|
||||
}
|
||||
|
||||
/// 위도 (십진도)
|
||||
double get latitude => mapy / 10000000.0;
|
||||
|
||||
/// 경도 (십진도)
|
||||
double get longitude => mapx / 10000000.0;
|
||||
|
||||
/// Restaurant 엔티티로 변환
|
||||
Restaurant toRestaurant({required String id}) {
|
||||
// 카테고리 파싱
|
||||
final categories = category.split('>').map((c) => c.trim()).toList();
|
||||
final mainCategory = categories.isNotEmpty ? categories.first : '기타';
|
||||
final subCategory = categories.length > 1 ? categories.last : mainCategory;
|
||||
|
||||
return Restaurant(
|
||||
id: id,
|
||||
name: title,
|
||||
category: mainCategory,
|
||||
subCategory: subCategory,
|
||||
description: description.isNotEmpty ? description : null,
|
||||
phoneNumber: telephone.isNotEmpty ? telephone : null,
|
||||
roadAddress: roadAddress.isNotEmpty ? roadAddress : address,
|
||||
jibunAddress: address,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
lastVisitDate: null,
|
||||
source: DataSource.NAVER,
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
naverPlaceId: null,
|
||||
naverUrl: link.isNotEmpty ? link : null,
|
||||
businessHours: null,
|
||||
lastVisited: null,
|
||||
visitCount: 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user