You've already forked RadioPlayer
mirror of
https://github.com/radio95-rnt/RadioPlayer.git
synced 2026-02-26 21:53:54 +01:00
diffrent quit method
This commit is contained in:
@@ -3,7 +3,7 @@ import os, socket
|
|||||||
import random
|
import random
|
||||||
import subprocess
|
import subprocess
|
||||||
import time, datetime
|
import time, datetime
|
||||||
import sys
|
import sys, signal
|
||||||
import threading
|
import threading
|
||||||
import re, unidecode
|
import re, unidecode
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
@@ -31,18 +31,15 @@ udp_host = ("127.0.0.1", 5000)
|
|||||||
logger = log95.log95("radioPlayer")
|
logger = log95.log95("radioPlayer")
|
||||||
|
|
||||||
exit_pending = False
|
exit_pending = False
|
||||||
reload_pending = False
|
intr_time = 0
|
||||||
|
|
||||||
class Time:
|
class Time:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_day_hour():
|
def get_day_hour(): return datetime.now().strftime('%A').lower(), datetime.now().hour
|
||||||
return datetime.now().strftime('%A').lower(), datetime.now().hour
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_playlist_modification_time(playlist_path):
|
def get_playlist_modification_time(playlist_path):
|
||||||
try:
|
try: return os.path.getmtime(playlist_path)
|
||||||
return os.path.getmtime(playlist_path)
|
except OSError: return 0
|
||||||
except OSError:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Process:
|
class Process:
|
||||||
@@ -56,14 +53,8 @@ class ProcessManager:
|
|||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
self.processes: list[Process] = []
|
self.processes: list[Process] = []
|
||||||
def _get_audio_duration(self, file_path):
|
def _get_audio_duration(self, file_path):
|
||||||
try:
|
result = subprocess.run(['ffprobe', '-v', 'quiet', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', file_path], capture_output=True, text=True)
|
||||||
result = subprocess.run([
|
if result.returncode == 0: return float(result.stdout.strip())
|
||||||
'ffprobe', '-v', 'quiet', '-show_entries', 'format=duration',
|
|
||||||
'-of', 'default=noprint_wrappers=1:nokey=1', file_path
|
|
||||||
], capture_output=True, text=True)
|
|
||||||
|
|
||||||
if result.returncode == 0: return float(result.stdout.strip())
|
|
||||||
except Exception as e: logger.warning(f"Exception while reading audio duration: {e}")
|
|
||||||
return None
|
return None
|
||||||
def play(self, track_path: str, fade_in: bool=False, fade_out: bool=False) -> Process:
|
def play(self, track_path: str, fade_in: bool=False, fade_out: bool=False) -> Process:
|
||||||
cmd = ['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet']
|
cmd = ['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet']
|
||||||
@@ -105,7 +96,21 @@ class ProcessManager:
|
|||||||
|
|
||||||
procman = ProcessManager()
|
procman = ProcessManager()
|
||||||
|
|
||||||
def load_dict_from_custom_format(file_path: str) -> dict:
|
def handle_sigint(signum, frame):
|
||||||
|
global exit_pending, intr_time
|
||||||
|
logger.info("Received SIGINT")
|
||||||
|
if (time.time() - intr_time) > 10:
|
||||||
|
intr_time = time.time()
|
||||||
|
logger.info("Will quit on song end.")
|
||||||
|
exit_pending = True
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
logger.warning("Force-Quit pending")
|
||||||
|
procman.stop_all(None)
|
||||||
|
|
||||||
|
signal.signal(signal.SIGINT, handle_sigint)
|
||||||
|
|
||||||
|
def load_dict_from_custom_format(file_path: str) -> dict[str, str]:
|
||||||
try:
|
try:
|
||||||
result_dict = {}
|
result_dict = {}
|
||||||
with open(file_path, 'r') as file:
|
with open(file_path, 'r') as file:
|
||||||
@@ -120,17 +125,19 @@ def load_dict_from_custom_format(file_path: str) -> dict:
|
|||||||
|
|
||||||
def update_rds(track_name: str):
|
def update_rds(track_name: str):
|
||||||
try:
|
try:
|
||||||
name_table: dict[str, str] = load_dict_from_custom_format(name_table_path)
|
name_table = load_dict_from_custom_format(name_table_path)
|
||||||
try:
|
try:
|
||||||
name = name_table[track_name]
|
name = name_table[track_name]
|
||||||
has_name = True
|
has_name = True
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
has_name = False
|
has_name = False
|
||||||
name = ".".join(track_name.split(".")[:-1])
|
name = track_name.rsplit(".", 1)[0]
|
||||||
|
|
||||||
|
name = re.sub(r'^\s*\d+\s*[-.]?\s*', '', name) # get rid of a
|
||||||
|
|
||||||
if " - " in name:
|
if " - " in name:
|
||||||
count = name.count(" - ")
|
count = name.count(" - ")
|
||||||
while count != 1: # yotutube reuploads, to avoid things like ilikedick123 - Micheal Jackson - Smooth Criminal
|
while count != 1: # youtube reuploads, to avoid things like ilikedick123 - Micheal Jackson - Smooth Criminal
|
||||||
name = name.split(" - ", 1)[1]
|
name = name.split(" - ", 1)[1]
|
||||||
count = name.count(" - ")
|
count = name.count(" - ")
|
||||||
artist = name.split(" - ", 1)[0]
|
artist = name.split(" - ", 1)[0]
|
||||||
@@ -142,7 +149,7 @@ def update_rds(track_name: str):
|
|||||||
|
|
||||||
title = unidecode.unidecode(title)
|
title = unidecode.unidecode(title)
|
||||||
artist = unidecode.unidecode(artist)
|
artist = unidecode.unidecode(artist)
|
||||||
|
|
||||||
title = re.sub(r'\s*[\(\[][^\(\)\[\]]*[\)\]]', '', title) # there might be junk
|
title = re.sub(r'\s*[\(\[][^\(\)\[\]]*[\)\]]', '', title) # there might be junk
|
||||||
|
|
||||||
prt = rds_base.format(artist, title)
|
prt = rds_base.format(artist, title)
|
||||||
@@ -158,9 +165,8 @@ def update_rds(track_name: str):
|
|||||||
rtp.append(1) # type 2
|
rtp.append(1) # type 2
|
||||||
rtp.append(prt.find(title)) # start 2
|
rtp.append(prt.find(title)) # start 2
|
||||||
rtp.append(len(title)) # len 2
|
rtp.append(len(title)) # len 2
|
||||||
rtp = list(map(str, rtp))
|
|
||||||
|
|
||||||
f.sendto(f"RTP={','.join(rtp)}\r\n".encode(), udp_host)
|
f.sendto(f"RTP={','.join(list(map(str, rtp)))}\r\n".encode(), udp_host)
|
||||||
f.close()
|
f.close()
|
||||||
except Exception as e: logger.error(f"Error updating RDS: {e}")
|
except Exception as e: logger.error(f"Error updating RDS: {e}")
|
||||||
|
|
||||||
@@ -189,11 +195,12 @@ def get_newest_track(tracks):
|
|||||||
return newest_track
|
return newest_track
|
||||||
|
|
||||||
def check_control_files():
|
def check_control_files():
|
||||||
|
global exit_pending
|
||||||
"""Check for control files and return action to take"""
|
"""Check for control files and return action to take"""
|
||||||
if can_delete_file("/tmp/radioPlayer_quit"):
|
if exit_pending:
|
||||||
os.remove("/tmp/radioPlayer_quit")
|
exit_pending = False
|
||||||
return "quit"
|
return "quit"
|
||||||
|
|
||||||
if can_delete_file("/tmp/radioPlayer_reload"):
|
if can_delete_file("/tmp/radioPlayer_reload"):
|
||||||
os.remove("/tmp/radioPlayer_reload")
|
os.remove("/tmp/radioPlayer_reload")
|
||||||
return "reload"
|
return "reload"
|
||||||
@@ -298,7 +305,6 @@ def parse_arguments():
|
|||||||
if arg.lower() == "-h":
|
if arg.lower() == "-h":
|
||||||
print("Control files:")
|
print("Control files:")
|
||||||
print(" Note: All of these files are one-time only, after they have been acked by the player they will be deleted")
|
print(" Note: All of these files are one-time only, after they have been acked by the player they will be deleted")
|
||||||
print(" /tmp/radioPlayer_quit - Quit the player")
|
|
||||||
print(" /tmp/radioPlayer_reload - Reload arguments from /tmp/radioPlayer_arg")
|
print(" /tmp/radioPlayer_reload - Reload arguments from /tmp/radioPlayer_arg")
|
||||||
print(" /tmp/radioPlayer_arg - Contains arguments to use")
|
print(" /tmp/radioPlayer_arg - Contains arguments to use")
|
||||||
print()
|
print()
|
||||||
|
|||||||
Reference in New Issue
Block a user