forked from aleeherasimiuk/DotHub-DiscordBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
100 lines (72 loc) · 2.88 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
from main.notifications.twitch import TwitchNotification, TwitchNotificationBuilder
from main.notifications.youtube import YoutubeNotification
from main.config import Config
from flask import Flask, config
from flask import request
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup
import json
import logging
import requests
app = Flask(__name__)
LOG_FILENAME = 'logs/errores.log'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
@app.route('/youtube_video', methods=['POST'])
def receive_youtube_notification():
app.logger.info("Received youtube notification.")
config = Config.from_file('res/notifications_config.json')
youtube_message = YoutubeNotification.from_xml(config, request.get_data())
app.logger.info("Sending webhook message for: {} - {}".format(youtube_message.channel_name, youtube_message.title))
youtube_message.send()
return "", 204
@app.route('/youtube_video', methods=['GET'])
def challenge():
challenge = request.args.get('hub.challenge')
app.logger.info("Challenge for youtube subscription received: {}".format(challenge))
if challenge:
return challenge
return '', 204
@app.route("/")
def hello():
return "Hola Dothub!"
@app.route('/twitch_stream', methods=['POST'])
def receive_twitch_notification():
_json = json.loads(request.get_data())
if 'challenge' in _json.keys():
challenge = _json['challenge']
app.logger.info("Challenge for youtube subscription received: {}".format(challenge))
return challenge
file = open('res/twitch_config.json')
_json = json.load(file)
file.close()
try:
config = Config.from_file('res/notifications_config.json')
twitch_notification = TwitchNotificationBuilder(**_json).build_twitch_notification(config)
twitch_notification.send()
except Exception as e:
app.logger.error("Can't send twitch notification: {}".format(e))
app.logger.info(
"Sending webhook message for: {} - {}".format(twitch_notification.user_name, twitch_notification.stream_title))
return '', 204
@app.route('/twitch_stream_test', methods=['POST'])
def receive_twitch_notification_test():
_json = json.loads(request.get_data())
data = _json['data'][0]
user_name = data['user_name']
user_login = data['user_login']
title = data['title']
app.logger.info("Received test twitch notification: {} - {}".format(user_name, title))
config = Config.from_file('res/notifications_config.json')
twitch_notification = Twitch(config, user_name, user_login, title)
twitch_notification.send()
return '', 204
@app.route('/logs')
def logs():
file = open(LOG_FILENAME, "r")
return file.read()
@app.route('/dotesting')
def dotesting_logs():
file = open("logs/dotesting.log", "r")
return "<br>".join(file.read().splitlines())
if __name__ == "__main__":
app.run(host="0.0.0.0")