added formatting and fetching by keywords

This commit is contained in:
Linus E 2022-03-15 13:54:08 +01:00
parent 50a4df4e71
commit 46dd6c0332

View File

@ -9,6 +9,7 @@ __license__ = "None"
import sys import sys
from newsapi import NewsApiClient from newsapi import NewsApiClient
from pandas.io.json import json_normalize from pandas.io.json import json_normalize
import json
import pandas as pd import pandas as pd
# Init # Init
@ -23,6 +24,20 @@ all_articles = newsapi.get_everything(q='bitcoin', sources='bbc-news,the-verge',
# /v2/top-headlines/sources # /v2/top-headlines/sources
sources = newsapi.get_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__': if __name__ == '__main__':
print(top_headlines) articles = get_top_news_by_keyword("business")
print(all_articles) formatted_article = format_article(articles["articles"][0])