From 46dd6c0332f52fff04fe0c5bea0159b14ef55c15 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 15 Mar 2022 13:54:08 +0100 Subject: [PATCH] added formatting and fetching by keywords --- telegram_bot/news_fetcher.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/telegram_bot/news_fetcher.py b/telegram_bot/news_fetcher.py index 31357bd..77626fc 100644 --- a/telegram_bot/news_fetcher.py +++ b/telegram_bot/news_fetcher.py @@ -9,6 +9,7 @@ __license__ = "None" import sys from newsapi import NewsApiClient from pandas.io.json import json_normalize +import json import pandas as pd # Init @@ -23,6 +24,20 @@ all_articles = newsapi.get_everything(q='bitcoin', sources='bbc-news,the-verge', # /v2/top-headlines/sources sources = newsapi.get_sources() +def get_top_news_by_keyword(keyword): + top_headlines = newsapi.get_top_headlines(q=keyword, sources='bbc-news,the-verge,cnn', language='en') + out_file = open("top_headline.json", "w") + json.dump(top_headlines, out_file) + return top_headlines + +def format_article(article): + sourcename = article["source"]["name"] + headline = article["title"] + url = article["url"] + formatted_article = f"{sourcename}\n{headline}\n\ntext" + + return formatted_article + if __name__ == '__main__': - print(top_headlines) - print(all_articles) \ No newline at end of file + articles = get_top_news_by_keyword("business") + formatted_article = format_article(articles["articles"][0]) \ No newline at end of file