-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
42 lines (35 loc) · 1.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from crawler import sel_option, craw
import os
from dotenv import load_dotenv
load_dotenv()
app = FastAPI(title="Crawler Service")
app.add_middleware(
CORSMiddleware,
allow_origins=[f"http://{os.getenv('PUBLIC_IP')}:{os.getenv('BACKEND_PORT')}"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class URLRequest(BaseModel):
url: str
uuid: str
@app.post("/crawl")
async def crawl_url(request: URLRequest):
print(f"크롤링 시작 URL: {request.url}, UUID: {request.uuid}")
try:
print("Selenium 드라이버 초기화 시작")
driver = sel_option()
print("드라이버 초기화 완료")
print("크롤링 시작")
result = craw(request.url, request.uuid, driver)
print("크롤링 완료")
driver.quit()
return {"status": "success", "data": result}
except Exception as e:
print(f"크롤링 중 오류 발생: {str(e)}")
if driver:
driver.quit()
raise HTTPException(status_code=500, detail=f"크롤링 오류: {str(e)}")