From 0864158b5cc297dadc5e7bd94e199acd2f753b40 Mon Sep 17 00:00:00 2001 From: happycastle <41810556+happycastle114@users.noreply.github.com> Date: Fri, 17 Nov 2023 14:51:27 +0900 Subject: [PATCH 1/5] feat(add) key --- API/candidate.py | 4 +++- API/elected.py | 9 ++++----- API/utils.py | 20 +++++++++++++++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/API/candidate.py b/API/candidate.py index 7ec6743..a983e06 100644 --- a/API/candidate.py +++ b/API/candidate.py @@ -7,7 +7,7 @@ import argparse from configurations.secrets import OpenDataPortalSecrets -from .utils import save_to_excel +from .utils import save_to_excel, getLocalMetroMap BASE_URL = "http://apis.data.go.kr/9760000/PofelcddInfoInqireService/getPofelcddRegistSttusInfoInqire" @@ -84,6 +84,8 @@ def fetch_all_data( drop_columns = args.drop_columns.split(",") if args.drop_columns else [] data_list = fetch_all_data(sgIds, args.sgTypecode, drop_columns=drop_columns) + print(getLocalMetroMap()) + if args.save_method == "excel": save_to_excel(data_list, args.sgTypecode, is_elected=True) diff --git a/API/elected.py b/API/elected.py index 7fbeb8b..5586f07 100644 --- a/API/elected.py +++ b/API/elected.py @@ -3,14 +3,13 @@ import argparse import requests import xml.etree.ElementTree as ET -from typing import List +from typing import List, Dict from configurations.secrets import OpenDataPortalSecrets -from .utils import save_to_excel, save_to_mongo +from .utils import save_to_excel, save_to_mongo, getLocalMetroMap -BASE_URL = "http://apis.data.go.kr/9760000/WinnerInfoInqireService2/getWinnerInfoInqire" - +BASE_URL = "http://apis.data.go.kr/9760000/WinnerInfoInqireService2/getWinnerInfoInqire" def fetch_data( sgId: str, @@ -82,7 +81,7 @@ def fetch_all_data( drop_columns = args.drop_columns.split(",") if args.drop_columns else [] data_list = fetch_all_data(sgIds, args.sgTypecode, drop_columns=drop_columns) - + print(getLocalMetroMap()) if args.save_method == "excel": save_to_excel(data_list, args.sgTypecode, is_elected=True) elif args.save_method == "mongo": diff --git a/API/utils.py b/API/utils.py index 0e8f33e..808e77f 100644 --- a/API/utils.py +++ b/API/utils.py @@ -1,7 +1,7 @@ import os import requests import pandas as pd -from typing import List, Optional +from typing import List, Optional, Dict from . import BASE_DIR, SG_TYPECODE, SG_TYPECODE_TYPE from configurations.secrets import MongoDBSecrets @@ -58,3 +58,21 @@ def save_to_mongo(data: List[dict], sgTypecode: str) -> None: raise NotImplementedError("현재 구시군의회의원(6)만 구현되어 있습니다.") print(f"데이터를 성공적으로 MongoDB '{main_collection.name}' 컬렉션에 저장하였습니다.") + +def getLocalMetroMap() -> Dict[str, str]: + db = client["district"] + result = db["local_district"].aggregate([{ + "$lookup": { + "from": "metro_district", + "localField": "sdName", + "foreignField": "name_ko", + "as": "productInfo" + } + }, { "$unwind": "$productInfo" }, { "$project": { + "cid": 1, + "metro_id": "$productInfo.metro_id", + "sdName": 1, + "wiwName": 1, + } }]) + return {(item["sdName"], item["wiwName"]): {"local_id": item["cid"], "metro_id": item["metro_id"]} for item in result} + From 9451b0fb31784a924237b508e5e7d79fcc457767 Mon Sep 17 00:00:00 2001 From: happycastle114 Date: Fri, 17 Nov 2023 05:51:56 +0000 Subject: [PATCH 2/5] Formatted with black --- API/candidate.py | 1 - API/elected.py | 3 ++- API/utils.py | 42 ++++++++++++++++++++++++++++-------------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/API/candidate.py b/API/candidate.py index a983e06..1e91554 100644 --- a/API/candidate.py +++ b/API/candidate.py @@ -86,7 +86,6 @@ def fetch_all_data( data_list = fetch_all_data(sgIds, args.sgTypecode, drop_columns=drop_columns) print(getLocalMetroMap()) - if args.save_method == "excel": save_to_excel(data_list, args.sgTypecode, is_elected=True) elif args.save_method == "mongo": diff --git a/API/elected.py b/API/elected.py index 5586f07..4be46c6 100644 --- a/API/elected.py +++ b/API/elected.py @@ -9,7 +9,8 @@ from .utils import save_to_excel, save_to_mongo, getLocalMetroMap -BASE_URL = "http://apis.data.go.kr/9760000/WinnerInfoInqireService2/getWinnerInfoInqire" +BASE_URL = "http://apis.data.go.kr/9760000/WinnerInfoInqireService2/getWinnerInfoInqire" + def fetch_data( sgId: str, diff --git a/API/utils.py b/API/utils.py index 808e77f..c7994b5 100644 --- a/API/utils.py +++ b/API/utils.py @@ -59,20 +59,34 @@ def save_to_mongo(data: List[dict], sgTypecode: str) -> None: print(f"데이터를 성공적으로 MongoDB '{main_collection.name}' 컬렉션에 저장하였습니다.") + def getLocalMetroMap() -> Dict[str, str]: db = client["district"] - result = db["local_district"].aggregate([{ - "$lookup": { - "from": "metro_district", - "localField": "sdName", - "foreignField": "name_ko", - "as": "productInfo" + result = db["local_district"].aggregate( + [ + { + "$lookup": { + "from": "metro_district", + "localField": "sdName", + "foreignField": "name_ko", + "as": "productInfo", + } + }, + {"$unwind": "$productInfo"}, + { + "$project": { + "cid": 1, + "metro_id": "$productInfo.metro_id", + "sdName": 1, + "wiwName": 1, + } + }, + ] + ) + return { + (item["sdName"], item["wiwName"]): { + "local_id": item["cid"], + "metro_id": item["metro_id"], } - }, { "$unwind": "$productInfo" }, { "$project": { - "cid": 1, - "metro_id": "$productInfo.metro_id", - "sdName": 1, - "wiwName": 1, - } }]) - return {(item["sdName"], item["wiwName"]): {"local_id": item["cid"], "metro_id": item["metro_id"]} for item in result} - + for item in result + } From d0206523bb40381329cb19b22fa74005e8266dc8 Mon Sep 17 00:00:00 2001 From: happycastle <41810556+happycastle114@users.noreply.github.com> Date: Fri, 17 Nov 2023 14:52:30 +0900 Subject: [PATCH 3/5] Remove Print --- API/candidate.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/API/candidate.py b/API/candidate.py index a983e06..7e612fb 100644 --- a/API/candidate.py +++ b/API/candidate.py @@ -84,8 +84,6 @@ def fetch_all_data( drop_columns = args.drop_columns.split(",") if args.drop_columns else [] data_list = fetch_all_data(sgIds, args.sgTypecode, drop_columns=drop_columns) - print(getLocalMetroMap()) - if args.save_method == "excel": save_to_excel(data_list, args.sgTypecode, is_elected=True) From 1ba0304efbd966d06f7197246c56277c00a31c06 Mon Sep 17 00:00:00 2001 From: happycastle <41810556+happycastle114@users.noreply.github.com> Date: Fri, 17 Nov 2023 14:54:34 +0900 Subject: [PATCH 4/5] remove print --- API/elected.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/API/elected.py b/API/elected.py index 4be46c6..3e1574e 100644 --- a/API/elected.py +++ b/API/elected.py @@ -9,8 +9,7 @@ from .utils import save_to_excel, save_to_mongo, getLocalMetroMap -BASE_URL = "http://apis.data.go.kr/9760000/WinnerInfoInqireService2/getWinnerInfoInqire" - +BASE_URL = "http://apis.data.go.kr/9760000/WinnerInfoInqireService2/getWinnerInfoInqire" def fetch_data( sgId: str, @@ -82,7 +81,6 @@ def fetch_all_data( drop_columns = args.drop_columns.split(",") if args.drop_columns else [] data_list = fetch_all_data(sgIds, args.sgTypecode, drop_columns=drop_columns) - print(getLocalMetroMap()) if args.save_method == "excel": save_to_excel(data_list, args.sgTypecode, is_elected=True) elif args.save_method == "mongo": From ad323a7a8fa156b7861e74fc8da9766b8a42c7bf Mon Sep 17 00:00:00 2001 From: happycastle114 Date: Fri, 17 Nov 2023 05:55:00 +0000 Subject: [PATCH 5/5] Formatted with black --- API/elected.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/API/elected.py b/API/elected.py index 3e1574e..dd91346 100644 --- a/API/elected.py +++ b/API/elected.py @@ -9,7 +9,8 @@ from .utils import save_to_excel, save_to_mongo, getLocalMetroMap -BASE_URL = "http://apis.data.go.kr/9760000/WinnerInfoInqireService2/getWinnerInfoInqire" +BASE_URL = "http://apis.data.go.kr/9760000/WinnerInfoInqireService2/getWinnerInfoInqire" + def fetch_data( sgId: str,