moved news_fetcher to new dir
This commit is contained in:
60
telegram_bot/news/news_fetcher.py
Normal file
60
telegram_bot/news/news_fetcher.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
script for news fetching (by keywords)
|
||||
"""
|
||||
__author__ = "Florian Kellermann, Linus Eickhoff"
|
||||
__date__ = "15.03.2022"
|
||||
__version__ = "0.0.1"
|
||||
__license__ = "None"
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from newsapi import NewsApiClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Init
|
||||
newsapi = NewsApiClient(api_key=os.getenv('NEWS_API_KEY'))
|
||||
|
||||
|
||||
# /v2/top-headlines/sources
|
||||
sources = newsapi.get_sources()
|
||||
|
||||
def get_top_news_by_keyword(keyword):
|
||||
"""get top news to keyword
|
||||
Args:
|
||||
keyword (String): keyword for search
|
||||
|
||||
Returns:
|
||||
JSON/dict: dict containing articles
|
||||
"""
|
||||
top_headlines = newsapi.get_top_headlines(q=keyword, sources='bbc-news,the-verge,cnn', language='en')
|
||||
return top_headlines
|
||||
|
||||
def format_article(article):
|
||||
"""format article for messaging (using markdown syntax)
|
||||
|
||||
Args:
|
||||
article (dict): article to format for messaging
|
||||
|
||||
Returns:
|
||||
String: formatted article
|
||||
"""
|
||||
sourcename = article["source"]["name"]
|
||||
headline = article["title"]
|
||||
url = article["url"]
|
||||
formatted_article = f"_{sourcename}_\n*{headline}*\n\n{url}"
|
||||
|
||||
return formatted_article
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
print("fetching top news by keyword business...")
|
||||
|
||||
articles = get_top_news_by_keyword("business")
|
||||
formatted_article = format_article(articles["articles"][0])
|
||||
print(formatted_article)
|
Reference in New Issue
Block a user