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 + }