TelegramAktienBot/telegram_bot/news_fetcher.py
2022-03-15 13:54:08 +01:00

43 lines
1.4 KiB
Python

"""
script for news fetching (by keywords)
"""
__author__ = "Florian Kellermann, Linus Eickhoff"
__date__ = "15.03.2022"
__version__ = "0.0.1"
__license__ = "None"
import sys
from newsapi import NewsApiClient
from pandas.io.json import json_normalize
import json
import pandas as pd
# Init
newsapi = NewsApiClient(api_key='4261069558d64489a104ca40df8d2edc')
# /v2/top-headlines
top_headlines = newsapi.get_top_headlines(q='bitcoin', sources='bbc-news,the-verge', language='en')
# /v2/everything
all_articles = newsapi.get_everything(q='bitcoin', sources='bbc-news,the-verge', domains='bbc.co.uk,techcrunch.com', from_param='2022-03-14', to='2022-03-15', language='en', sort_by='relevancy', page=2)
# /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"<i>{sourcename}</i>\n{headline}\n\n<a href=\"{url}\">text</a>"
return formatted_article
if __name__ == '__main__':
articles = get_top_news_by_keyword("business")
formatted_article = format_article(articles["articles"][0])