Skip to content

Commit 07304d6

Browse files
Merge pull request #17 from CoolCoderCarl/develop
Develop
2 parents e2900a5 + 06b4ac8 commit 07304d6

File tree

6 files changed

+72
-26
lines changed

6 files changed

+72
-26
lines changed

.dockerignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# Docker
88
docker-compose.yml
9+
docker-compose.yaml
910
**/Dockerfile
1011
**/Containerfile*
1112
.docker
@@ -14,4 +15,9 @@ docker-compose.yml
1415
# Meta
1516
README.md
1617
LICENSE
17-
requirements.txt
18+
19+
# Configuration
20+
requirements.txt
21+
changelog.json
22+
settings.toml
23+
*.db

.github/workflows/publish.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,5 @@ jobs:
129129
<b>Pushed image:</b> <code>docker pull h0d0user/news_db:${{ needs.prepare.outputs.get_current_tag }}</code>,
130130
<code>docker pull h0d0user/truth_seeker:${{ needs.prepare.outputs.get_current_tag }}</code>,
131131
<code>docker pull h0d0user/datapath:${{ needs.prepare.outputs.get_current_tag }}</code>
132-
See changes: https://github.com/${{ github.repository }}/commit/${{ github.sha }}
133132
134133
See changes: https://github.com/${{ github.repository }}/commit/${{ github.sha }}

README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,42 @@ You can check the last available tags here -
1717
3) https://hub.docker.com/repository/docker/h0d0user/news_db
1818

1919
Need to fill `settings.toml` with next important variables:
20-
1) `API_KEY`. You can find this data here - https://my.telegram.org/apps
21-
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/botAPI_TOKEN/getUpdates
23-
4) `DB_NAME`. Where you want to load your data before send to telegram.
24-
5) `QUERY`. Key word to search for in articles.
20+
1) `DB_NAME`. Where you want to load your data before send to telegram.
21+
2) `API_KEY`. You can find this data here - https://my.telegram.org/apps
22+
3) `QUERY`. Key word to search for in articles.
23+
4) `API_TOKEN`. Ask *BotFather* in telegram.
24+
5) `CHAT_ID`. Use this to find chat ID where you want to send messages - https://api.telegram.org/botAPI_TOKEN/getUpdates
2525
6) `docker-compose up -d`
2626
7) ...
2727
8) PROFIT !!!
2828

29+
*It is not final configuration. You can find template below.*
30+
31+
File `settings.toml` template:
32+
```
33+
[DB]
34+
DB_NAME = "/mnt/test.db"
35+
36+
37+
[NEWS_API]
38+
API_KEY = ""
39+
QUERY = "test"
40+
LANGUAGE = "en"
41+
42+
43+
[TELEGRAM]
44+
API_TOKEN = ""
45+
CHAT_ID = ""
46+
47+
48+
[TIMINIGS]
49+
TIME_TO_PURGE = "00:00"
50+
TIME_TO_SEARCH = "02:00"
51+
TIME_TO_SEND_START = "10:00"
52+
TIME_TO_SEND_END = "20:00"
53+
SENDING_INTERVAL = 300
54+
```
55+
56+
57+
2958
**Still have questions ? Google it.**

datapath.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,16 @@ def send_news_to_telegram(message):
7070
time.sleep(1)
7171
current_time = datetime.now().strftime("%H:%M")
7272
if TIME_TO_SEND_START < current_time < TIME_TO_SEND_END:
73-
logging.info("Time to send has come !")
73+
logging.info("Time to send news has come !")
7474
data_from_db = news_db.send_all_news(db_connection)
7575
if len(data_from_db) == 0:
7676
logging.warning("Database is empty !")
7777
else:
7878
for news in data_from_db:
7979
send_news_to_telegram(news)
8080
time.sleep(SENDING_INTERVAL)
81+
else:
82+
logging.warning("All news was sent !")
8183
else:
8284
logging.info("Still waiting to send.")
8385
else:

news_db.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@
2525
INSERT_INTO_SQL = """
2626
INSERT INTO news
2727
(author,title,description,url,pub_date)
28-
VALUES(?,?,?,?,?)
28+
VALUES(?,?,?,?,?);
2929
"""
3030

