raghavNCI commited on
Commit
2aed3fd
·
1 Parent(s): f8db5c2

redis setup and startup functionality

Browse files
Files changed (4) hide show
  1. .env +2 -1
  2. app.py +5 -0
  3. cache_init.py +59 -0
  4. requirements.txt +1 -0
.env CHANGED
@@ -1 +1,2 @@
1
- GNEWS_API_KEY=6c61f5da1b24fa83fbf964f8b280c438
 
 
1
+ GNEWS_API_KEY=6c61f5da1b24fa83fbf964f8b280c438
2
+ UPSTASH_REDIS_URL=rediss://:AU6-AAIjcDEyZDY4Njk3OGUwNzg0NTczODUxYmRmMDUyZDlmZWNiZXAxMA@clever-turtle-20158.upstash.io:6379
app.py CHANGED
@@ -1,12 +1,17 @@
1
  from fastapi import FastAPI
2
  from routes import router # routes.py must be in same folder
3
  from dotenv import load_dotenv
 
4
 
5
  load_dotenv()
6
 
7
  app = FastAPI()
8
  app.include_router(router)
9
 
 
 
 
 
10
  @app.get("/health")
11
  def health_check():
12
  return {"status": "ok"}
 
1
  from fastapi import FastAPI
2
  from routes import router # routes.py must be in same folder
3
  from dotenv import load_dotenv
4
+ from cache_init import fetch_and_cache_articles
5
 
6
  load_dotenv()
7
 
8
  app = FastAPI()
9
  app.include_router(router)
10
 
11
+ @app.on_event("startup")
12
+ async def startup_event():
13
+ fetch_and_cache_articles()
14
+
15
  @app.get("/health")
16
  def health_check():
17
  return {"status": "ok"}
cache_init.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # cache_init.py
2
+ import os
3
+ import redis
4
+ import requests
5
+ import uuid
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv()
9
+
10
+ CATEGORIES = [
11
+ "technology", "business", "science", "health", "world", "entertainment"
12
+ ]
13
+
14
+ GNEWS_API_KEY = os.getenv("GNEWS_API_KEY")
15
+ REDIS_URL = os.getenv("UPSTASH_REDIS_URL")
16
+
17
+ r = redis.Redis.from_url(REDIS_URL, decode_responses=True)
18
+
19
+
20
+ def fetch_and_cache_articles():
21
+ print("[INIT] Fetching and caching articles...")
22
+ for category in CATEGORIES:
23
+ base_url = "https://gnews.io/api/v4/top-headlines"
24
+ params = {
25
+ "topic": category,
26
+ "lang": "en",
27
+ "max": 5,
28
+ "expand": "content",
29
+ "token": GNEWS_API_KEY
30
+ }
31
+ try:
32
+ response = requests.get(base_url, params=params, timeout=10)
33
+ response.raise_for_status()
34
+ articles = response.json().get("articles", [])
35
+
36
+ for article in articles:
37
+ article_id = str(uuid.uuid4())
38
+ article_data = {
39
+ "id": article_id,
40
+ "title": article["title"],
41
+ "url": article["url"],
42
+ "description": article.get("description"),
43
+ "content": article.get("content"),
44
+ "image": article.get("image"),
45
+ "publishedAt": article["publishedAt"],
46
+ "category": category,
47
+ "source": article["source"]["name"]
48
+ }
49
+ r.hset(f"article:{article_id}", mapping=article_data)
50
+ r.sadd(f"category:{category}", article_id)
51
+
52
+ except Exception as e:
53
+ print(f"[ERROR] Failed for category {category}: {e}")
54
+
55
+ print("[INIT] Article caching complete.")
56
+
57
+
58
+ if __name__ == "__main__":
59
+ fetch_and_cache_articles()
requirements.txt CHANGED
@@ -2,3 +2,4 @@ fastapi
2
  uvicorn[standard]
3
  requests
4
  python-dotenv
 
 
2
  uvicorn[standard]
3
  requests
4
  python-dotenv
5
+ redis