From f7cd06b0eebbe91d35782e0389f9d650d548eb8d Mon Sep 17 00:00:00 2001 From: happycastle <41810556+happycastle114@users.noreply.github.com> Date: Mon, 20 Nov 2023 20:50:39 +0900 Subject: [PATCH 1/5] fix(api) mongodb --- API/MongoDB.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ API/candidate.py | 1 + API/utils.py | 17 ++++++++++------- API/votecode.py | 1 - 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 API/MongoDB.py diff --git a/API/MongoDB.py b/API/MongoDB.py new file mode 100644 index 0000000..38faf68 --- /dev/null +++ b/API/MongoDB.py @@ -0,0 +1,47 @@ +from dataclasses import dataclass + + +@dataclass +class Councilor: + sdName: str + sggName: str + wiwName: str + name: 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"), + gender=data.get("gender"), + birthday=data.get("birthday"), + age=data.get("age"), + jobId=data.get("jobId"), + job=data.get("job"), + eduId=data.get("eduId"), + edu=data.get("edu"), + ) + + def to_dict(self): + return { + "sdName": self.sdName, + "sggName": self.sggName, + "wiwName": self.wiwName, + "name": self.name, + "gender": self.gender, + "birthday": self.birthday, + "age": self.age, + "jobId": self.jobId, + "job": self.job, + "eduId": self.eduId, + "edu": self.edu, + } \ No newline at end of file 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..2243920 100644 --- a/API/utils.py +++ b/API/utils.py @@ -36,21 +36,24 @@ def save_to_mongo(data: List[dict], sgTypecode: str) -> None: main_collection = db["local_councilor"] local_metro_map = getLocalMetroMap() - + print(len(data)) # 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, + "localId": district_id["local_id"], + "metroId": 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(","): From 18a7453bbcf520c7a579c38c9211cc1c146ad494 Mon Sep 17 00:00:00 2001 From: happycastle114 Date: Mon, 20 Nov 2023 11:51:05 +0000 Subject: [PATCH 2/5] Formatted with black --- API/MongoDB.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/API/MongoDB.py b/API/MongoDB.py index 38faf68..62e9b67 100644 --- a/API/MongoDB.py +++ b/API/MongoDB.py @@ -30,7 +30,7 @@ def from_dict(cls, data: dict): eduId=data.get("eduId"), edu=data.get("edu"), ) - + def to_dict(self): return { "sdName": self.sdName, @@ -44,4 +44,4 @@ def to_dict(self): "job": self.job, "eduId": self.eduId, "edu": self.edu, - } \ No newline at end of file + } From bd817e9a15086d313d9615b2515a81e0dbd73850 Mon Sep 17 00:00:00 2001 From: happycastle <41810556+happycastle114@users.noreply.github.com> Date: Mon, 20 Nov 2023 21:05:33 +0900 Subject: [PATCH 3/5] fix --- API/MongoDB.py | 6 +++--- API/utils.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/API/MongoDB.py b/API/MongoDB.py index 38faf68..a879be0 100644 --- a/API/MongoDB.py +++ b/API/MongoDB.py @@ -24,10 +24,10 @@ def from_dict(cls, data: dict): name=data.get("name"), gender=data.get("gender"), birthday=data.get("birthday"), - age=data.get("age"), - jobId=data.get("jobId"), + age=int(data.get("age")), + jobId=int(data.get("jobId")), job=data.get("job"), - eduId=data.get("eduId"), + eduId=int(data.get("eduId")), edu=data.get("edu"), ) diff --git a/API/utils.py b/API/utils.py index 2243920..8314102 100644 --- a/API/utils.py +++ b/API/utils.py @@ -50,8 +50,8 @@ def save_to_mongo(data: List[dict], sgTypecode: str) -> None: main_collection.update_one( { "name": entry["name"], - "localId": district_id["local_id"], - "metroId": district_id["metro_id"], + "local_id": district_id["local_id"], + "metro_id": district_id["metro_id"], }, {"$set": Councilor.from_dict(entry).to_dict()}, upsert=True, From d0bbe9e05c22fb8ed2bac2a9b8f46ac77c331c5c Mon Sep 17 00:00:00 2001 From: Heewon Lee <94441510+pingpingy1@users.noreply.github.com> Date: Mon, 20 Nov 2023 23:33:41 +0900 Subject: [PATCH 4/5] =?UTF-8?q?Councilor=20class=EC=97=90=20jdName=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API/MongoDB.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/API/MongoDB.py b/API/MongoDB.py index c429773..261334b 100644 --- a/API/MongoDB.py +++ b/API/MongoDB.py @@ -7,6 +7,7 @@ class Councilor: sggName: str wiwName: str name: str + jdName: str gender: str birthday: str age: int @@ -22,6 +23,7 @@ def from_dict(cls, data: dict): 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")), @@ -37,6 +39,7 @@ def to_dict(self): "sggName": self.sggName, "wiwName": self.wiwName, "name": self.name, + "jdName": self.jdName, "gender": self.gender, "birthday": self.birthday, "age": self.age, From 1f697601ebff0e47a5b25a52ec00f77176cbf42e Mon Sep 17 00:00:00 2001 From: keonly Date: Tue, 21 Nov 2023 12:48:01 +0900 Subject: [PATCH 5/5] fix(API): remove debugging print --- API/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API/utils.py b/API/utils.py index 8314102..5391a8a 100644 --- a/API/utils.py +++ b/API/utils.py @@ -36,7 +36,7 @@ def save_to_mongo(data: List[dict], sgTypecode: str) -> None: main_collection = db["local_councilor"] local_metro_map = getLocalMetroMap() - print(len(data)) + # TODO: Support other types of councils if sgTypecode == "6": for entry in data: