-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
547 lines (446 loc) · 17.5 KB
/
main.py
File metadata and controls
547 lines (446 loc) · 17.5 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import json
from typing import TypeVar
import flask
from flask import Flask
from flask_socketio import SocketIO, emit
from cryptography.fernet import Fernet
from lrcup import LRCLib
import asyncio
import dotenv
import os
import threading
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import requests
import flask_cors
from twitchAPI.twitch import Twitch
from twitchAPI.type import AuthScope, ChatEvent
from twitchAPI.chat import Chat
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.helper import first
import unicodedata
dotenv.load_dotenv()
CONFIG = {}
try:
with open("config.json", "r", encoding="utf-8") as f:
CONFIG = json.load(f)
except FileNotFoundError:
CONFIG = {}
def cfg(key: str, default=None):
"""Return config value from config.json if present, otherwise from environment."""
val = CONFIG.get(key, None)
if val is None:
return os.getenv(key, default)
return val
blocklist_raw = cfg("BLOCKLIST_WORDS", "")
if isinstance(blocklist_raw, list):
BLOCKLIST_WORDS = [str(w).lower() for w in blocklist_raw]
else:
BLOCKLIST_WORDS = [w.strip().lower() for w in str(blocklist_raw).split(',') if w.strip()]
def normalize_text(text: str) -> str:
"""Normalize text for blocklist checking by removing special characters and spaces."""
text = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore').decode('ascii')
zero_width_and_spaces = '\u200B\u200C\u200D\u200E\u200F\uFEFF\u00A0 \t\n\r'
text = ''.join(c for c in text if c not in zero_width_and_spaces)
return text.lower()
username = cfg("LASTFM_USER") or ""
T = TypeVar("T")
def notnone(val: T | None) -> T:
assert val is not None
return val
TWITCH_API_ID = os.getenv("TWITCH_API_ID")
TWITCH_API_SECRET = os.getenv("TWITCH_API_SECRET")
assert TWITCH_API_ID, "TWITCH_API_ID environment variable not set"
assert TWITCH_API_SECRET, "TWITCH_API_SECRET environment variable not set"
TWITCH_OAUTH_TOKEN = None
TWITCH_USERNAME = cfg("TWITCH_USERNAME")
assert TWITCH_USERNAME, "TWITCH_USERNAME not set in env or config"
TWITCH_CHANNEL = cfg("TWITCH_CHANNEL")
assert TWITCH_CHANNEL, "TWITCH_CHANNEL not set in env or config"
BROADCASTER_ID = cfg("BROADCASTER_ID")
MODERATOR_ID = cfg("MODERATOR_ID")
TRACKERGG_API_KEY = os.getenv("TRACKERGG_API_KEY")
assert TRACKERGG_API_KEY, "TRACKERGG_API_KEY environment variable not set"
SPOT_API_KEY = os.getenv("SPOT_API_KEY")
SPOT_API_SECRET = os.getenv("SPOT_API_SECRET")
assert SPOT_API_KEY, "SPOT_API_KEY environment variable not set"
assert SPOT_API_SECRET, "SPOT_API_SECRET environment variable not set"
SPOT_CACHE = cfg("SPOT_CACHE") or f".cache-{username or 'spotify'}"
WIDGET_PORT = int(cfg("WIDGET_PORT", "5001") or 5001)
SPOT_REDIRECT_URI = f"http://127.0.0.1:{WIDGET_PORT}/callback"
spot_oauth = SpotifyOAuth(
client_id=SPOT_API_KEY,
client_secret=SPOT_API_SECRET,
redirect_uri=SPOT_REDIRECT_URI,
scope="user-read-currently-playing",
cache_path=SPOT_CACHE,
)
async def twitch_chat_listen():
try:
twitch = await Twitch(notnone(TWITCH_API_ID), notnone(TWITCH_API_SECRET))
USER_SCOPES = [
AuthScope.CHAT_READ,
AuthScope.CHAT_EDIT,
AuthScope.CHANNEL_MODERATE,
AuthScope.MODERATOR_MANAGE_CHAT_MESSAGES,
AuthScope.MODERATOR_READ_CHAT_MESSAGES,
AuthScope.MODERATOR_MANAGE_BLOCKED_TERMS,
AuthScope.MODERATOR_READ_BLOCKED_TERMS,
]
auth = UserAuthenticator(twitch, USER_SCOPES)
auth_result = await auth.authenticate()
assert auth_result is not None, "Authentication failed, no token received."
token, refresh_token = auth_result
await twitch.set_user_authentication(token, USER_SCOPES, refresh_token)
global BROADCASTER_ID, MODERATOR_ID
broadcaster_user = await first(twitch.get_users(logins=[notnone(TWITCH_CHANNEL)]))
if broadcaster_user:
BROADCASTER_ID = broadcaster_user.id
moderator_user = await first(twitch.get_users())
if moderator_user:
MODERATOR_ID = moderator_user.id
assert TWITCH_CHANNEL is not None, "TWITCH_CHANNEL environment variable not set"
chat = await Chat(twitch, initial_channel=[notnone(TWITCH_CHANNEL)])
async def handle_message(msg):
normalized_msg = normalize_text(msg.text)
blocked = any(normalize_text(word) in normalized_msg for word in BLOCKLIST_WORDS)
if blocked:
print(f"Blocked message from {msg.user.display_name}: {msg.text}")
if BROADCASTER_ID and MODERATOR_ID:
try:
await twitch.delete_chat_message(BROADCASTER_ID, MODERATOR_ID, msg.id)
print(f"Deleted message ID {msg.id}")
except Exception as e:
print(f"Failed to delete message: {e}")
else:
print("Broadcaster or Moderator ID not set, cannot delete message")
return
socketio.emit(
"chat_message",
{
"user": msg.user.display_name,
"channel": msg.room.name,
"message": msg.text,
},
)
chat_history.append({
"user": msg.user.display_name,
"channel": msg.room.name,
"message": msg.text,
})
if len(chat_history) > 100:
chat_history.pop(0)
chat.register_event(ChatEvent.MESSAGE, handle_message)
chat.start()
while True:
await asyncio.sleep(60)
except Exception as e:
print(f"twitchAPI chat failed or missing user auth: {e}")
import traceback
traceback.print_exc()
def get_spotify_client():
return spotipy.Spotify(auth_manager=spot_oauth)
def get_current_track_from_spotify():
try:
sp = get_spotify_client()
return sp.current_user_playing_track()
except Exception as e:
print(f"Error fetching Spotify track: {e}")
return None
app = Flask(__name__)
app.config["SECRET_KEY"] = Fernet.generate_key()
socketio = SocketIO(app, async_mode="threading", cors_allowed_origins="*")
flask_cors.CORS(app, resources={r"/*": {"origins": "*"}})
@app.route("/login")
def spotify_login():
auth_url = spot_oauth.get_authorize_url()
return flask.redirect(auth_url)
@app.route("/callback")
def spotify_callback():
code = flask.request.args.get("code")
error = flask.request.args.get("error")
if error:
return flask.Response(f"Spotify auth error: {error}", status=400)
if not code:
return flask.Response("No code provided", status=400)
try:
token_info = spot_oauth.get_access_token(code)
print("Spotify token_info acquired:", token_info)
except Exception as e:
return flask.Response(f"Error exchanging code for token: {e}", status=500)
return flask.Response(
"Spotify authorization complete. You can close this window.", status=200
)
@app.route("/music")
def home():
return flask.send_file("pages/music.htm")
@app.route("/rivals")
def rivals():
return flask.send_file("pages/rivals.htm")
@app.route("/wmark")
def watermark():
return flask.send_file("pages/wmark.htm")
@app.route("/chat")
def chat():
return flask.send_file("pages/chat.htm")
@app.route("/logo.svg")
def logo():
return flask.send_file("logo.svg")
last_title = ""
lrclib = LRCLib()
current_data = {}
current_lyrics = ""
subtext = cfg("SUBTEXT", "")
current_rivals_data = {}
chat_history = []
_d = cfg("DISABLE_LYRICS", False)
if isinstance(_d, str):
disable_lyrics = _d.lower() == "true"
else:
disable_lyrics = bool(_d)
@socketio.on("init_client")
def on_connect():
global current_data, current_lyrics, subtext, current_rivals_data, disable_lyrics, chat_history
emit("rivals_info", current_rivals_data)
emit("subtext_info", {"subtext": subtext})
emit("lyrics", {"disable": disable_lyrics})
emit("lyrics", {"disabled": disable_lyrics})
emit(
"wmark_slides",
{
"slides": notnone(cfg("WMARK_SLIDES"))
if cfg("WMARK_SLIDES")
else []
},
)
emit("chat_history", chat_history)
emit("channel_info", {"channel": TWITCH_CHANNEL})
async def fetch_lyrics(artist, title):
global lrclib, current_lyrics
results = lrclib.search(
track=title,
artist=artist,
)
if results and results[0] and results[0].syncedLyrics:
return results[0].syncedLyrics
return None
async def monitor_playback():
global last_title, current_data, current_lyrics
print("Starting playback monitor...")
while True:
try:
spotify_track = get_current_track_from_spotify()
if not spotify_track or not spotify_track.get("item"):
current_data = {
"artist_name": "",
"title": "",
"album_title": "",
"art_url": "",
"is_playing": False,
"progress": 0,
"duration": 0,
"start_time": 0,
}
current_lyrics = ""
socketio.emit("playback_info", current_data)
last_title = ""
await asyncio.sleep(15)
continue
is_playing = spotify_track.get("is_playing", False)
track = spotify_track.get("item", {})
title = track.get("name", "")
if title != last_title:
last_title = title
artists = track.get("artists", [])
artist_name = (
", ".join([artist.get("name", "") for artist in artists])
if artists
else ""
)
album = track.get("album", {})
album_title = album.get("name", "")
images = album.get("images", [])
art_url = images[0].get("url") if images else None
track_duration = track.get("duration_ms", 0) / 1000
if not disable_lyrics and artist_name and title:
lyrics = await fetch_lyrics(artist_name, title)
if lyrics:
socketio.emit("playback_lyrics", {"lyrics": lyrics})
current_lyrics = lyrics
else:
socketio.emit("playback_lyrics", {"lyrics": ""})
current_lyrics = ""
else:
socketio.emit("playback_lyrics", {"lyrics": ""})
current_lyrics = ""
progress_ms = spotify_track.get("progress_ms", 0)
timestamp_ms = spotify_track.get("timestamp", 0)
start_time = timestamp_ms / 1000
progress = (
(progress_ms / track.get("duration_ms", 1))
if track.get("duration_ms")
else 0
)
progress = min(max(progress, 0), 1)
print(
f"Track: {title} by {artist_name}, "
f"Duration: {track_duration}s, Progress: {progress_ms}ms ({progress * 100:.1f}%)"
)
current_data = {
"artist_name": artist_name,
"title": title,
"album_title": album_title,
"art_url": art_url,
"is_playing": is_playing,
"progress": progress,
"duration": track_duration,
"start_time": start_time,
}
socketio.emit("playback_info", current_data)
track_duration = track.get("duration_ms", 0) / 1000
progress_ms = spotify_track.get("progress_ms", 0)
progress = (
(progress_ms / track.get("duration_ms", 1))
if track.get("duration_ms")
else 0
)
progress = min(max(progress, 0), 1)
if (
current_data.get("progress") != progress
or current_data.get("is_playing") != is_playing
):
timestamp_ms = spotify_track.get("timestamp", 0)
start_time = timestamp_ms / 1000
current_data.update(
{
"progress": progress,
"is_playing": is_playing,
"start_time": start_time,
}
)
socketio.emit("playback_info", current_data)
await asyncio.sleep(2)
except Exception as e:
print(f"Error in monitor_playback: {e}")
await asyncio.sleep(5)
async def monitor_rivals():
global TRACKERGG_API_KEY, current_rivals_data, notnone
print("Starting rivals monitor...")
while True:
try:
rivals_user = notnone(cfg("RIVALS_USER"))
url = f"https://api.tracker.gg/api/v2/marvel-rivals/standard/profile/ign/{rivals_user}?"
flaresolverr_url = notnone(cfg("FLARESOLVERR_URL"))
user = requests.post(
flaresolverr_url,
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
json={
"cmd": "request.get",
"url": url,
"maxTimeout": 15000,
},
)
if user.status_code != 200:
print(f"FlareSolverr request failed with status {user.status_code}")
await asyncio.sleep(10)
continue
response_json = user.json()
solution = response_json.get("solution", {})
response_text = solution.get("response", "")
if "<pre>" not in response_text or "</pre>" not in response_text:
print("No <pre> tags found in response")
await asyncio.sleep(10)
continue
text = response_text.split("<pre>")[1].split("</pre>")[0]
user_data = json.loads(text).get("data", {})
data = {
"metadata": user_data.get("metadata", {}),
"platform": user_data.get("platformInfo", {}),
"user": user_data.get("userInfo", {}),
"heroStats": list(
filter(
lambda x: x.get("type") == "hero", user_data.get("segments", [])
)
),
"roleStats": list(
filter(
lambda x: x.get("type") == "hero-role",
user_data.get("segments", []),
)
),
}
current_rivals_data = data
socketio.emit("rivals_info", data)
await asyncio.sleep(15 * 60)
except Exception as e:
print(f"Error in monitor_rivals: {e}")
import traceback
traceback.print_exc()
await asyncio.sleep(10)
@app.get("/img_proxy")
def img_proxy():
url = flask.request.args.get("url", "")
if not url:
return flask.Response("No URL provided", status=400)
try:
resp = requests.get(url)
excluded_headers = [
"content-encoding",
"content-length",
"transfer-encoding",
"connection",
]
headers = [
(name, value)
for (name, value) in resp.raw.headers.items()
if name.lower() not in excluded_headers
]
response = flask.Response(resp.content, resp.status_code, headers)
return response
except Exception as e:
return flask.Response(f"Error fetching image: {e}", status=500)
@app.get("/static/<path:filename>")
def static_files(filename):
if ".." in filename or filename.startswith("/"):
return flask.Response("Invalid filename", status=400)
return flask.send_file(os.path.join("static", filename))
if __name__ == "__main__":
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.create_task(monitor_playback())
loop.create_task(monitor_rivals())
loop.create_task(twitch_chat_listen())
loop_thread = threading.Thread(target=loop.run_forever, name="asyncio_loop_thread")
loop_thread.start()
try:
socketio.run(app, port=int(os.getenv("WIDGET_PORT", 5001)), host="127.0.0.1")
except KeyboardInterrupt:
print("KeyboardInterrupt received, shutting down...")
finally:
def schedule_cancel_and_stop():
async def _cancel_tasks_and_stop():
try:
tasks = [t for t in asyncio.all_tasks(loop) if not t.done()]
for t in tasks:
t.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
except Exception as e:
print("Error cancelling tasks:", e)
finally:
try:
loop.stop()
except Exception:
pass
try:
loop.call_soon_threadsafe(
asyncio.ensure_future, _cancel_tasks_and_stop()
)
except Exception as e:
print("Failed to schedule cancellation on event loop:", e)
schedule_cancel_and_stop()
loop_thread.join(timeout=5)
print("Shutdown complete. Exiting.")
exit(0)