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