raghavNCI commited on
Commit
0b7d949
·
1 Parent(s): 41b21fc

changes in cache_init

Browse files
Files changed (1) hide show
  1. cache_init.py +23 -20
cache_init.py CHANGED
@@ -2,7 +2,7 @@
2
  import os
3
  import redis
4
  import requests
5
- import uuid
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
- "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)
@@ -34,20 +36,21 @@ def fetch_and_cache_articles():
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}")
 
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}")