-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
102 lines (76 loc) · 2.95 KB
/
main.py
File metadata and controls
102 lines (76 loc) · 2.95 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from ElpamBot import ElpamBot
from Parser import Parser
from WebMonitor import WebMonitor
import configparser
import time
import threading
def get_config(filename):
config = configparser.ConfigParser()
config.read(filename)
assert 'Bot' in config.sections(), 'Bot section must not None!'
assert 'token' in config['Bot'], 'token in Bot must not None!'
assert 'WebMonitor' in config.sections(), 'WebMonitor must not None!'
assert 'url' in config['WebMonitor'], 'url in WebMonitor must not None!'
if 'Subscribers' not in config.sections():
config.add_section('Subscribers')
return config
lock = threading.Lock()
def update_config(config):
with lock:
with open('ElpamBot.ini', 'w', buffering=1) as fp:
config.write(fp)
def main():
config_file_path = 'ElpamBot.ini'
config = get_config(config_file_path)
bot_config = config['Bot']
web_monitor_config = config['WebMonitor']
parser_configs = []
for section in config.sections():
if section.startswith('Parser'):
parser_configs.append(config[section])
subscribers_section = config['Subscribers']
# init bot
receivers = set(map(int, subscribers_section.keys()))
elpam_bot = ElpamBot(bot_config.get('token'), proxy=bot_config.get('proxy'), receivers=receivers)
# 用户订阅消息回调
@elpam_bot.event_receiver('subscribe')
def on_subscribe(message):
chat_id = str(message.chat_id)
if chat_id not in subscribers_section.keys():
data = {
'id': message.chat.id,
'type': message.chat.type,
'username': message.chat.username,
'first_name': message.chat.first_name,
'timestamp': int(time.time()),
}
config.set('Subscribers', chat_id, str(data))
update_config(config)
@elpam_bot.event_receiver('unsubscribe')
def on_unsubscribe(message):
chat_id = str(message.chat_id)
if chat_id in subscribers_section.keys():
config.remove_option('Subscribers', chat_id)
update_config(config)
# init parsers
parsers = []
for parser_config in parser_configs:
parsers.append(Parser.create_parser(**dict(parser_config)))
# init web monitor
headers = {}
user_agent = web_monitor_config.get('user_agent')
if user_agent:
headers['User-Agent'] = user_agent
url = web_monitor_config.get('url')
interval = web_monitor_config.getint('interval')
def changed_callback(data):
elpam_bot.notify(data)
config.set('WebMonitor', 'last_data', monitor.last_data or '')
update_config(config)
last_data = web_monitor_config.get('last_data')
monitor = WebMonitor(url, parsers, changed_callback=changed_callback, interval=interval, headers=headers, last_data=last_data)
# start bot
elpam_bot.start(blocking=False)
monitor.start()
if __name__ == '__main__':
main()