raghavNCI
commited on
Commit
·
0b7d949
1
Parent(s):
41b21fc
changes in cache_init
Browse files- cache_init.py +23 -20
cache_init.py
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
import os
|
3 |
import redis
|
4 |
import requests
|
5 |
-
import
|
6 |
from dotenv import load_dotenv
|
7 |
|
8 |
load_dotenv()
|
@@ -16,17 +16,19 @@ 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 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
}
|
31 |
try:
|
32 |
response = requests.get(base_url, params=params, timeout=10)
|
@@ -34,20 +36,21 @@ def fetch_and_cache_articles():
|
|
34 |
articles = response.json().get("articles", [])
|
35 |
|
36 |
for article in articles:
|
37 |
-
article_id =
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
|
52 |
except Exception as e:
|
53 |
print(f"[ERROR] Failed for category {category}: {e}")
|
|
|
2 |
import os
|
3 |
import redis
|
4 |
import requests
|
5 |
+
import hashlib
|
6 |
from dotenv import load_dotenv
|
7 |
|
8 |
load_dotenv()
|
|
|
16 |
|
17 |
r = redis.Redis.from_url(REDIS_URL, decode_responses=True)
|
18 |
|
19 |
+
def generate_id(url: str) -> str:
|
20 |
+
return hashlib.sha1(url.encode()).hexdigest()
|
21 |
|
22 |
def fetch_and_cache_articles():
|
23 |
print("[INIT] Fetching and caching articles...")
|
24 |
for category in CATEGORIES:
|
25 |
base_url = "https://gnews.io/api/v4/top-headlines"
|
26 |
params = {
|
27 |
+
"topic": category,
|
28 |
+
"lang": "en",
|
29 |
+
"max": 5,
|
30 |
+
"expand": "content",
|
31 |
+
"token": GNEWS_API_KEY
|
32 |
}
|
33 |
try:
|
34 |
response = requests.get(base_url, params=params, timeout=10)
|
|
|
36 |
articles = response.json().get("articles", [])
|
37 |
|
38 |
for article in articles:
|
39 |
+
article_id = generate_id(article["url"])
|
40 |
+
if not r.exists(f"article:{article_id}"):
|
41 |
+
article_data = {
|
42 |
+
"id": article_id,
|
43 |
+
"title": article["title"],
|
44 |
+
"url": article["url"],
|
45 |
+
"description": article.get("description"),
|
46 |
+
"content": article.get("content"),
|
47 |
+
"image": article.get("image"),
|
48 |
+
"publishedAt": article["publishedAt"],
|
49 |
+
"category": category,
|
50 |
+
"source": article["source"]["name"]
|
51 |
+
}
|
52 |
+
r.hset(f"article:{article_id}", mapping=article_data)
|
53 |
+
r.sadd(f"category:{category}", article_id)
|
54 |
|
55 |
except Exception as e:
|
56 |
print(f"[ERROR] Failed for category {category}: {e}")
|