Skip to content

Commit 12ea288

Browse files
Merge 5f2a66f into 28a2010
2 parents 28a2010 + 5f2a66f commit 12ea288

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

README.md

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,47 @@
11
# datapath
2-
3-
Simple ETL getting info from https://newsapi.org/ and send to telegram channel
2+
Send info getting from API to telegram.
3+
Third part of a system which search, save and share info.
44

55
## Prehistory
6-
This simple service help to search information automatically via News API
6+
It is good idea to share info.
77

88
> Datapath it is like telepath or astropath
99
> (c) Author
1010
1111
Enjoy.
1212

1313
## How to use
14-
You can check the last available tags here -
15-
1) https://hub.docker.com/repository/docker/h0d0user/truth_seeker
16-
2) https://hub.docker.com/repository/docker/h0d0user/datapath
17-
3) https://hub.docker.com/repository/docker/h0d0user/news_db
18-
1914
Need to fill `settings.toml` with next important variables:
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
25-
6) `docker-compose up -d`
26-
7) ...
27-
8) PROFIT !!!
28-
29-
*It is not final configuration. You can find template below.*
30-
31-
File `settings.toml` template:
15+
1) `API_KEY`. You can find this data here - https://my.telegram.org/apps
16+
2) `QUERY`. Key word to search for in articles.
17+
3) `API_TOKEN`. Ask *BotFather* in telegram.
18+
4) `CHAT_ID`. Use this to find chat ID where you want to send messages - https://api.telegram.org/botAPI_TOKEN/getUpdates
19+
5) `docker-compose up -d` or `make dstart`
20+
6) ...
21+
7) PROFIT !!!
22+
3223
```
3324
[DB]
34-
DB_NAME = "/mnt/test.db"
35-
25+
DB_API_URL = "http://attainments_sanctuary:8888"
3626
3727
[NEWS_API]
3828
API_KEY = ""
39-
QUERY = "test"
29+
QUERY = "computer science"
4030
LANGUAGE = "en"
4131
42-
4332
[TELEGRAM]
4433
API_TOKEN = ""
4534
CHAT_ID = ""
4635
47-
4836
[TIMINIGS]
49-
TIME_TO_PURGE = "00:00"
5037
TIME_TO_SEARCH = "02:00"
5138
TIME_TO_SEND_START = "10:00"
5239
TIME_TO_SEND_END = "20:00"
5340
SENDING_INTERVAL = 300
5441
```
5542

56-
43+
For more info check:
44+
1) API & DB repo - https://github.com/CoolCoderCarl/attainments_sanctuary
45+
2) ETL repo - https://github.com/CoolCoderCarl/epistolary
5746

5847
**Still have questions ? Google it.**

datapath.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def check_api_available() -> bool:
3737
return requests.get(f"{NEWS_DB_API_URL}/healthcheck").ok
3838
except (ConnectionError, ConnectionRefusedError) as con_err:
3939
logging.error(con_err)
40+
return False
4041

4142

4243
def send_news_to_telegram(message):
@@ -73,23 +74,47 @@ def send_news_to_telegram(message):
7374
logging.error(err)
7475

7576

77+
def ask_entities() -> int:
78+
"""
79+
Ask API request to discover how much entities in db
80+
Return int instead of str
81+
:return:
82+
"""
83+
return int(requests.get(f"{NEWS_DB_API_URL}/entities").text)
84+
85+
7686
if __name__ == "__main__":
7787
while True:
7888
if check_api_available():
79-
time.sleep(1)
8089
current_time = datetime.now().strftime("%H:%M")
8190
if TIME_TO_SEND_START < current_time < TIME_TO_SEND_END:
8291
logging.info("Time to send news has come !")
83-
data_from_db = requests.get(NEWS_DB_API_URL).json()
84-
if len(data_from_db) == 0:
85-
logging.warning("Database is empty !")
92+
if ask_entities() == 0:
93+
logging.warning("Database is empty ! Take a break for 30 min.")
94+
time.sleep(1800)
8695
else:
87-
for news in data_from_db:
88-
send_news_to_telegram(news)
89-
time.sleep(SENDING_INTERVAL)
90-
else:
91-
logging.warning("All news was sent !")
96+
try:
97+
data_from_db = requests.get(f"{NEWS_DB_API_URL}/news").json()
98+
for news in data_from_db:
99+
send_news_to_telegram(news)
100+
time.sleep(SENDING_INTERVAL)
101+
else:
102+
logging.warning(
103+
f"All news was sent ! Going to purge ! Entities in db for now {ask_entities()}."
104+
)
105+
try:
106+
response = requests.get(f"{NEWS_DB_API_URL}/purge")
107+
except Exception as exception:
108+
logging.error(f"Exception while purging: {exception}.")
109+
else:
110+
logging.info(
111+
f"Database was purged successfully ! Entities in db for now {ask_entities()}."
112+
)
113+
except Exception as exception:
114+
logging.error(f"Exception while getting news: {exception}")
92115
else:
93116
logging.info("Still waiting to send.")
117+
time.sleep(5)
94118
else:
95119
logging.error("API is not available !")
120+
time.sleep(5)

0 commit comments

Comments
 (0)