77import dynaconfig
88import news_db
99
10- # Time range for sending messages
11- TIME_TO_SEND_START = datetime . now (). replace ( hour = 10 , minute = 00 ). strftime ( "%H:%M" )
12- TIME_TO_SEND_END = datetime . now (). replace ( hour = 20 , minute = 00 ). strftime ( "%H:%M" )
10+ # Time range for sending messages loaded from settings.toml
11+ TIME_TO_SEND_START = dynaconfig . settings [ "TIMINIGS" ][ "TIME_TO_SEND_START" ]
12+ TIME_TO_SEND_END = dynaconfig . settings [ "TIMINIGS" ][ "TIME_TO_SEND_END" ]
1313
1414# Logging
1515logging .basicConfig (
1616 format = "%(asctime)s - %(levelname)s - %(message)s" , level = logging .INFO
1717)
18+ logging .basicConfig (
19+ format = "%(asctime)s - %(levelname)s - %(message)s" , level = logging .WARNING
20+ )
1821logging .basicConfig (
1922 format = "%(asctime)s - %(levelname)s - %(message)s" , level = logging .ERROR
2023)
2124
2225
23- API_TOKEN = dynaconfig .settings ["API_TOKEN" ]
26+ API_TOKEN = dynaconfig .settings ["TELEGRAM" ][ " API_TOKEN" ]
2427API_URL = f"https://api.telegram.org/bot{ API_TOKEN } /sendMessage"
25- CHAT_ID = dynaconfig .settings ["CHAT_ID" ]
28+ CHAT_ID = dynaconfig .settings ["TELEGRAM" ][ " CHAT_ID" ]
2629
2730
2831def send_news_to_telegram (message ):
@@ -32,7 +35,21 @@ def send_news_to_telegram(message):
3235 :return:
3336 """
3437 try :
35- response = requests .post (API_URL , json = {"chat_id" : CHAT_ID , "text" : message })
38+ response = requests .post (
39+ API_URL ,
40+ json = {
41+ "chat_id" : CHAT_ID ,
42+ "text" : f"Author: { message [0 ]} \n "
43+ f"\n "
44+ f"Title: { message [1 ]} \n "
45+ f"\n "
46+ f"{ message [2 ]} \n "
47+ f"\n "
48+ f"URL: { message [3 ]} \n "
49+ f"\n "
50+ f"Date published: { message [4 ]} \n " ,
51+ },
52+ )
3653 if response .status_code == 200 :
3754 logging .info (
3855 f"Sent: { response .reason } . Status code: { response .status_code } "
@@ -46,14 +63,21 @@ def send_news_to_telegram(message):
4663
4764
4865if __name__ == "__main__" :
49- data_from_db = news_db .create_connection (news_db .DB_FILE )
66+ db_connection = news_db .create_connection (news_db .DB_FILE )
5067 while True :
51- time .sleep (1 )
52- current_time = datetime .now ().strftime ("%H:%M" )
53- if TIME_TO_SEND_START < current_time < TIME_TO_SEND_END :
54- logging .info (f"Time: { current_time } . Time to send has come !" )
55- for news in news_db .send_all_news (data_from_db ):
56- send_news_to_telegram (news )
57- time .sleep (300 )
68+ if db_connection :
69+ time .sleep (1 )
70+ current_time = datetime .now ().strftime ("%H:%M" )
71+ if TIME_TO_SEND_START < current_time < TIME_TO_SEND_END :
72+ logging .info ("Time to send has come !" )
73+ data_from_db = news_db .send_all_news (db_connection )
74+ if len (data_from_db ) == 0 :
75+ logging .warning ("Database is empty !" )
76+ else :
77+ for news in data_from_db :
78+ send_news_to_telegram (news )
79+ time .sleep (3 )
80+ else :
81+ logging .info ("Still waiting to send." )
5882 else :
59- logging .info ( f"Time: { current_time } . Still waiting to send. " )
83+ logging .error ( "Connection to db is not exist ! " )
0 commit comments