-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
92 lines (82 loc) · 2.42 KB
/
bot.py
File metadata and controls
92 lines (82 loc) · 2.42 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
# -*- coding: utf-8 -*-
# stdlib modules
import logging
from asyncio import sleep
from argparse import ArgumentParser
from datetime import datetime, timedelta
from functools import partial
# local modules
from src.db.db import Sqlite3
from src.preloader import load_env, load_config
from src.tglib.tg_client import TGClient, TGClientError
from src.appkiller.killer import GracefulKiller
parser = ArgumentParser(description='Telegram bot for parsing and posting messages.')
parser.add_argument(
'--env-file',
action='store',
type=str,
default='.env',
help='path to environment file (default .env)',
)
parser.add_argument(
'--db-file',
action='store',
type=str,
default='data.db',
help='path to sqlite3 .db file (default data.db)',
)
parser.add_argument(
'--config-file',
action='store',
type=str,
default='config.json',
help='path to config file (default config.json)',
)
args = parser.parse_args()
args = vars(args)
logging.info(args)
env = load_env(args['env_file'])
config = load_config(args['config_file'])
db = Sqlite3(args['db_file'])
client = TGClient(
session='anon',
api_id=env['API_ID'],
api_hash=env['API_HASH'],
config=config,
db=db,
)
killer = GracefulKiller()
async def main():
try:
client.check_connection()
except TGClientError:
logging.error('Got error from TG Client, exiting...')
db._disconnect()
exit(-1)
await client.add_new_entities_db(['channel_id', 'message_id'], 'channels', client.channels)
await client.add_new_entities_db(['user_id', 'message_id'], 'admins', client.admins)
while not killer.kill_now:
logging.info(f'Crawling, next run at {datetime.now()+timedelta(seconds=10)}')
await client.crawl_entities(
client.channels,
partial(
client.crawl_entity_messages,
table='channels',
where_column='channel_id',
message_function=client.send_channel_message,
),
)
await client.crawl_entities(
client.admins,
partial(
client.crawl_entity_messages,
table='admins',
where_column='user_id',
message_function=client.check_admin_reaction,
),
)
await sleep(10)
db.connection.close()
logging.info('Stopping...')
with client:
client.loop.run_until_complete(main())