added date parameter
This commit is contained in:
parent
0aee85d718
commit
6289e5dc71
@ -24,6 +24,7 @@ import json
|
|||||||
|
|
||||||
import news.news_fetcher as news
|
import news.news_fetcher as news
|
||||||
import shares.share_fetcher as share_fetcher
|
import shares.share_fetcher as share_fetcher
|
||||||
|
import datetime as dt
|
||||||
|
|
||||||
from telebot import types
|
from telebot import types
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@ -247,7 +248,11 @@ def send_news(message):
|
|||||||
keywords = api_handler.get_user_keywords(user_id)
|
keywords = api_handler.get_user_keywords(user_id)
|
||||||
keywords_search = ' OR '.join(keywords)
|
keywords_search = ' OR '.join(keywords)
|
||||||
print(keywords_search)
|
print(keywords_search)
|
||||||
news_list = news.get_all_news_by_keyword(keywords_search)["articles"][:5]
|
now = dt.datetime.now().date()
|
||||||
|
from_date = now - dt.timedelta(days=7)
|
||||||
|
from_date_formatted = dt.datetime.strftime(from_date, '%Y-%m-%d')
|
||||||
|
print(from_date_formatted)
|
||||||
|
news_list = news.get_all_news_by_keyword(keywords_search, from_date_formatted)["articles"][:5]
|
||||||
|
|
||||||
if news_list:
|
if news_list:
|
||||||
for article in news_list:
|
for article in news_list:
|
||||||
|
@ -11,6 +11,7 @@ import sys
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
import datetime as dt
|
||||||
|
|
||||||
from newsapi import NewsApiClient
|
from newsapi import NewsApiClient
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@ -25,15 +26,16 @@ sources = source_json["sources"]
|
|||||||
str_sources = ",".join([source["id"] for source in sources])
|
str_sources = ",".join([source["id"] for source in sources])
|
||||||
|
|
||||||
|
|
||||||
def get_all_news_by_keyword(keyword, from_date="2022-01-01", to_date="2022-03-29"): # hard coded will change soon
|
def get_all_news_by_keyword(keyword, from_date="2000-01-01"): # hard coded will change soon
|
||||||
"""get all news to keyword
|
"""get all news to keyword
|
||||||
Args:
|
Args:
|
||||||
keyword (String): keyword for search
|
keyword (String): keyword for search
|
||||||
|
from_date (String): min date for search
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
JSON/dict: dict containing articles
|
JSON/dict: dict containing articles
|
||||||
"""
|
"""
|
||||||
top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en')
|
top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en', from_param=from_date)
|
||||||
return top_headlines
|
return top_headlines
|
||||||
|
|
||||||
def format_article(article):
|
def format_article(article):
|
||||||
@ -54,7 +56,8 @@ def format_article(article):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
print("fetching top news by keyword business...")
|
print("this is a module and should not be run directly")
|
||||||
|
print("fetching top news by keyword bitcoin...")
|
||||||
|
|
||||||
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])
|
||||||
|
Loading…
Reference in New Issue
Block a user