Skip to content
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

[KAN-87] naver geo api를 활용하여 좌표 데이터 추가 #20

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion csv-to-es.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"image_url": {"type": "text"},
"category": {"type": "text", "analyzer": "korean"},
"discount_content": {"type": "text", "analyzer": "korean"},
"longitude": {"type": "float"},
"latitude": {"type": "float"},
"menus": {
"type": "nested",
"properties": {
Expand Down Expand Up @@ -86,8 +88,18 @@
else:
discount_content = None

if pd.notna(row['longitude']):
longitude = float(row['longitude'])
else:
longitude = None

if pd.notna(row['latitude']):
latitude = float(row['latitude'])
else:
latitude = None

print(row['name'], row['category'], row['review_count'], row['address'], rating, number, restaurant_image_url,
menus)
menus, longitude, latitude)
data = {
"id": row['id'],
"name": row['name'],
Expand All @@ -99,6 +111,8 @@
"image_url": restaurant_image_url,
"category": row['custom_category'],
"discount_content": discount_content,
"longitude": longitude,
"latitude": latitude,
"menus": menus,
}
if data.get("discount_content") is None:
Expand Down
41 changes: 41 additions & 0 deletions csv-to-geoapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os

import pandas as pd
import requests

# 네이버 지오코딩 API 키 설정
client_id = os.getenv('NAVER_CLIENT_ID')
client_secret = os.getenv('NAVER_CLIENT_SECRET')

df = pd.read_csv('restaurants.csv')


def get_coordinates(address):
url = f"https://naveropenapi.apigw.ntruss.com/map-geocode/v2/geocode?query={address}"
headers = {
"X-NCP-APIGW-API-KEY-ID": client_id,
"X-NCP-APIGW-API-KEY": client_secret
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
if data['addresses']:
x = data['addresses'][0]['x']
y = data['addresses'][0]['y']
return x, y
else:
print("Error Code:", response.status_code)
print(response.text)
return None, None


df['longitude'] = ''
df['latitude'] = ''

for idx, row in df.iterrows():
address = row['address']
x, y = get_coordinates(address)
df.at[idx, 'longitude'] = x
df.at[idx, 'latitude'] = y

df.to_csv('restaurants.csv', index=False)
10 changes: 0 additions & 10 deletions mysql_batch/create_table.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
from create_table_list import *
from delete_table_list import *

def create_table(cursor):

# table deletion query
cursor.execute(delete_table_restaurants)
cursor.execute(delete_table_categories)
cursor.execute(delete_table_restaurant_categories)
cursor.execute(delete_table_operating_infos)
cursor.execute(delete_table_menus)

# table creation query
cursor.execute(create_table_restaurants)
cursor.execute(create_table_restaurant_likes)
cursor.execute(create_table_categories)
cursor.execute(create_table_restaurant_categories)
cursor.execute(create_table_operating_infos)
cursor.execute(create_table_menus)

64 changes: 0 additions & 64 deletions mysql_batch/create_table_list.py

This file was deleted.

10 changes: 7 additions & 3 deletions mysql_batch/insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def insert_into_restaurants(cursor, restaurant):
`rating_avg`,
`representative_image_url`,
`view_count`,
`discount_content`
`discount_content`,
`longitude`,
`latitude`
)
VALUES (
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
);
"""

Expand All @@ -29,7 +31,9 @@ def insert_into_restaurants(cursor, restaurant):
0,
restaurant['image_url'],
0,
restaurant['discount_content']
restaurant['discount_content'],
restaurant['longitude'],
restaurant['latitude']
))


Expand Down
Loading
Loading