The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Make a terminal-based newsreader

Creating a terminal-based newsreader can be done in Python using libraries like requests for fetching news from an API and curses or simple print statements for terminal display. Here’s a simple but functional example using the free NewsAPI (https://newsapi.org/) to fetch headlines and display them in the terminal.


python
import requests API_KEY = "YOUR_NEWSAPI_KEY" # You need to get a free API key from https://newsapi.org/ NEWS_API_URL = "https://newsapi.org/v2/top-headlines" COUNTRY = "us" # Change to your preferred country code def fetch_news(): params = { 'apiKey': API_KEY, 'country': COUNTRY, 'pageSize': 10 # Number of news articles to fetch } response = requests.get(NEWS_API_URL, params=params) if response.status_code == 200: data = response.json() return data.get('articles', []) else: print(f"Failed to fetch news: {response.status_code}") return [] def display_news(articles): print("nTop News Headlines:n") for i, article in enumerate(articles, 1): print(f"{i}. {article['title']}") print("nEnter the number of the article to read more or 'q' to quit:") def read_article(articles, index): article = articles[index] print("n" + "="*80) print(f"Title: {article['title']}") print(f"Author: {article.get('author', 'Unknown')}") print(f"Source: {article['source']['name']}") print(f"Published At: {article['publishedAt']}") print("n" + article.get('description', 'No description available.') + "n") print(f"Read more: {article['url']}") print("="*80 + "n") input("Press Enter to return to the list...") def main(): articles = fetch_news() if not articles: print("No articles to display.") return while True: display_news(articles) choice = input("> ").strip() if choice.lower() == 'q': print("Goodbye!") break if choice.isdigit(): idx = int(choice) - 1 if 0 <= idx < len(articles): read_article(articles, idx) else: print("Invalid selection. Try again.") else: print("Invalid input. Enter a number or 'q' to quit.") if __name__ == "__main__": main()

How it works:

  • Fetches top 10 headlines from NewsAPI for a specified country.

  • Lists headlines in the terminal.

  • User inputs a number to read the article summary and more details.

  • User can quit anytime with q.

Before running:

  • Register for a free API key at https://newsapi.org/

  • Replace "YOUR_NEWSAPI_KEY" with your actual API key.

  • Run with Python 3.

Would you like me to extend this with features like category selection, pagination, or offline caching?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About