added auto sources for news
This commit is contained in:
parent
3b675cf7fc
commit
536bdf3bd6
@ -205,7 +205,7 @@ def send_news(message):
|
|||||||
:rtype: none
|
:rtype: none
|
||||||
"""
|
"""
|
||||||
|
|
||||||
keyword = "business"
|
keyword = "bitcoin"
|
||||||
user_id = int(message.from_user.id)
|
user_id = int(message.from_user.id)
|
||||||
#Get Information for user with this id
|
#Get Information for user with this id
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ __license__ = "None"
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
import requests
|
||||||
|
|
||||||
from newsapi import NewsApiClient
|
from newsapi import NewsApiClient
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@ -16,7 +17,12 @@ from dotenv import load_dotenv
|
|||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
# Init
|
# Init
|
||||||
newsapi = NewsApiClient(api_key=os.getenv('NEWS_API_KEY'))
|
api_key = os.getenv('NEWS_API_KEY')
|
||||||
|
newsapi = NewsApiClient(api_key=api_key)
|
||||||
|
source_json = requests.get(f"https://newsapi.org/v2/top-headlines/sources?apiKey={api_key}&language=en").json()
|
||||||
|
sources = source_json["sources"]
|
||||||
|
str_sources = ",".join([source["id"] for source in sources])
|
||||||
|
|
||||||
|
|
||||||
def get_top_news_by_keyword(keyword):
|
def get_top_news_by_keyword(keyword):
|
||||||
"""get top news to keyword
|
"""get top news to keyword
|
||||||
@ -26,7 +32,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='bbc-news,the-verge,cnn,buzzfeed,fox', language='en')
|
top_headlines = newsapi.get_top_headlines(q=keyword, sources=str_sources, language='en')
|
||||||
return top_headlines
|
return top_headlines
|
||||||
|
|
||||||
def format_article(article):
|
def format_article(article):
|
||||||
@ -49,6 +55,6 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
print("fetching top news by keyword business...")
|
print("fetching top news by keyword business...")
|
||||||
|
|
||||||
articles = get_top_news_by_keyword("business")
|
articles = get_top_news_by_keyword("bitcoin")
|
||||||
formatted_article = format_article(articles["articles"][0])
|
formatted_article = format_article(articles["articles"][0])
|
||||||
print(formatted_article)
|
print(formatted_article)
|
Loading…
Reference in New Issue
Block a user