Skip to content

Commit b7b43dc

Browse files
Merge pull request #11 from CoolCoderCarl/feature/FUNC_5
Feature/func 5
2 parents 177e014 + d73493f commit b7b43dc

7 files changed

Lines changed: 70 additions & 49 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# truth_seeker
1+
# datapath
22

33
Simple ETL getting info from https://newsapi.org/ and send to telegram channel
44

@@ -19,7 +19,7 @@ You can check the last available tags here -
1919
Need to fill `settings.toml` with next important variables:
2020
1) `API_KEY`. You can find this data here - https://my.telegram.org/apps
2121
2) `API_TOKEN`. Ask *BotFather* in telegram.
22-
3) `CHAT_ID`. Use this to find chat ID where you want to send messages - https://api.telegram.org/bot<API_TOKEN>/getUpdates
22+
3) `CHAT_ID`. Use this to find chat ID where you want to send messages - https://api.telegram.org/botAPI_TOKEN/getUpdates
2323
4) `DB_NAME`. Where you want to load your data before send to telegram.
2424
5) `QUERY`. Key word to search for in articles.
2525
6) `docker-compose up -d`

datapath.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@
77
import dynaconfig
88
import 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
1515
logging.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+
)
1821
logging.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"]
2427
API_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

2831
def send_news_to_telegram(message):
@@ -46,14 +49,21 @@ def send_news_to_telegram(message):
4649

4750

4851
if __name__ == "__main__":
49-
data_from_db = news_db.create_connection(news_db.DB_FILE)
52+
db_connection = news_db.create_connection(news_db.DB_FILE)
5053
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)
54+
if db_connection:
55+
time.sleep(1)
56+
current_time = datetime.now().strftime("%H:%M")
57+
if TIME_TO_SEND_START < current_time < TIME_TO_SEND_END:
58+
logging.info("Time to send has come !")
59+
data_from_db = news_db.send_all_news(db_connection)
60+
if len(data_from_db) == 0:
61+
logging.warning("Database is empty !")
62+
else:
63+
for news in data_from_db:
64+
send_news_to_telegram(news)
65+
time.sleep(300)
66+
else:
67+
logging.info("Still waiting to send.")
5868
else:
59-
logging.info(f"Time: {current_time}. Still waiting to send.")
69+
logging.error("Connection to db is not exist !")

docker-compose.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,36 @@ services:
44
news_db:
55
container_name: news_db
66
image: h0d0user/news_db:latest
7+
pull_policy: always
78
restart: always
89
network_mode: host
910
volumes:
1011
- "shared-volume:/mnt/"
11-
- "settings.toml:/opt/settings.toml"
12+
- "./settings.toml:/opt/settings.toml"
1213

1314
truth_seeker:
1415
container_name: truth_seeker
1516
image: h0d0user/truth_seeker:latest
17+
pull_policy: always
1618
restart: always
1719
network_mode: host
1820
depends_on:
1921
- "news_db"
2022
volumes:
2123
- "shared-volume:/mnt/"
22-
- "settings.toml:/opt/settings.toml"
24+
- "./settings.toml:/opt/settings.toml"
2325

2426
datapath:
2527
container_name: datapath
2628
image: h0d0user/datapath:latest
29+
pull_policy: always
2730
restart: always
2831
network_mode: host
2932
depends_on:
3033
- "news_db"
3134
volumes:
3235
- "shared-volume:/mnt/"
33-
- "settings.toml:/opt/settings.toml"
36+
- "./settings.toml:/opt/settings.toml"
3437

3538
volumes:
3639
shared-volume:

news_db.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import dynaconfig
99

10-
TIME_TO_PURGE = "00:00"
11-
12-
DB_FILE = Path(f"/mnt/{dynaconfig.settings['DB_NAME']}")
10+
# Timing and db name loaded from settings.toml
11+
TIME_TO_PURGE = dynaconfig.settings["TIMINIGS"]["TIME_TO_PURGE"]
12+
DB_FILE = Path(f"{dynaconfig.settings['DB']['DB_NAME']}")
1313

