forked from kalinochkind/vkbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.py
98 lines (86 loc) · 2.96 KB
/
prepare.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
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
import codecs
import importlib
import logging
import os
import sys
import time
import html
import accounts
import config
import log
from args import args
from vkapi import VkApi
from vkbot import createCaptchaHandler
os.chdir(os.path.dirname(os.path.realpath(sys.argv[0])))
accounts.init()
class MyHandler(logging.Handler):
def emit(self, record):
pass
def handle(self, record):
msg = record.getMessage()
lvl = record.levelname
if any(msg.lower().startswith(i) for i in ('red|', 'green|', 'yellow|')):
color, msg = msg.split('|', maxsplit=1)
log.info((msg, html.escape(msg)), color.lower())
return
db_msg = getattr(record, 'db', None)
if db_msg:
msg = (msg, db_msg)
if lvl == 'CRITICAL':
log.error(msg, fatal=True)
elif lvl == 'ERROR':
log.error(msg, record.exc_info is not None)
elif lvl == 'WARNING':
log.warning(msg)
elif lvl == 'INFO':
log.info(msg)
elif lvl == 'DEBUG':
log.debug(msg)
logging.basicConfig(handlers=[MyHandler()], level=logging.DEBUG)
logging.getLogger('antigate').setLevel(logging.CRITICAL)
logging.getLogger('requests').setLevel(logging.CRITICAL)
if config.get('vkbot.suppress_chat_stderr', 'b'):
logging.getLogger('chatlog').setLevel(logging.CRITICAL)
os.environ['LC_ALL'] = 'ru_RU.utf-8'
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
sys.stdout.encoding = 'UTF-8'
login = config.get('login.login')
password = config.get('login.password')
def availableScripts():
print('Available scripts:', ', '.join(sorted(i[:-3] for i in os.listdir('scripts') if i.endswith('.py') and not i.startswith('__'))))
if args['script'] is None:
availableScripts()
sys.exit()
if args['script']:
if not args['script'].replace('_', '').isalpha():
print('Invalid script')
availableScripts()
sys.exit()
log.script_name = args['script'].lower()
try:
script = importlib.import_module('scripts.' + args['script'].lower())
main = script.main
need_auth = getattr(script, 'need_auth', False)
except ImportError:
print('Invalid script')
availableScripts()
sys.exit()
v = VkApi(login, password, timeout=config.get('vkbot_timing.default_timeout', 'i'), token_file=accounts.getFile('token.txt'),
log_file=accounts.getFile('inf.log') if args['logging'] else '', captcha_handler=createCaptchaHandler())
if need_auth:
v.initLongpoll()
main(v, args['args'])
v.sync()
sys.exit()
import fcntl
pid_file = accounts.getFile('inf.pid')
lock_file = accounts.getFile('inf.lock')
fp = open(lock_file, 'w')
try:
fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
print('Another instance is running, pid', open(pid_file).read())
sys.exit(1)
with open(pid_file, 'w') as f:
f.write(str(os.getpid()))
logging.info('Starting vkbot, pid ' + str(os.getpid()))