raghavNCI
commited on
Commit
·
cad0e2e
1
Parent(s):
3be496d
env file and news route
Browse files
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GNEWS_API_KEY=a060d96b8ffb68188df49fe49a80e28f
|
routes.py
CHANGED
@@ -1,11 +1,37 @@
|
|
1 |
-
|
|
|
|
|
2 |
|
3 |
router = APIRouter()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@router.get("/")
|
6 |
async def root():
|
7 |
return {"message": "LucidFeed backend is up and running!"}
|
8 |
-
|
9 |
-
@router.get("/ping")
|
10 |
-
async def ping():
|
11 |
-
return {"message": "pong"}
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from fastapi import APIRouter, Query
|
4 |
|
5 |
router = APIRouter()
|
6 |
|
7 |
+
GNEWS_API_KEY = os.getenv("GNEWS_API_KEY")
|
8 |
+
|
9 |
+
@router.get("/news/category/{category}")
|
10 |
+
def get_news_by_category(category: str, limit: int = 5):
|
11 |
+
base_url = "https://gnews.io/api/v4/top-headlines"
|
12 |
+
params = {
|
13 |
+
"topic": category,
|
14 |
+
"lang": "en",
|
15 |
+
"max": limit,
|
16 |
+
"token": GNEWS_API_KEY
|
17 |
+
}
|
18 |
+
response = requests.get(base_url, params=params)
|
19 |
+
data = response.json()
|
20 |
+
|
21 |
+
articles = []
|
22 |
+
for article in data.get("articles", []):
|
23 |
+
articles.append({
|
24 |
+
"title": article["title"],
|
25 |
+
"url": article["url"],
|
26 |
+
"description": article.get("description"),
|
27 |
+
"publishedAt": article["publishedAt"],
|
28 |
+
"image": article.get("image"),
|
29 |
+
"source": article["source"]["name"]
|
30 |
+
})
|
31 |
+
|
32 |
+
return articles
|
33 |
+
|
34 |
+
|
35 |
@router.get("/")
|
36 |
async def root():
|
37 |
return {"message": "LucidFeed backend is up and running!"}
|
|
|
|
|
|
|
|