1414
# SQL queries
1515
CREATE_TABLE_SQL = """
@@ -121,19 +121,20 @@ def delete_all_news(conn):
121121

122122

123123
if __name__ == "__main__":
124-
conn = create_connection(DB_FILE)
125-
if DB_FILE.exists():
126-
create_table(conn, CREATE_TABLE_SQL)
127-
128-
while True:
129-
CURRENT_TIME = datetime.now().strftime("%H:%M")
130-
time.sleep(1)
131-
if conn is not None:
132-
send_all_news(conn)
133-
if CURRENT_TIME == TIME_TO_PURGE:
134-
logging.info(f"Time: {CURRENT_TIME}. Time to purge has come !")
135-
delete_all_news(conn)
136-
else:
137-
logging.info(f"Time: {CURRENT_TIME}. Still waiting for purging.")
138-
else:
139-
logging.error("Error! Cannot create the database connection.")
124+
try:
125+
conn = create_connection(DB_FILE)
126+
if DB_FILE.exists():
127+
create_table(conn, CREATE_TABLE_SQL)
128+
129+
while True:
130+
CURRENT_TIME = datetime.now().strftime("%H:%M")
131+
time.sleep(1)
132+
if conn is not None:
133+
send_all_news(conn)
134+
if CURRENT_TIME == TIME_TO_PURGE:
135+
logging.info("Time to purge has come !")
136+
delete_all_news(conn)
137+
else:
138+
logging.info("Still waiting for purging.")
139+
except sqlite3.Error as sql_err:
140+
logging.error(f"Cannot create the database connection. Error: {sql_err}")

requirements-dp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
newsapi-python
12
requests~=2.28.1
23
dynaconf~=3.1.11
34
dynaconfig~=0.4

requirements.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
black
2-
isort
2+
isort
3+
newsapi-python
4+
requests~=2.28.1
5+
dynaconf~=3.1.11
6+
dynaconfig~=0.4

truth_seeker.py

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

10-
# Load variables from settings.toml
11-
API_KEY = dynaconfig.settings["API_KEY"]
12-
QUERY = dynaconfig.settings["QUERY"]
10+
# News API params loaded from settings.toml
11+
API_KEY = dynaconfig.settings["NEWS_API"]["API_KEY"]
12+
QUERY = dynaconfig.settings["NEWS_API"]["QUERY"]
13+
LANGUAGE = dynaconfig.settings["NEWS_API"]["LANGUAGE"]
14+
TIME_TO_SEARCH = dynaconfig.settings["TIMINGS"]["TIME_TO_SEARCH"]
1315

1416
newsapi = NewsApiClient(api_key=API_KEY)
1517

16-
TIME_TO_SEARCH = "11:10"
17-
1818

1919
# Logging
2020
logging.basicConfig(
@@ -37,10 +37,12 @@ def fetch_info() -> dict:
3737
# Use to get news from yesterday to current time
3838
yesterday = date.today() - timedelta(days=1)
3939
try:
40+
logging.info(
41+
f"Searching for {QUERY}; most popular; from: {yesterday}; language: {LANGUAGE}"
42+
)
4043
result = newsapi.get_everything(
41-
q=QUERY, sort_by="popularity", from_param=yesterday
44+
q=QUERY, sort_by="popularity", from_param=yesterday, language=LANGUAGE
4245
)
43-
logging.info(f"Searching for {QUERY}")
4446
return result
4547
except ConnectionError as con_err:
4648
logging.error(con_err)
@@ -74,7 +76,7 @@ def load_to_db(fetch_info: dict):
7476
tuple(article_list),
7577
)
7678
else:
77-
logging.warning("Empty response from API.")
79+
logging.warning("Empty response from News API.")
7880
raise IndexError
7981

8082

@@ -84,10 +86,10 @@ def load_to_db(fetch_info: dict):
8486
time.sleep(1)
8587
# Pull too much info
8688
if CURRENT_TIME == TIME_TO_SEARCH:
87-
logging.info(f"Time: {CURRENT_TIME}. Time to search has come !")
89+
logging.info("Time to search has come !")
8890
try:
8991
load_to_db(fetch_info())
9092
except BaseException as base_err:
9193
logging.error(base_err)
9294
else:
93-
logging.info(f"Time: {CURRENT_TIME}. Still waiting for searching.")
95+
logging.info("Still waiting for searching.")

0 commit comments

Comments
 (0)