-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
108 lines (80 loc) · 3.25 KB
/
server.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
from models.notifications.twitch import TwitchNotificationBuilder
from models.notifications.youtube import YoutubeNotification
from models.config import Config
from models.logger import setup_logger
from flask import Flask
from flask import request
from flask import render_template
import json
import logging
import traceback
app = Flask(__name__)
SERVER_LOG_FILENAME = 'logs/errores.log'
DOTESTING_LOG_FILENAME = "logs/dotesting.log"
INFOBOT_LOG_FILENAME = "logs/infobot.log"
MODERADOT_LOG_FILENAME = "logs/moderadot.log"
logger = setup_logger(app.logger, SERVER_LOG_FILENAME, logging.INFO)
last_sent_youtube_notification = []
last_sent_twitch_notification = ""
@app.route('/youtube_video', methods=['POST'])
def receive_youtube_notification():
global last_sent_youtube_notification
logger.info("Received youtube notification.")
logger.debug("Received youtube payload: {}".format(request.get_data()))
config = Config.from_file('res/notifications_config.json')
youtube_message = YoutubeNotification.from_xml(config, request.get_data())
if youtube_message.id not in last_sent_youtube_notification:
logger.info("Sending webhook message for: {} - {}".format(youtube_message.channel_name, youtube_message.title))
youtube_message.send()
last_sent_youtube_notification.append(youtube_message.id)
return "", 204
@app.route('/youtube_video', methods=['GET'])
def challenge():
challenge = request.args.get('hub.challenge')
logger.info("Challenge for youtube subscription received")
if challenge:
return challenge
return '', 204
@app.route("/")
def hello():
return "Hola Dothub!"
@app.route('/twitch_stream', methods=['POST'])
def receive_twitch_notification():
global last_sent_twitch_notification
_json = json.loads(request.get_data())
logger.debug("Received twitch payload: {}".format(request.get_data()))
if 'challenge' in _json.keys():
challenge = _json['challenge']
logger.info("Challenge for youtube subscription received")
return challenge
try:
config = Config.from_file('res/notifications_config.json')
twitch_notification = TwitchNotificationBuilder(**_json).build_twitch_notification(config)
if twitch_notification.stream_id not in last_sent_twitch_notification:
twitch_notification.send()
last_sent_twitch_notification = twitch_notification.stream_id
logger.info("Sending webhook message for: {}".format(twitch_notification.user_name))
else:
logger.info("Duplicated twitch notification")
except Exception as e:
logger.error("Can't send twitch notification: {}".format(e))
traceback.print_exc()
return '', 204
@app.route('/logs')
def logs():
return show_logs(SERVER_LOG_FILENAME)
@app.route('/dotesting')
def dotesting_logs():
return show_logs(DOTESTING_LOG_FILENAME)
@app.route('/infobot')
def infobot_logs():
return show_logs(INFOBOT_LOG_FILENAME)
@app.route('/moderadot')
def moderadot_logs():
return show_logs(MODERADOT_LOG_FILENAME)
def show_logs(filename):
with open(filename, "r") as file:
lines = file.read().splitlines()
return render_template('logs.html', lines=lines)
if __name__ == "__main__":
app.run(host="0.0.0.0")