feat: MySQL DB 연동 및 크롤링 로직 정상화 (ID 매칭 및 데이터 정밀화)

This commit is contained in:
2026-03-10 14:11:49 +09:00
parent 9369e18eb8
commit 743cce543b
21 changed files with 696 additions and 204 deletions

View File

@@ -42,53 +42,52 @@ app.add_middleware(
# --- HTML 라우팅 ---
import csv
import pymysql
def get_db_connection():
return pymysql.connect(
host='localhost',
user='root',
password='45278434',
database='crawling',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
@app.get("/project-data")
async def get_project_data():
"""
sheet.csv 파일을 읽어서 프로젝트 현황 데이터를 반환
MySQL overseas_projects 테이블에서 프로젝트 현황 데이터를 반환
"""
projects = []
try:
if not os.path.exists("sheet.csv"):
return []
with open("sheet.csv", mode="r", encoding="utf-8-sig") as f:
reader = csv.reader(f)
rows = list(reader)
# "No." 헤더를 찾아 데이터 시작점 결정
start_idx = None
for i, row in enumerate(rows):
if any("No." in cell for cell in row):
start_idx = i + 1
break
if start_idx is not None:
for row in rows[start_idx:]:
if len(row) >= 8:
# [프로젝트명, 담당부서, 담당자, 최근활동로그, 파일수]
# 복구된 sheet.csv 형식에 맞춰 인덱스 추출 (1, 2, 3, 5, 7)
try:
# 파일 수 숫자로 변환 (공백 제거 후 처리)
raw_count = row[7].strip()
file_count = int(raw_count) if raw_count.isdigit() else 0
except (ValueError, IndexError):
file_count = 0
projects.append([
row[1], # 프로젝트 명
row[2], # 담당부서
row[3], # 담당자
row[5], # 최근 활동로그
file_count # 파일 수
])
conn = get_db_connection()
try:
with conn.cursor() as cursor:
# 대시보드에 필요한 모든 정보를 쿼리 (short_nm 포함)
cursor.execute("SELECT project_nm, short_nm, department, master, recent_log, file_count, continent, country FROM overseas_projects ORDER BY id ASC")
rows = cursor.fetchall()
# 프론트엔드 기대 형식에 맞춰 반환
# [표시될 프로젝트명(short_nm), 담당부서, 담당자, 최근활동로그, 파일수, 대륙, 국가]
projects = []
for row in rows:
# short_nm이 있으면 그것을 쓰고, 없으면 project_nm 사용
display_name = row['short_nm'] if row['short_nm'] and row['short_nm'].strip() else row['project_nm']
projects.append([
display_name,
row['department'],
row['master'],
row['recent_log'],
row['file_count'],
row['continent'],
row['country']
])
return projects
finally:
conn.close()
except Exception as e:
print(f"Error reading sheet.csv: {e}")
print(f"Error fetching from DB: {e}")
return {"error": str(e)}
return projects
@app.get("/")
async def root(request: Request):