2024-03-06 18:33:04 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import bare, pylast
|
|
|
|
import config as conf
|
|
|
|
import random as r
|
|
|
|
from logs import log
|
|
|
|
from typing import Any, Callable, NoReturn
|
|
|
|
from threading import Thread
|
|
|
|
from time import sleep
|
|
|
|
from traceback import format_exc
|
|
|
|
|
2024-04-07 05:38:49 +00:00
|
|
|
|
2024-03-06 18:33:04 +00:00
|
|
|
def is_dead(thr: Thread) -> bool:
|
|
|
|
thr.join(timeout=0)
|
|
|
|
return not thr.is_alive()
|
|
|
|
|
|
|
|
|
|
|
|
def threadWrapper(data: dict) -> NoReturn:
|
|
|
|
if not data["noWrap"]:
|
|
|
|
while 1:
|
2024-05-01 00:09:50 +00:00
|
|
|
if data["ignoreErrors"]:
|
2024-03-06 18:33:04 +00:00
|
|
|
try:
|
|
|
|
data["func"](*data["args"])
|
|
|
|
except Exception:
|
|
|
|
Err = format_exc()
|
|
|
|
for line in Err.split("\n"):
|
|
|
|
log(line, "Thread", "WARN")
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
data["func"](*data["args"])
|
|
|
|
except Exception:
|
|
|
|
Err = format_exc()
|
|
|
|
for line in Err.split("\n"):
|
|
|
|
log(line, "Thread", "CRASH")
|
|
|
|
exit(1)
|
|
|
|
sleep(data["interval"])
|
|
|
|
log("Threaded loop broken", "Thread", "FATAL")
|
|
|
|
else:
|
|
|
|
data["func"](*data["args"])
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def startThread(data: dict) -> Thread:
|
|
|
|
t = Thread(target=threadWrapper, args=(data,))
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
2024-04-07 05:38:49 +00:00
|
|
|
def threadManager(
|
|
|
|
threads: dict[str, dict[str, Any]],
|
|
|
|
output: bool = False,
|
|
|
|
mgr: str = "TManager",
|
|
|
|
interval: int = 60,
|
|
|
|
) -> NoReturn:
|
2024-03-06 18:33:04 +00:00
|
|
|
if output:
|
|
|
|
log("Begin init of thread manager", mgr)
|
|
|
|
running = {}
|
|
|
|
for name in threads:
|
|
|
|
data = threads[name]
|
|
|
|
running[name] = startThread(data)
|
|
|
|
if output:
|
|
|
|
log("All threads running, starting checkup loop", mgr)
|
|
|
|
while 1:
|
|
|
|
sleep(interval)
|
|
|
|
if output:
|
|
|
|
log("Checking threads", mgr)
|
|
|
|
for name in running:
|
|
|
|
t = running[name]
|
|
|
|
if is_dead(t):
|
|
|
|
if output:
|
|
|
|
log(f"Thread {name} has died, restarting", mgr, "WARN")
|
|
|
|
data = threads[name]
|
|
|
|
running[name] = startThread(data)
|
|
|
|
log("Thread manager loop broken", mgr, "FATAL")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def radio(instance: bare.bot) -> NoReturn:
|
|
|
|
lastTrack = ""
|
2024-04-09 17:46:43 +00:00
|
|
|
complained = False
|
2024-04-12 02:43:04 +00:00
|
|
|
firstMiss = False
|
2024-05-06 14:36:31 +00:00
|
|
|
misses = 0
|
|
|
|
missChunk = 0
|
|
|
|
missCap = -5
|
|
|
|
perChunk = 10
|
2024-05-10 01:01:05 +00:00
|
|
|
debug = instance.server == "replirc"
|
2024-03-06 18:33:04 +00:00
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
newTrack = instance.lastfmLink.get_user("Firepup650").get_now_playing()
|
|
|
|
if newTrack:
|
2024-05-06 14:45:02 +00:00
|
|
|
if complained:
|
|
|
|
complained = False
|
2024-05-06 14:36:31 +00:00
|
|
|
misses = 0
|
|
|
|
missChunk = 0
|
|
|
|
elif misses > missCap:
|
2024-05-06 14:36:40 +00:00
|
|
|
missChunk += 1
|
2024-05-06 14:36:31 +00:00
|
|
|
if missChunk >= perChunk:
|
|
|
|
misses -= 1
|
|
|
|
missChunk = 0
|
2024-05-06 14:45:02 +00:00
|
|
|
thisTrack = newTrack.__str__()
|
|
|
|
if thisTrack != lastTrack:
|
|
|
|
lastTrack = thisTrack
|
|
|
|
instance.msg("f.sp " + thisTrack, "#fp-radio")
|
|
|
|
instance.sendraw(
|
|
|
|
f"TOPIC #fp-radio :Firepup radio ({thisTrack}) - https://open.spotify.com/playlist/4ctNy3O0rOwhhXIKyLvUZM"
|
|
|
|
)
|
2024-04-09 17:46:43 +00:00
|
|
|
elif not complained:
|
2024-05-06 14:47:30 +00:00
|
|
|
missChunk = 0
|
2024-05-06 14:36:31 +00:00
|
|
|
if misses < 0:
|
|
|
|
misses += 1
|
2024-05-10 01:01:05 +00:00
|
|
|
if debug:
|
|
|
|
instance.msg(str({"misses": misses, "missChunk": missChunk, "misses exceed or meet limit": misses <= missCap}), "#fp-radio-debug")
|
|
|
|
sleep(2)
|
2024-04-12 02:43:04 +00:00
|
|
|
continue
|
2024-04-12 02:45:40 +00:00
|
|
|
instance.msg(
|
|
|
|
"Firepup seems to have stopped the music by mistake :/", "#fp-radio"
|
|
|
|
)
|
|
|
|
instance.sendraw(
|
|
|
|
"TOPIC #fp-radio :Firepup radio (Offline) - https://open.spotify.com/playlist/4ctNy3O0rOwhhXIKyLvUZM"
|
|
|
|
)
|
2024-04-09 17:46:43 +00:00
|
|
|
complained = True
|
2024-04-12 02:43:04 +00:00
|
|
|
lastTrack = "null"
|
2024-03-06 18:33:04 +00:00
|
|
|
except Exception:
|
|
|
|
Err = format_exc()
|
|
|
|
for line in Err.split("\n"):
|
|
|
|
instance.log(line, "WARN")
|
2024-05-10 01:01:05 +00:00
|
|
|
if debug:
|
|
|
|
instance.msg(str({"misses": misses, "missChunk": missChunk, "misses exceed or meet limit": misses <= missCap}), "#fp-radio-debug")
|
2024-03-06 18:33:04 +00:00
|
|
|
sleep(2)
|
|
|
|
instance.log("Thread while loop broken", "FATAL")
|
|
|
|
exit(1)
|
|
|
|
|
2024-05-01 20:41:32 +00:00
|
|
|
|
2024-03-06 18:33:04 +00:00
|
|
|
data: dict[str, dict[str, Any]] = {
|
2024-05-01 01:45:53 +00:00
|
|
|
"radio": {"noWrap": True, "func": radio, "passInstance": True}
|
2024-03-06 18:33:04 +00:00
|
|
|
}
|