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

feat(add) key #57

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion API/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -84,6 +84,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)
Expand Down
6 changes: 3 additions & 3 deletions API/elected.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
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"
Expand Down Expand Up @@ -82,7 +82,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":
Expand Down
34 changes: 33 additions & 1 deletion API/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -58,3 +58,35 @@ 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
}