-
Notifications
You must be signed in to change notification settings - Fork 8
/
webhook_listener.py
executable file
·220 lines (182 loc) · 8.89 KB
/
webhook_listener.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from flask import Flask, render_template, request, redirect, url_for, jsonify
import subprocess
import os
import logging
import json
import sonarr_utils
from datetime import datetime
from dotenv import load_dotenv
app = Flask(__name__)
# Load environment variables from .env file
load_dotenv()
# Load environment variables
SONARR_URL = os.getenv('SONARR_URL')
SONARR_API_KEY = os.getenv('SONARR_API_KEY')
MISSING_LOG_PATH = os.getenv('MISSING_LOG_PATH', '/app/logs/missing.log')
# Setup logging to capture all logs
logging.basicConfig(filename=os.getenv('LOG_PATH', '/app/logs/app.log'),
level=logging.DEBUG if os.getenv('FLASK_DEBUG', 'false').lower() == 'true' else logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
# Adding stream handler to also log to console for Docker logs to capture
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG if os.getenv('FLASK_DEBUG', 'false').lower() == 'true' else logging.INFO)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')
stream_handler.setFormatter(formatter)
app.logger.addHandler(stream_handler)
# Configuration management
config_path = os.path.join(app.root_path, 'config', 'config.json')
def load_config():
config_path = os.getenv('CONFIG_PATH', '/app/config/config.json')
try:
with open(config_path, 'r') as file:
config = json.load(file)
if 'rules' not in config:
config['rules'] = {}
# Only add default rule if it doesn't exist in the config
if 'default_rule' not in config:
config['default_rule'] = 'default'
if 'default' not in config['rules']:
config['rules']['default'] = {
'get_option': 'season',
'action_option': 'monitor',
'keep_watched': 'season',
'monitor_watched': True,
'series': []
}
return config
except FileNotFoundError:
# Default config structure for first-time users or missing config
return {
'rules': {
'default': {
'get_option': 'season',
'action_option': 'monitor',
'keep_watched': 'season',
'monitor_watched': True,
'series': []
}
},
'default_rule': 'default'
}
def save_config(config):
config_path = os.getenv('CONFIG_PATH', '/app/config/config.json')
with open(config_path, 'w') as file:
json.dump(config, file, indent=4)
def normalize_name(name):
return ' '.join(word.capitalize() for word in name.replace('_', ' ').split())
def get_missing_log_content():
try:
with open(MISSING_LOG_PATH, 'r') as file:
return file.read()
except FileNotFoundError:
return "No missing entries logged."
except Exception as e:
app.logger.error(f"Failed to read missing log: {str(e)}")
return "Failed to read log."
@app.route('/')
def home():
config = load_config()
preferences = sonarr_utils.load_preferences()
current_series = sonarr_utils.fetch_series_and_episodes(preferences)
upcoming_premieres = sonarr_utils.fetch_upcoming_premieres(preferences)
all_series = sonarr_utils.get_series_list(preferences) # Ensure this function fetches series info
# Build a dictionary that maps series IDs to their assigned rules
rules_mapping = {str(series_id): rule_name for rule_name, details in config['rules'].items() for series_id in details.get('series', [])}
# Annotate each series with its assigned rule or default rule
default_rule = config.get('default_rule', 'default')
for series in all_series:
series['assigned_rule'] = rules_mapping.get(str(series['id']), default_rule)
missing_log_content = get_missing_log_content() # Fetch the missing log content here
rule = request.args.get('rule', 'full_seasons') # Get the rule parameter from the request
return render_template('index.html', config=config, current_series=current_series,
upcoming_premieres=upcoming_premieres, all_series=all_series,
sonarr_url=SONARR_URL, missing_log=missing_log_content, rule=rule)
@app.route('/update-settings', methods=['POST'])
def update_settings():
config = load_config()
rule_name = request.form.get('rule_name')
if rule_name == 'add_new':
rule_name = request.form.get('new_rule_name')
if not rule_name:
return redirect(url_for('home', section='settings', message="New rule name is required."))
get_option = request.form.get('get_option')
keep_watched = request.form.get('keep_watched')
config['rules'][rule_name] = {
'get_option': get_option,
'action_option': request.form.get('action_option'),
'keep_watched': keep_watched,
'monitor_watched': request.form.get('monitor_watched', 'false').lower() == 'true',
'series': config['rules'].get(rule_name, {}).get('series', [])
}
# Set this rule as default if the checkbox is checked
if request.form.get('default_rule'):
config['default_rule'] = rule_name
save_config(config)
return redirect(url_for('home', section='settings', message="Settings updated successfully"))
@app.route('/delete_rule', methods=['POST'])
def delete_rule():
config = load_config()
rule_name = request.form.get('rule_name')
if rule_name and rule_name in config['rules']:
del config['rules'][rule_name]
save_config(config)
return redirect(url_for('home', section='settings', message=f"Rule '{rule_name}' deleted successfully."))
else:
return redirect(url_for('home', section='settings', message=f"Rule '{rule_name}' not found."))
@app.route('/assign_rules', methods=['POST'])
def assign_rules():
config = load_config()
rule_name = request.form.get('assign_rule_name')
submitted_series_ids = set(request.form.getlist('series_ids'))
if not rule_name or rule_name == 'remove':
# If no rule is selected or it's explicitly marked to remove rules, unassign series from all rules
for key, details in config['rules'].items():
details['series'] = [sid for sid in details.get('series', []) if sid not in submitted_series_ids]
else:
# Update the rule's series list to include only those submitted
if rule_name in config['rules']:
current_series = set(config['rules'][rule_name]['series'])
updated_series = current_series.union(submitted_series_ids)
config['rules'][rule_name]['series'] = list(updated_series)
# Remove the series from any other rules
for key, details in config['rules'].items():
if key != rule_name:
# Remove series IDs from other rules
details['series'] = [sid for sid in details.get('series', []) if sid not in submitted_series_ids]
save_config(config)
return redirect(url_for('home', section='settings', message="Rules updated successfully."))
@app.route('/unassign_rules', methods=['POST'])
def unassign_rules():
config = load_config()
rule_name = request.form.get('assign_rule_name')
submitted_series_ids = set(request.form.getlist('series_ids'))
# Update the rule's series list to exclude those submitted
if rule_name in config['rules']:
current_series = set(config['rules'][rule_name]['series'])
updated_series = current_series.difference(submitted_series_ids)
config['rules'][rule_name]['series'] = list(updated_series)
save_config(config)
return redirect(url_for('home', section='settings', message="Rules updated successfully."))
@app.route('/webhook', methods=['POST'])
def handle_server_webhook():
app.logger.info("Received POST request from Tautulli")
data = request.json
if data:
app.logger.info(f"Webhook received with data: {data}")
try:
temp_dir = '/app/temp'
os.makedirs(temp_dir, exist_ok=True)
with open(os.path.join(temp_dir, 'data_from_tautulli.json'), 'w') as f:
json.dump(data, f)
app.logger.info("Data successfully written to data_from_tautulli.json")
result = subprocess.run(["python3", "/app/servertosonarr.py"], capture_output=True, text=True)
if result.stderr:
app.logger.error("Errors from servertosonarr.py: " + result.stderr)
except Exception as e:
app.logger.error(f"Failed to handle data or run script: {e}")
return jsonify({'status': 'error', 'message': str(e)}), 500
return jsonify({'status': 'success', 'message': 'Script triggered successfully'}), 200
else:
return jsonify({'status': 'error', 'message': 'No data received'}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=os.getenv('FLASK_DEBUG', 'false').lower() == 'true')