-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (58 loc) Β· 2.37 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# python lib
import os
from time import perf_counter_ns, process_time_ns
# custom lib
from core import exception_handler
from core.slack import SlackAPI
from core.config import set_env
from core.db.mongodb import Repository
from crawler.crawler import crawler_run
def send_slack_msgs(msg_txt_list: list[dict], repository: Repository):
'''
botμ μΆκ°ν λͺ¨λ μ±λμ message 보λ΄λ μ΅μμ ν¨μ
- standard_url.csvμ λ³λλ urlμ΄ κ°μ§λμ΄μΌλ§ λ¬λ
- msg_txt_listμ λ°μ΄ν° ννκ° μ€μν¨
'''
# slack
slack = SlackAPI(debug=True)
target_channels = slack.get_channel_info()
# msg_dict - # dict( url: str(λ³νκ°μ§λ url), img: str(url), msg: list[list] )
for msg_dict in msg_txt_list:
# μ±λμ μΆκ°νκ³ , λ©€λ²λ‘ μ΄λλ₯Ό ν΄μΌλ§ messageλ₯Ό λ³΄λΌ μ μλ€
for target_ch in target_channels:
if target_ch['is_member']:
repository.log_data(
"hook_log",
f"{target_ch['id']}, {target_ch['name']}, {msg_dict.get('url')}"
)
slack.post_text_message(
target_ch['id'], msg_dict.get("img"), msg_dict.get("msg")
)
if __name__ == "__main__":
start_time = perf_counter_ns() # 1ns * 10^9 = 1s
process_start_time = process_time_ns()
# νκ²½λ³μ μΈν
- λ°°ν¬ νκ²½, ν€λ‘μΏ μμλ set_env νμ X
# set_env(["slack_token", "event_check_url", "mongodb_atlas_url"])
# DBMS object
repository = Repository()
repository.log_data("process_start")
# monitor
try:
# dict( url: str(λ³νκ°μ§λ url), img: str(url), msg: list[list] )
msg_txt_list: list[dict] = crawler_run(repository)
# λ³λ κ°μ§λ λ§ν¬κ° μλ€λ©΄
if len(msg_txt_list) > 0:
send_slack_msgs(msg_txt_list, repository)
end_time = perf_counter_ns()
process_end_time = process_time_ns()
except Exception as exc:
exception_handler(exc, repository)
# logging
repository.log_data(
"run_log",
f"total run time: {end_time - start_time} ms {(end_time - start_time) * 0.000000001} second"
)
repository.log_data(
"run_log",
f"process run time: {process_end_time - process_start_time} ms {(process_end_time - process_start_time) * 0.000000001} second"
)