saherPervaiz commited on
Commit
6f71a1e
Β·
verified Β·
1 Parent(s): 203f0b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -5,8 +5,10 @@ import requests
5
  API_KEY = "fe1e6bcbbf384b3e9220a7a1138805e0" # πŸ”‘ Add your NewsAPI.org API key here
6
 
7
  # Function to fetch news from News API
8
- def fetch_news(topic):
9
- url = f"https://newsapi.org/v2/everything?q={topic}&apiKey={API_KEY}"
 
 
10
  response = requests.get(url)
11
  if response.status_code == 200:
12
  return response.json().get("articles", [])
@@ -35,11 +37,8 @@ def main():
35
  else:
36
  articles = []
37
  for topic in selected_topics:
38
- articles.extend(fetch_news(topic))
39
-
40
- # Apply keyword filter if provided
41
- if keyword_filter:
42
- articles = [a for a in articles if keyword_filter.lower() in a.get("title", "").lower()]
43
 
44
  if articles:
45
  for article in articles:
@@ -56,3 +55,4 @@ def main():
56
 
57
  if __name__ == "__main__":
58
  main()
 
 
5
  API_KEY = "fe1e6bcbbf384b3e9220a7a1138805e0" # πŸ”‘ Add your NewsAPI.org API key here
6
 
7
  # Function to fetch news from News API
8
+ def fetch_news(topic, keyword):
9
+ # Combine topic and keyword for a refined search
10
+ query = f"{topic} {keyword}" if keyword else topic
11
+ url = f"https://newsapi.org/v2/everything?q={query}&apiKey={API_KEY}"
12
  response = requests.get(url)
13
  if response.status_code == 200:
14
  return response.json().get("articles", [])
 
37
  else:
38
  articles = []
39
  for topic in selected_topics:
40
+ # Fetch news for each topic combined with the keyword
41
+ articles.extend(fetch_news(topic, keyword_filter))
 
 
 
42
 
43
  if articles:
44
  for article in articles:
 
55
 
56
  if __name__ == "__main__":
57
  main()
58
+