스크래핑 로직 점검 및 추가
This commit is contained in:
1 parent
d930679e90
commit
0d3543d84d
18 files changed
+361
-43
No files matched your search
@@ -0,0 +1,142 @@
|
||||
import json
|
||||
import os
|
||||
import aiomysql
|
||||
from common.utils import get_env
|
||||
|
||||
_pool: aiomysql.Pool | None = None
|
||||
|
||||
|
||||
async def get_pool() -> aiomysql.Pool:
|
||||
global _pool
|
||||
if _pool is None:
|
||||
_pool = await aiomysql.create_pool(
|
||||
host=get_env("MYSQL_HOST"),
|
||||
port=int(os.getenv("MYSQL_PORT", "3306")),
|
||||
user=get_env("MYSQL_USER"),
|
||||
password=get_env("MYSQL_PASSWORD"),
|
||||
db=get_env("MYSQL_DB"),
|
||||
autocommit=True,
|
||||
charset="utf8mb4",
|
||||
)
|
||||
return _pool
|
||||
|
||||
|
||||
async def execute(sql: str, args: tuple = ()) -> int:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(sql, args)
|
||||
return cur.lastrowid
|
||||
|
||||
|
||||
async def fetchone(sql: str, args: tuple = ()) -> dict | None:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||
await cur.execute(sql, args)
|
||||
return await cur.fetchone()
|
||||
|
||||
async def insert_instagram_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO instagram_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_facebook_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO facebook_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_naver_blog_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO naver_blog_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_youtube_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO youtube_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_gangnam_unni_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO gangnam_unni_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_analysis_run(
|
||||
analysis_run_id: str,
|
||||
hospital_id: str,
|
||||
owner_user_id: int,
|
||||
instagram_data_id: int | None,
|
||||
facebook_data_id: int | None,
|
||||
naver_blog_data_id: int | None,
|
||||
youtube_data_id: int | None,
|
||||
gangnam_unni_data_id: int | None,
|
||||
) -> str:
|
||||
await execute(
|
||||
"INSERT INTO analysis_runs"
|
||||
" (analysis_run_id, hospital_id, owner_user_id, instagram_data_id, facebook_data_id, naver_blog_data_id, youtube_data_id, gangnam_unni_data_id)"
|
||||
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
|
||||
(analysis_run_id, hospital_id, owner_user_id, instagram_data_id, facebook_data_id, naver_blog_data_id, youtube_data_id, gangnam_unni_data_id),
|
||||
)
|
||||
return analysis_run_id
|
||||
|
||||
|
||||
|
||||
async def is_done(table: str, row_id: int | None) -> bool:
|
||||
if row_id is None:
|
||||
return True
|
||||
r = await fetchone(f"SELECT status FROM {table} WHERE id = %s", (row_id,))
|
||||
return r["status"] == "done"
|
||||
|
||||
|
||||
async def set_instagram_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE instagram_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def set_facebook_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE facebook_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def set_naver_blog_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE naver_blog_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def set_youtube_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE youtube_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def set_gangnam_unni_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE gangnam_unni_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def save_instagram_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE instagram_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def save_facebook_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE facebook_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def save_naver_blog_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE naver_blog_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def save_youtube_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE youtube_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def save_gangnam_unni_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE gangnam_unni_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def insert_hospital(
|
||||
hospital_id: str,
|
||||
name: str,
|
||||
name_en: str | None = None,
|
||||
road_address: str | None = None,
|
||||
site_address: str | None = None,
|
||||
owner_user_id: int = 0,
|
||||
brn: str = "",
|
||||
) -> dict:
|
||||
await execute(
|
||||
"INSERT INTO hospital_baseinfo (hospital_id, hospital_name, hospital_name_en, road_address, site_address, owner_user_id, brn) VALUES (%s, %s, %s, %s, %s, %s, %s)",
|
||||
(hospital_id, name, name_en, road_address, site_address, owner_user_id, brn),
|
||||
)
|
||||
return await fetchone(
|
||||
"SELECT created_at FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
Reference in new issue
Block a user