Skip to content

Commit 8306751

Browse files
committed
Fix time fetch logic; simmplified fetching data from db; more info about services status
1 parent 165150c commit 8306751

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

datapath.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import dynaconfig
88
import news_db
99

10-
current_time = datetime.now().strftime("%H:%M")
1110
# Time range for sending messages
1211
TIME_TO_SEND_START = datetime.now().replace(hour=10, minute=00).strftime("%H:%M")
1312
TIME_TO_SEND_END = datetime.now().replace(hour=20, minute=00).strftime("%H:%M")
@@ -42,10 +41,13 @@ def send_news_to_telegram(message):
4241

4342

4443
if __name__ == "__main__":
44+
data_from_db = news_db.create_connection(news_db.DB_FILE)
4545
while True:
46+
current_time = datetime.now().strftime("%H:%M")
4647
if TIME_TO_SEND_START < current_time < TIME_TO_SEND_END:
47-
for news in news_db.send_all_news(
48-
news_db.create_connection(news_db.DB_FILE)
49-
):
50-
time.sleep(300)
48+
logging.info(f"Time: {current_time}. Time to send has come !")
49+
for news in news_db.send_all_news(data_from_db):
5150
send_news_to_telegram(news)
51+
time.sleep(300)
52+
else:
53+
logging.info(f"Time: {current_time}. Still waiting to send.")

news_db.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import logging
22
import sqlite3
3+
import time
34
from datetime import datetime
45
from pathlib import Path
56
from sqlite3 import Error
67

7-
now = datetime.now()
8-
9-
CURRENT_TIME = now.strftime("%H:%M")
108
TIME_TO_PURGE = "00:00"
119

1210

@@ -120,10 +118,14 @@ def delete_all_news(conn):
120118
create_table(conn, CREATE_TABLE_SQL)
121119

122120
while True:
121+
time.sleep(1)
122+
CURRENT_TIME = datetime.now().strftime("%H:%M")
123123
if conn is not None:
124124
send_all_news(conn)
125125
if CURRENT_TIME == TIME_TO_PURGE:
126-
logging.info("Time to purge has come !")
126+
logging.info(f"Time: {CURRENT_TIME}. Time to purge has come !")
127127
delete_all_news(conn)
128+
else:
129+
logging.info(f"Time: {CURRENT_TIME}. Still waiting for purging.")
128130
else:
129131
logging.error("Error! Cannot create the database connection.")

truth_seeker.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import time
23
from datetime import date, datetime, timedelta
34

45
from newsapi import NewsApiClient
@@ -14,10 +15,7 @@
1415

1516
newsapi = NewsApiClient(api_key=API_KEY)
1617

17-
now = datetime.now()
18-
19-
CURRENT_TIME = now.strftime("%H:%M")
20-
TIME_TO_SEARCH = "01:00"
18+
TIME_TO_SEARCH = "14:10"
2119

2220

2321
# Logging
@@ -71,10 +69,14 @@ def load_to_db(fetch_info: dict):
7169

7270
if __name__ == "__main__":
7371
while True:
72+
time.sleep(1)
73+
CURRENT_TIME = datetime.now().strftime("%H:%M")
7474
# Pull too much info
7575
if CURRENT_TIME == TIME_TO_SEARCH:
76-
logging.info("Time to search has come !")
76+
logging.info(f"Time: {CURRENT_TIME}. Time to search has come !")
7777
try:
7878
load_to_db(fetch_info())
7979
except BaseException as base_err:
8080
logging.error(base_err)
81+
else:
82+
logging.info(f"Time: {CURRENT_TIME}. Still waiting for searching.")

0 commit comments

Comments
 (0)