-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
109 lines (95 loc) · 3.22 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
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
103
104
105
106
107
108
109
import argparse
import logging.handlers
import os
import sys
from time import sleep
from flask import jsonify
from routes import app
import routes_admin
import routes_insta
MAIN_DIR = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(MAIN_DIR, 'irequests/'))
sys.path.insert(0, os.path.join(MAIN_DIR, 'settings/'))
sys.path.insert(0, os.path.join(MAIN_DIR, 'vk/'))
sys.path.insert(0, os.path.join(MAIN_DIR, 'inst/'))
sys.path.insert(0, os.path.join(MAIN_DIR, 'analysis/'))
from settings import settings
from spider.spider import Spider
IS_DEVELOPMENT = os.environ.get('ENVIRONMENT') != 'production'
def cli_args_parser_init():
"""
Осуществляет инициализацию парсера аргументов коммандной строки
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-s",
"--settings",
action="store",
dest="SETTING_FILE_NAME",
help="File with settigs",
type=str)
parser.add_argument(
"-l",
"--log",
action="store",
dest="LOG_FILE_NAME",
help="File with logs",
type=str)
return parser
def log_init():
# logging.disable(logging.DEBUG)
# logging.basicConfig(level=logging.INFO)
if IS_DEVELOPMENT:
settings.log_level = logging.DEBUG
root_logger = logging.getLogger()
root_logger.setLevel(level=settings.log_level)
console = logging.StreamHandler(sys.stdout)
console.setLevel(level=settings.log_level)
console.setFormatter(logging.Formatter(settings.log_format))
handler = logging.handlers.RotatingFileHandler(
settings.log_file,
maxBytes=settings.log_max_bytes_in_file,
encoding=settings.encoding)
handler.setFormatter(logging.Formatter(settings.log_format))
root_logger.addHandler(handler)
root_logger.addHandler(console)
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("irequests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
# adjusted flask_logger
def flask_logger(bot_process):
global running
# Create empty job.log, old logging will be deleted
open("app/job.log", 'w').close()
"""creates logging information"""
with open("app/job.log") as log_info:
while bot_process.is_alive():
data = log_info.read()
yield data.encode()
sleep(1)
bot_process.join()
running = False
def init_app():
# parse cli arguments
parser = cli_args_parser_init()
cli_args = parser.parse_args()
# init settings
if cli_args.SETTING_FILE_NAME:
settings.load_JSON(cli_args.SETTING_FILE_NAME)
# init log
if cli_args.LOG_FILE_NAME:
settings.log_file = cli_args.LOG_FILE_NAME
log_init()
if os.environ.get("WERKZEUG_RUN_MAIN") == "true" or not IS_DEVELOPMENT:
init_app()
spider = Spider(settings, working_hours=(6, 24))
spider.start()
app.spider = spider
@app.route("/bot", methods=["GET"])
def stream():
"""run bot"""
global spider
spider.trigger_task()
return jsonify({"message": "Bot started successfully."}), 200
if __name__ == '__main__':
app.run(host="0.0.0.0", port=50000, threaded=True, debug=IS_DEVELOPMENT)