From fa6647936516620bb499068e54e863f8d35377b0 Mon Sep 17 00:00:00 2001 From: Joel Tikoo Date: Thu, 7 Aug 2025 14:13:24 +0530 Subject: [PATCH 1/4] Create news_oversimplifier.py Python command-line tool that fetches recent news articles based on a search query using NewsAPI and summarizes the article content using extractive summarization. You can also save the summaries to a text file. --- news_oversimplifier.py | 154 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 news_oversimplifier.py diff --git a/news_oversimplifier.py b/news_oversimplifier.py new file mode 100644 index 00000000000..1caad60255c --- /dev/null +++ b/news_oversimplifier.py @@ -0,0 +1,154 @@ +# news_oversimplifier.py +# Python command-line tool that fetches recent news articles based on a search query using NewsAPI and summarizes the article content using extractive summarization. You can also save the summaries to a text file. + +# (requires API key in .env file) + +import requests +import os +import sys +from dotenv import load_dotenv +from summa.summarizer import summarize + + +def main(): + + # loads .env variables + load_dotenv() + API_KEY = os.getenv("NEWS_API_KEY") + + # check validity of command-line arguments + try: + if len(sys.argv) == 2: + news_query = sys.argv[1] + else: + raise IndexError() + except IndexError: + sys.exit('Please provide correct number of command-line arguments') + + try: + # get number of articles from user + while True: + try: + num_articles = int(input('Enter number of articles: ')) + break + except ValueError: + continue + + # fetch news articles based on user's query + articles = fetch_news(API_KEY, query=news_query, max_articles=num_articles) + + # output printing title, summary and no. of words in the summary + for i, article in enumerate(articles): + capitalized_title = capitalize_title(article["title"]) + print(f"\n{i+1}. {capitalized_title}") + + content = article.get("content") or article.get("description") or "" + if not content.strip(): + print("No content to oversimplify.") + continue + + summary = summarize_text(content) # returns summary + count = word_count(summary) # returns word count + print(f"\nOVERSIMPLIFIED:\n{summary}\n{count} words\n") + + # ask user whether they want to save the output in a txt file + while True: + saving_status = input( + "Would you like to save this in a text file? (y/n): ").strip().lower() + if saving_status == "y": + save_summary(article["title"], summary) + break + elif saving_status == "n": + break + else: + print('Try again\n') + continue + + except Exception as e: + print("ERROR:", e) + + +def word_count(text): # pytest in test file + """ + Returns the number of words in the given text. + + args: + text (str): Input string to count words from. + + returns: + int: Number of words in the string. + """ + return len(text.split()) + + +def summarize_text(text, ratio=0.6): # pytest in test file + """ + Summarizes the given text using the summa library. + + args: + text (str): The input text to summarize. + ratio (float): Ratio of the original text to retain in the summary. + + returns: + str: The summarized text, or a fallback message if intro is present or summary is empty. + """ + summary = summarize(text, ratio=ratio) + if summary.lower().startswith("hello, and welcome to decoder!"): + return "No description available for this headline" + else: + return summary.strip() if summary else text + + +def capitalize_title(title): # pytest in test file + """ + Capitalizes all letters in a given article title. + + args: + title (str): The title to format. + + returns: + str: Title in uppercase with surrounding spaces removed. + """ + return title.upper().strip() + + +def fetch_news(api_key, query, max_articles=5): # no pytest + """ + Fetches a list of news articles from NewsAPI based on a query string. + + args: + api_key (str): NewsAPI key loaded from environment. + query (str): The keyword to search for in news articles. + max_articles (int): Maximum number of articles to fetch. + + returns: + list: List of dictionaries, each representing a news article. + + raises: + Exception: If the API response status is not 'ok'. + """ + url = ( + f"https://newsapi.org/v2/everything?q={query}&language=en&apiKey={api_key}&pageSize={max_articles}" + ) + response = requests.get(url) + data = response.json() + if data.get("status") != "ok": + raise Exception("Failed to fetch news:", data.get("message")) + return data["articles"] + + +def save_summary(title, summary, path="summaries.txt"): # no pytest + """ + Appends a formatted summary to a file along with its title. + + args: + title (str): Title of the article. + summary (str): Summarized text to save. + path (str): File path to save the summary, i.e. 'summaries.txt' + """ + with open(path, "a", encoding="utf-8") as f: + f.write(f"{title}\n{summary}\n{'='*60}\n") + + +if __name__ == "__main__": + main() \ No newline at end of file From 02da74d78195b61f2d7c236efd36af6f9e5c27ca Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 7 Aug 2025 18:17:53 +0000 Subject: [PATCH 2/4] Bump pymongo from 4.13.2 to 4.14.0 Bumps [pymongo](https://github.com/mongodb/mongo-python-driver) from 4.13.2 to 4.14.0. - [Release notes](https://github.com/mongodb/mongo-python-driver/releases) - [Changelog](https://github.com/mongodb/mongo-python-driver/blob/master/doc/changelog.rst) - [Commits](https://github.com/mongodb/mongo-python-driver/compare/4.13.2...4.14.0) --- updated-dependencies: - dependency-name: pymongo dependency-version: 4.14.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- requirements_with_versions.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_with_versions.txt b/requirements_with_versions.txt index d7b15014929..1c0357ccec4 100644 --- a/requirements_with_versions.txt +++ b/requirements_with_versions.txt @@ -42,7 +42,7 @@ obs==0.0.0 todo==0.1 oauth2client==4.1.3 keras==3.10.0 -pymongo==4.13.2 +pymongo==4.14.0 playsound==1.3.0 pyttsx3==2.99 auto-mix-prep==0.2.0 From 7028470ce49528e131b95694d76c482cc27a55be Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 7 Aug 2025 18:18:26 +0000 Subject: [PATCH 3/4] Bump ccxt from 4.4.98 to 4.4.99 Bumps [ccxt](https://github.com/ccxt/ccxt) from 4.4.98 to 4.4.99. - [Release notes](https://github.com/ccxt/ccxt/releases) - [Changelog](https://github.com/ccxt/ccxt/blob/master/CHANGELOG.md) - [Commits](https://github.com/ccxt/ccxt/compare/v4.4.98...v4.4.99) --- updated-dependencies: - dependency-name: ccxt dependency-version: 4.4.99 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- requirements_with_versions.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_with_versions.txt b/requirements_with_versions.txt index d7b15014929..35a9dc7f2cf 100644 --- a/requirements_with_versions.txt +++ b/requirements_with_versions.txt @@ -81,7 +81,7 @@ Unidecode==1.4.0 Ball==0.2.9 pynput==1.8.1 gTTS==2.5.4 -ccxt==4.4.98 +ccxt==4.4.99 fitz==0.0.1.dev2 fastapi==0.116.1 Django==5.1.7 From 4169f8b212b4593de87c6915e04f61105bcc3de8 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 8 Aug 2025 18:43:21 +0000 Subject: [PATCH 4/4] Bump openai from 1.99.1 to 1.99.5 Bumps [openai](https://github.com/openai/openai-python) from 1.99.1 to 1.99.5. - [Release notes](https://github.com/openai/openai-python/releases) - [Changelog](https://github.com/openai/openai-python/blob/main/CHANGELOG.md) - [Commits](https://github.com/openai/openai-python/compare/v1.99.1...v1.99.5) --- updated-dependencies: - dependency-name: openai dependency-version: 1.99.5 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- requirements_with_versions.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_with_versions.txt b/requirements_with_versions.txt index d7b15014929..d33c70e1dce 100644 --- a/requirements_with_versions.txt +++ b/requirements_with_versions.txt @@ -49,7 +49,7 @@ auto-mix-prep==0.2.0 lib==4.0.0 pywifi==1.1.12 patterns==0.3 -openai==1.99.1 +openai==1.99.5 background==0.2.1 pydantic==2.11.7 openpyxl==3.1.2