-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrashAlertBot.py
123 lines (104 loc) · 3.81 KB
/
TrashAlertBot.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#######################################
########### Trash Alert Bot ###########
#######################################
# _._ #
# __.{,_.).__ #
# .-" "-. #
# .' __.........__ '. #
# /.-'`___.......___`'-.\ #
# /_.-'` / \ / \ `'-._\ #
# | | '/ \' | | #
# | '-' '-' | #
# ; ; #
# _\ ___ /_ #
# / '.'-.__ ___ __.-'.' \ #
# _/_ `'-..._____...-'` _\_ #
# / \ . / \ #
# \____) . (____/ #
# \___________.___________/ #
# \___________________/ #
# (_____________________) #
# #
# - Desenvolvido por Espaker Kaminski #
#######################################
import os
import sys
import logging
import signal
import threading
import time
import pprint
from logging.handlers import RotatingFileHandler
from flask import Flask, request, Response, send_file
from flask_cors import CORS
from Classes.Utils import Utils
from Classes.Skype import Skype
from Classes.Parser import Parser
from Classes.Mail import Mail
from Classes.RequestFormater import RequestFormatter
app_version = '1.0.1'
app = Flask(__name__)
CORS(app)
mail = None
skype = None
def monitoring():
print("Loop inicio")
time.sleep(5)
print("Loop fim")
def initiate():
log_main.info('Iniciando o TrashAlertBot versão: {}'.format(app_version))
signal.signal(signal.SIGTERM, finalize)
signal.signal(signal.SIGINT, finalize)
global mail, skype
try:
usr = conf.get('Skype', 'User', fallback='')
pwd = conf.get('Skype', 'Pass', fallback='')
skype = Skype(usr, pwd)
pprint(skype.get_contact())
except Exception as e:
log_main.exception('Erro ao inicializar conexão com o Skype: {}'.format(e))
try:
usr = conf.get('Mail', 'User', fallback='')
pwd = conf.get('Mail', 'Pass', fallback='')
host = conf.get('Mail', 'host', fallback='')
port = conf.getint('Mail', 'port', fallback='')
security = conf.get('Mail', 'security', fallback='')
mail = Mail(host, port, security, usr, pwd)
except Exception as e:
log_main.exception('Erro ao inicializar instanciar e-mail: {}'.format(e))
# x = threading.Thread(target=monitoring)
# x.start()
def finalize(signum, desc):
global execute, server
log_main.info('Recebi o sinal [{}] Desc [{}], finalizando...'.format(signum, desc))
log_main.warning('Limpando Cache Control ...')
cache_control.clear()
if server is not None:
log_main.warning('Parando Serviço ...')
server.stop()
if execute:
execute = False
else:
sys.exit(2)
if __name__ == '__main__':
execute = True
workdir = Utils.get_workdir()
conf = Parser(os.path.join(workdir, 'config.ini')).conf_get()
_level = conf.getint('Debug', 'Level', fallback=3)
debug_dir = os.path.join(workdir, 'debug')
log_file_path = os.path.join(debug_dir, 'TrashAlert.log')
if not os.path.exists(debug_dir):
os.mkdir(debug_dir, 0o775)
log_handler = RotatingFileHandler(log_file_path, maxBytes=1024 * 1024 * 10, backupCount=10)
log_handler.setLevel(logging.DEBUG)
log_handler.setFormatter(RequestFormatter(
'[%(asctime)s] | %(levelname)s | %(name)s | %(remote_addr)s | %(method)s | %(url)s | %(message)s'))
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
root_logger.addHandler(log_handler)
log_main = logging.getLogger('TrashAlert:' + str(os.getpid()))
app.logger.addHandler(log_handler)
app.debug = True
initiate()