diff --git a/API/MongoDB.py b/API/MongoDB.py new file mode 100644 index 0000000..261334b --- /dev/null +++ b/API/MongoDB.py @@ -0,0 +1,50 @@ +from dataclasses import dataclass + + +@dataclass +class Councilor: + sdName: str + sggName: str + wiwName: str + name: str + jdName: str + gender: str + birthday: str + age: int + jobId: int + job: str + eduId: int + edu: str + + @classmethod + def from_dict(cls, data: dict): + return cls( + sdName=data.get("sdName"), + sggName=data.get("sggName"), + wiwName=data.get("wiwName"), + name=data.get("name"), + jdName=data.get("jdName"), + gender=data.get("gender"), + birthday=data.get("birthday"), + age=int(data.get("age")), + jobId=int(data.get("jobId")), + job=data.get("job"), + eduId=int(data.get("eduId")), + edu=data.get("edu"), + ) + + def to_dict(self): + return { + "sdName": self.sdName, + "sggName": self.sggName, + "wiwName": self.wiwName, + "name": self.name, + "jdName": self.jdName, + "gender": self.gender, + "birthday": self.birthday, + "age": self.age, + "jobId": self.jobId, + "job": self.job, + "eduId": self.eduId, + "edu": self.edu, + } diff --git a/API/candidate.py b/API/candidate.py index 7ddda3b..b61b2c8 100644 --- a/API/candidate.py +++ b/API/candidate.py @@ -3,6 +3,7 @@ import os, requests, sys import xml.etree.ElementTree as ET from typing import List +import argparse from configurations.secrets import OpenDataPortalSecrets from .utils import save_to_excel, getLocalMetroMap diff --git a/API/utils.py b/API/utils.py index e6d214b..5391a8a 100644 --- a/API/utils.py +++ b/API/utils.py @@ -40,17 +40,20 @@ def save_to_mongo(data: List[dict], sgTypecode: str) -> None: # TODO: Support other types of councils if sgTypecode == "6": for entry in data: - district_id = local_metro_map[(entry["sdName"], entry["wiwName"])][ - "local_id" - ] - + if not (entry["sdName"], entry["wiwName"]) in local_metro_map: + print( + f"Warning: '{entry['sdName']} {entry['wiwName']}'에 해당하는 지역 ID가 존재하지 않습니다." + ) + continue + district_id = local_metro_map[(entry["sdName"], entry["wiwName"])] if district_id: main_collection.update_one( { "name": entry["name"], - "localId": district_id, + "local_id": district_id["local_id"], + "metro_id": district_id["metro_id"], }, - {"$push": Councilor.from_dict(entry)}, + {"$set": Councilor.from_dict(entry).to_dict()}, upsert=True, ) else: diff --git a/API/votecode.py b/API/votecode.py index 374e761..236c18d 100644 --- a/API/votecode.py +++ b/API/votecode.py @@ -27,7 +27,6 @@ xml_data = response.content.decode("utf-8") # Parse the XML data root = ET.fromstring(xml_data) - # Find all elements where sgTypecode is equal to INPUT and extract their sgId values sgIds = set() for code in input("Input the number of sgTypecode: ").split(","):