3131

3232
SELECT_FROM_SQL = """
33-
SELECT * FROM news
33+
SELECT * FROM news;
3434
"""
3535

3636
DELETE_FROM_SQL = """
37-
DELETE FROM news
37+
DELETE FROM news;
38+
"""
39+
40+
SELECT_COUNT_SQL = """
41+
SELECT COUNT(*) FROM news;
3842
"""
3943

4044

@@ -47,6 +51,10 @@
4751
)
4852

4953

54+
def check_entities_count(conn):
55+
return conn.cursor().execute(SELECT_COUNT_SQL).fetchone()[0]
56+
57+
5058
def create_connection(db_file: Path):
5159
"""
5260
Create db file
@@ -71,7 +79,7 @@ def create_table(conn, create_table_query):
7179
try:
7280
c = conn.cursor()
7381
c.execute(create_table_query)
74-
logging.info("Table created successfully !")
82+
logging.info(f"Table created successfully !")
7583
except Error as create_table_err:
7684
logging.error(create_table_err)
7785

@@ -88,7 +96,9 @@ def insert_into(conn, data: tuple):
8896
cur = conn.cursor()
8997
cur.execute(INSERT_INTO_SQL, data)
9098
conn.commit()
91-
logging.info("Data inserted successfully !")
99+
logging.info(
100+
f"Data inserted successfully ! Entities in db for now: {check_entities_count(conn)}"
101+
)
92102
return cur.lastrowid
93103
except Error as insert_err:
94104
logging.error(insert_err)
@@ -100,12 +110,7 @@ def send_all_news(conn):
100110
:param conn: Connection to the SQLite database
101111
:return:
102112
"""
103-
cur = conn.cursor()
104-
cur.execute(SELECT_FROM_SQL)
105-
106-
rows = cur.fetchall()
107-
108-
return rows
113+
return conn.cursor().execute(SELECT_FROM_SQL).fetchall()
109114

110115

111116
def delete_all_news(conn):
@@ -114,10 +119,11 @@ def delete_all_news(conn):
114119
:param conn: Connection to the SQLite database
115120
:return:
116121
"""
117-
cur = conn.cursor()
118-
cur.execute(DELETE_FROM_SQL)
122+
conn.cursor().execute(DELETE_FROM_SQL)
119123
conn.commit()
120-
logging.info("Database was purged !")
124+
logging.info(
125+
f"Database was purged ! Entities in db for now: {check_entities_count(conn)}"
126+
)
121127

122128

123129
if __name__ == "__main__":
@@ -132,9 +138,13 @@ def delete_all_news(conn):
132138
if conn is not None:
133139
send_all_news(conn)
134140
if CURRENT_TIME == TIME_TO_PURGE:
135-
logging.info("Time to purge has come !")
141+
logging.info(
142+
f"Time to purge has come ! Entities in db for now: {check_entities_count(conn)}"
143+
)
136144
delete_all_news(conn)
137145
else:
138-
logging.info("Still waiting for purging.")
146+
logging.info(
147+
f"Still waiting for purging. Entities in db for now: {check_entities_count(conn)}"
148+
)
139149
except sqlite3.Error as sql_err:
140150
logging.error(f"Cannot create the database connection. Error: {sql_err}")

truth_seeker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def load_to_db(fetch_info: dict):
7373
article_list.append(article_data)
7474
news_db.insert_into(
7575
news_db.create_connection(news_db.DB_FILE),
76-
tuple(article_list),
76+
# Pull too much info but add set conversion
77+
tuple(set(article_list)),
7778
)
7879
else:
7980
logging.warning("Empty response from News API.")
@@ -84,7 +85,6 @@ def load_to_db(fetch_info: dict):
8485
while True:
8586
CURRENT_TIME = datetime.now().strftime("%H:%M")
8687
time.sleep(1)
87-
# Pull too much info
8888
if CURRENT_TIME == TIME_TO_SEARCH:
8989
logging.info("Time to search has come !")
9090
try:

0 commit comments

Comments
 (0)