added comments

This commit is contained in:
Linus E 2022-04-25 17:45:47 +02:00
parent 14963176cc
commit 69164c655f

View File

@ -15,12 +15,13 @@ import datetime as dt
from newsapi import NewsApiClient from newsapi import NewsApiClient
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv() # loads environment vars
# Init # Init
api_key = os.getenv('NEWS_API_KEY') api_key = os.getenv('NEWS_API_KEY') # get API Key from .env file
newsapi = NewsApiClient(api_key=api_key) newsapi = NewsApiClient(api_key=api_key) # news api from https://newsapi.org/
try: try:
# get all available news sources (e.g BBC, New York Times, etc.)
source_json = requests.get(f"https://newsapi.org/v2/top-headlines/sources?apiKey={api_key}&language=en").json() source_json = requests.get(f"https://newsapi.org/v2/top-headlines/sources?apiKey={api_key}&language=en").json()
sources = source_json["sources"] sources = source_json["sources"]
str_sources = ",".join([source["id"] for source in sources]) str_sources = ",".join([source["id"] for source in sources])
@ -38,7 +39,7 @@ def get_all_news_by_keyword(keyword, from_date="2000-01-01"):
Returns: Returns:
JSON/dict: dict containing articles JSON/dict: dict containing articles
""" """
top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en', from_param=from_date) top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en', from_param=from_date) # keywords can be combined with OR (e.g. keyword = "bitcoin OR ethereum")
if(top_headlines["status"] == "ok"): if(top_headlines["status"] == "ok"):
return top_headlines return top_headlines
else: else:
@ -53,7 +54,7 @@ def get_top_news_by_keyword(keyword):
Returns: Returns:
JSON/dict: dict containing articles JSON/dict: dict containing articles
""" """
top_headlines = newsapi.get_top_headlines(q=keyword, sources=str_sources, language='en') top_headlines = newsapi.get_top_headlines(q=keyword, sources=str_sources, language='en') # get top headlines, measured by popularity from NewsApi
if(top_headlines["status"] == "ok"): if(top_headlines["status"] == "ok"):
return top_headlines return top_headlines
else: else:
@ -72,11 +73,11 @@ def format_article(article):
sourcename = article["source"]["name"] sourcename = article["source"]["name"]
headline = article["title"] headline = article["title"]
url = article["url"] url = article["url"]
formatted_article = f"_{sourcename}_\n*{headline}*\n\n{url}" formatted_article = f"_{sourcename}_\n*{headline}*\n\n{url}" # formatting in Markdown syntax
return formatted_article return formatted_article
if __name__ == '__main__': if __name__ == '__main__': # only execute if script is called directly -> for simple testing
print("this is a module and should not be run directly") print("this is a module and should not be run directly")
print("fetching top news by keyword bitcoin...") print("fetching top news by keyword bitcoin...")
@ -84,4 +85,7 @@ if __name__ == '__main__':
articles = get_all_news_by_keyword("bitcoin") articles = get_all_news_by_keyword("bitcoin")
formatted_article = format_article(articles["articles"][0]) formatted_article = format_article(articles["articles"][0])
print(formatted_article) print(formatted_article)
articles = get_top_news_by_keyword("bitcoin")
formatted_article = format_article(articles["articles"][0])
print(formatted_article)
sys.exit(1) sys.exit(1)