Skip to content

Commit fd80242

Browse files
Merge pull request #12 from CoolCoderCarl/develop
Develop
2 parents 9a04455 + e96a1e5 commit fd80242

7 files changed

Lines changed: 82 additions & 47 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: 39 additions & 15 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):
@@ -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

4865
if __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 !")

docker-compose.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ 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:
@@ -13,6 +14,7 @@ services:
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:
@@ -24,6 +26,7 @@ services:
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:

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["TIMINIGS"]["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)