Spaces:
Sleeping
Sleeping
Create app.py
Browse files
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))
|