Skip to content

Commit

Permalink
[scrap] 신규의원 후보중에 찾기. 아직 후보목록이 mongodb에 없어서 테스트는 하지 않음
Browse files Browse the repository at this point in the history
  • Loading branch information
Re-st committed Nov 23, 2023
1 parent 151b3d5 commit 29a4038
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion API/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ def fetch_all_data(
if args.save_method == "excel":
save_to_excel(data_list, args.sgTypecode, is_elected=False)
elif args.save_method == "mongo":
save_to_mongo(data_list, args.sgTypecode)
save_to_mongo(data_list, args.sgTypecode, "local_councilor_cand")
2 changes: 1 addition & 1 deletion API/elected.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ def fetch_all_data(
if args.save_method == "excel":
save_to_excel(data_list, args.sgTypecode, is_elected=True)
elif args.save_method == "mongo":
save_to_mongo(data_list, args.sgTypecode)
save_to_mongo(data_list, args.sgTypecode, "local_councilor")
5 changes: 2 additions & 3 deletions API/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ def get_district_id(sd_name: str, wiw_name: str) -> Optional[int]:
return LOCAL_METRO_ID_MAP[(sd_name, wiw_name)]


def save_to_mongo(data: List[dict], sgTypecode: str) -> None:
def save_to_mongo(data: List[dict], sgTypecode: str, where: str) -> None:
db = client["council"]
main_collection = db["local_councilor"]
# main_collection = db["test"]
main_collection = db[where]

# TODO: Support other types of councils
if sgTypecode in ["6", "9"]:
Expand Down
30 changes: 30 additions & 0 deletions scrap/utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def save_to_database(record: ScrapResult) -> bool:
# TODO: DB에 없던 새로운 의원 핸들링
update_councilors(collection, cid, other_councilors)
remove_councilors(collection, resigned_councilors)
add_councilors(collection, cid, new_councilors, str(record.council_type))

return True

Expand Down Expand Up @@ -96,3 +97,32 @@ def remove_councilors(collection, councilors: List[Councilor]) -> None:
collection.delete_one(
{"localId": councilor["localId"], "name": councilor["name"]}
)

def add_councilors(collection, cid: int, councilors: List[Councilor], council_type: str) -> None:
"""
신규 의원을 검사합니다. 기존 비례대표의 사퇴로 새 비례대표가 바로 추가되었을
가능성을 보고, 아닐 시에는 사용자에게 보고합니다.
:param collection: MongoDB 컬렉션
:param councilors: 업데이트할 의원 리스트
:param council_type: local_councilor 등등, 의원의 종류
"""
db = client[str(MongoDBSecrets.database_name)]
candcoll = db[council_type + "_cand"] # 예: local_councilor_cand
for councilor in councilors:
# find the councilor in candidate
query = {"localId": cid, "name": councilor.name}
all_documents = list(candcoll.find(query))
assert(len(all_documents) <= 1, "신규의원을 같은 의회의 후보중에 찾는데, 동명이인이 있네요.")
if len(all_documents) == 0:
# no candidate found, report to user
print(f"신규의원 {councilor.name}을 찾을 수 없습니다.")
continue
found_document = all_documents[0]
age = found_document.get("age")
# birthday = found_document.get("birthday")
councilor.age = age
collection.update_one(
{"localId": cid, "name": councilor.name},
{"$set": asdict(councilor)},
upsert=False,
)

0 comments on commit 29a4038

Please sign in to comment.