Skip to content

Commit

Permalink
Merge pull request #50 from Kritika75/enhancing-features
Browse files Browse the repository at this point in the history
Added new features to the "news" file
  • Loading branch information
suryanshsk authored Oct 6, 2024
2 parents 038e637 + 7e8bcc6 commit db1290b
Showing 1 changed file with 70 additions and 7 deletions.
77 changes: 70 additions & 7 deletions news_info.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,72 @@
import requests
import json
import os

def get_news():
# Example API call, replace with a real news API
api_key = 'YOUR_API_KEY'
response = requests.get(f'https://newsapi.org/v2/top-headlines?apiKey={api_key}')
data = response.json()
for article in data['articles'][:5]:
print(article['title'])
def get_news(api_key, category='general', page=1, page_size=5, query=None):
try:
base_url = 'https://newsapi.org/v2/top-headlines' if not query else 'https://newsapi.org/v2/everything'
params = {
'apiKey': api_key,
'category': category,
'page': page,
'pagesize': page_size
}

if query:
params.pop('category')
params['q'] = query

response = requests.get(base_url, params=params)
response.raise_for_status()

data = response.json()
articles = data.get('articles', [])

return articles

except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return []
except json.JSONDecodeError as e:
print(f"Failed to parse JSON: {e}")
return []

def save_articles(articles, filename='articles.json'):
with open(filename, 'w') as f:
json.dump(articles, f, indent=4)
print(f"Articles saved to {filename}.")

def main():
API_KEY = os.environ.get('NEWS_API_KEY')
if not API_KEY:
print("Please set the NEWS_API_KEY environment variable.")
return

category = input("Enter news category (such as: general, business, technology, sports, entertainment, politics, health and science): ")
while True:
try:
num_articles = int(input("How many articles would you like to see? "))
if num_articles <= 0:
print("Please enter a positive integer.")
else:
break
except ValueError:
print("Invalid input. Please enter a positive integer.")

query = input("Enter a keyword to search for articles (or press Enter to skip): ")
if query == "":
query = None

articles = get_news(API_KEY, category=category, page_size=num_articles, query=query)

if articles:
save_articles(articles)
for article in articles:
print(f"Title: {article['title']}")
print(f"Author: {article.get('author', 'N/A')}")
print(f"Published at: {article['publishedAt']}")
print(f"Description: {article.get('description', 'N/A')}\n")

if __name__ == "__main__":
main()

0 comments on commit db1290b

Please sign in to comment.