All Toolsโ€บNews Summarizer CLI
๐Ÿ”ง AI-Powered News AggregationApril 28, 2026โœ… Tests passing

News Summarizer CLI

A command-line tool that aggregates headlines and articles from multiple news APIs and summarizes them using an AI model like OpenAI's GPT or Hugging Face models. Developers can easily fetch, filter, and summarize news by topic or region for faster consumption.

What It Does

  • Fetch news articles by topic and region using NewsAPI.
  • Summarize articles using OpenAI's GPT model.
  • Customizable summarization length.
  • Save summarized news to a text file.

Installation

  • Python 3.7+
  • requests==2.31.0
  • openai==0.27.8

Usage

Run the tool with the following command:

python news_summarizer_cli.py --news_api_key YOUR_NEWSAPI_KEY \
                              --openai_api_key YOUR_OPENAI_KEY \
                              --topic "technology" \
                              --region "us" \
                              --summary_length 3 \
                              --output_file summaries.txt

Example

python news_summarizer_cli.py --news_api_key YOUR_NEWSAPI_KEY \
                              --openai_api_key YOUR_OPENAI_KEY \
                              --topic "sports" \
                              --region "gb" \
                              --summary_length 2

Source Code

import argparse
import requests
import openai
import os

def fetch_news(api_key, topic, region):
    """Fetch news articles from NewsAPI."""
    url = "https://newsapi.org/v2/top-headlines"
    params = {
        "q": topic,
        "country": region,
        "apiKey": api_key
    }
    try:
        response = requests.get(url, params=params)
        response.raise_for_status()
        return response.json().get("articles", [])
    except requests.exceptions.RequestException as e:
        print(f"Error fetching news: {e}")
        return []

def summarize_articles(articles, summary_length, openai_api_key):
    """Summarize a list of articles using OpenAI's GPT model."""
    openai.api_key = openai_api_key
    summaries = []
    for article in articles:
        content = article.get("title", "") + " " + article.get("description", "")
        try:
            response = openai.Completion.create(
                engine="text-davinci-003",
                prompt=f"Summarize the following text in {summary_length} sentences:\n{content}",
                max_tokens=150
            )
            summaries.append(response.choices[0].text.strip())
        except openai.error.OpenAIError as e:
            print(f"Error summarizing article: {e}")
            summaries.append("Error summarizing this article.")
    return summaries

def save_to_file(summaries, output_file):
    """Save summaries to a file."""
    try:
        with open(output_file, "w") as f:
            for summary in summaries:
                f.write(summary + "\n\n")
        print(f"Summaries saved to {output_file}")
    except IOError as e:
        print(f"Error saving to file: {e}")

def main():
    parser = argparse.ArgumentParser(description="News Summarizer CLI")
    parser.add_argument("--news_api_key", required=True, help="API key for NewsAPI")
    parser.add_argument("--openai_api_key", required=True, help="API key for OpenAI")
    parser.add_argument("--topic", required=True, help="Topic to search for")
    parser.add_argument("--region", required=True, help="Region code (e.g., 'us', 'gb')")
    parser.add_argument("--summary_length", type=int, default=3, help="Number of sentences in the summary")
    parser.add_argument("--output_file", help="File to save summaries")

    args = parser.parse_args()

    articles = fetch_news(args.news_api_key, args.topic, args.region)
    if not articles:
        print("No articles found.")
        return

    summaries = summarize_articles(articles, args.summary_length, args.openai_api_key)

    for i, summary in enumerate(summaries, 1):
        print(f"Article {i} Summary:\n{summary}\n")

    if args.output_file:
        save_to_file(summaries, args.output_file)

if __name__ == "__main__":
    main()

Community

Downloads

ยทยทยท

Rate this tool

No ratings yet โ€” be the first!

Details

Tool Name
news_summarizer_cli
Category
AI-Powered News Aggregation
Generated
April 28, 2026
Tests
Passing โœ…

Quick Install

Clone just this tool:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/ptulin/autoaiforge.git
cd autoaiforge
git sparse-checkout set generated_tools/2026-04-28/news_summarizer_cli
cd generated_tools/2026-04-28/news_summarizer_cli
pip install -r requirements.txt 2>/dev/null || true
python news_summarizer_cli.py