KJ24 commited on
Commit
1c0805d
·
verified ·
1 Parent(s): 3ba12bd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -0
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ from newspaper import Article
4
+
5
+ app = FastAPI()
6
+
7
+ class URLRequest(BaseModel):
8
+ url: str
9
+
10
+ @app.post("/extract")
11
+ def extract_article(request: URLRequest):
12
+ try:
13
+ article = Article(request.url)
14
+ article.download()
15
+ article.parse()
16
+
17
+ return {
18
+ "title": article.title,
19
+ "content": article.text,
20
+ "author": article.authors,
21
+ "date": str(article.publish_date) if article.publish_date else None
22
+ }
23
+ except Exception as e:
24
+ raise HTTPException(status_code=500, detail=str(e))