You've already forked RadioPlayer
mirror of
https://github.com/radio95-rnt/RadioPlayer.git
synced 2026-02-27 14:13:55 +01:00
this
This commit is contained in:
326
radioPlayer.py
326
radioPlayer.py
@@ -22,200 +22,212 @@ rds_path = "/home/user/RDS"
|
|||||||
rds_default_rtp_data = "4,7,7,1,17"
|
rds_default_rtp_data = "4,7,7,1,17"
|
||||||
|
|
||||||
def get_current_hour():
|
def get_current_hour():
|
||||||
return datetime.now().hour
|
return datetime.now().hour
|
||||||
|
|
||||||
def get_current_day():
|
def get_current_day():
|
||||||
return datetime.now().strftime('%A').lower()
|
return datetime.now().strftime('%A').lower()
|
||||||
|
|
||||||
def load_dict_from_custom_format(file_path: str) -> dict:
|
def load_dict_from_custom_format(file_path: str) -> dict:
|
||||||
result_dict = {}
|
result_dict = {}
|
||||||
with open(file_path, 'r') as file:
|
with open(file_path, 'r') as file:
|
||||||
for line in file:
|
for line in file:
|
||||||
if line.strip() == "":
|
if line.strip() == "":
|
||||||
continue
|
continue
|
||||||
key, value = line.split(':', 1)
|
key, value = line.split(':', 1)
|
||||||
result_dict[key.strip()] = value.strip()
|
result_dict[key.strip()] = value.strip()
|
||||||
return result_dict
|
return result_dict
|
||||||
|
|
||||||
def update_rds(track_name):
|
def update_rds(track_name):
|
||||||
try:
|
try:
|
||||||
name_table = load_dict_from_custom_format(name_table_dir)
|
name_table = load_dict_from_custom_format(name_table_dir)
|
||||||
try:
|
try:
|
||||||
prt = rds_base.format(name_table[track_name])
|
prt = rds_base.format(name_table[track_name])
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
print("Unknown", e)
|
print("Unknown", e)
|
||||||
prt = rds_base.format(rds_default_name)
|
prt = rds_base.format(rds_default_name)
|
||||||
|
|
||||||
f = open(rds_path, "w")
|
f = open(rds_path, "w")
|
||||||
f.write(f"TEXT={prt}\r\n")
|
f.write(f"TEXT={prt}\r\n")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f.write(f"RTP={rds_default_rtp_data},{len(str(name_table[track_name]))-1}\r\n")
|
f.write(f"RTP={rds_default_rtp_data},{len(str(name_table[track_name]))-1}\r\n")
|
||||||
except KeyError:
|
except KeyError:
|
||||||
f.write(f"RTP={rds_default_rtp_data},{len(rds_default_name)-1}\r\n")
|
f.write(f"RTP={rds_default_rtp_data},{len(rds_default_name)-1}\r\n")
|
||||||
f.close()
|
f.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error updating RDS: {e}")
|
print(f"Error updating RDS: {e}")
|
||||||
|
|
||||||
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:
|
except OSError:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def load_playlist(playlist_path):
|
def load_playlist(playlist_path):
|
||||||
try:
|
try:
|
||||||
with open(playlist_path, 'r') as f:
|
with open(playlist_path, 'r') as f:
|
||||||
tracks = [line.strip() for line in f.readlines() if line.strip()]
|
tracks = [line.strip() for line in f.readlines() if line.strip()]
|
||||||
return tracks
|
return tracks
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print(f"Warning: Playlist not found: {playlist_path}")
|
print(f"Warning: Playlist not found: {playlist_path}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def get_newest_track(tracks):
|
def get_newest_track(tracks):
|
||||||
if not tracks:
|
if not tracks:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
newest_track = None
|
newest_track = None
|
||||||
newest_time = 0
|
newest_time = 0
|
||||||
|
|
||||||
for track in tracks:
|
for track in tracks:
|
||||||
track_path = os.path.expanduser(track)
|
track_path = os.path.expanduser(track)
|
||||||
try:
|
try:
|
||||||
mod_time = os.path.getmtime(track_path)
|
mod_time = os.path.getmtime(track_path)
|
||||||
if mod_time > newest_time:
|
if mod_time > newest_time:
|
||||||
newest_time = mod_time
|
newest_time = mod_time
|
||||||
newest_track = track
|
newest_track = track
|
||||||
except OSError:
|
except OSError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return newest_track
|
return newest_track
|
||||||
|
|
||||||
def play_playlist(playlist_path, play_newest_first=False):
|
def play_playlist(playlist_path, play_newest_first=False):
|
||||||
last_modified_time = get_playlist_modification_time(playlist_path)
|
last_modified_time = get_playlist_modification_time(playlist_path)
|
||||||
tracks = load_playlist(playlist_path)
|
tracks = load_playlist(playlist_path)
|
||||||
if not tracks:
|
if not tracks:
|
||||||
print(f"No tracks found in {playlist_path}, checking again in 15 seconds...")
|
print(f"No tracks found in {playlist_path}, checking again in 15 seconds...")
|
||||||
time.sleep(15)
|
time.sleep(15)
|
||||||
return
|
return
|
||||||
|
|
||||||
random.seed()
|
random.seed()
|
||||||
if play_newest_first:
|
if play_newest_first:
|
||||||
newest_track = get_newest_track(tracks)
|
newest_track = get_newest_track(tracks)
|
||||||
if newest_track:
|
if newest_track:
|
||||||
print(f"Playing newest track first: {os.path.basename(newest_track)}")
|
print(f"Playing newest track first: {os.path.basename(newest_track)}")
|
||||||
tracks.remove(newest_track)
|
tracks.remove(newest_track)
|
||||||
random.shuffle(tracks)
|
random.shuffle(tracks)
|
||||||
tracks.insert(0, newest_track)
|
tracks.insert(0, newest_track)
|
||||||
else:
|
else:
|
||||||
random.shuffle(tracks)
|
random.shuffle(tracks)
|
||||||
|
|
||||||
for track in tracks:
|
for track in tracks:
|
||||||
current_modified_time = get_playlist_modification_time(playlist_path)
|
current_modified_time = get_playlist_modification_time(playlist_path)
|
||||||
if current_modified_time > last_modified_time:
|
if current_modified_time > last_modified_time:
|
||||||
print(f"Playlist {playlist_path} has been modified, reloading...")
|
print(f"Playlist {playlist_path} has been modified, reloading...")
|
||||||
return
|
return
|
||||||
|
|
||||||
current_hour = get_current_hour()
|
current_hour = get_current_hour()
|
||||||
current_day = get_current_day()
|
current_day = get_current_day()
|
||||||
morning_playlist_path = os.path.join(playlist_dir, current_day, 'morning')
|
morning_playlist_path = os.path.join(playlist_dir, current_day, 'morning')
|
||||||
day_playlist_path = os.path.join(playlist_dir, current_day, 'day')
|
day_playlist_path = os.path.join(playlist_dir, current_day, 'day')
|
||||||
night_playlist_path = os.path.join(playlist_dir, current_day, 'night')
|
night_playlist_path = os.path.join(playlist_dir, current_day, 'night')
|
||||||
late_night_playlist_path = os.path.join(playlist_dir, current_day, 'late_night')
|
late_night_playlist_path = os.path.join(playlist_dir, current_day, 'late_night')
|
||||||
|
|
||||||
if DAY_START <= current_hour < DAY_END:
|
if DAY_START <= current_hour < DAY_END:
|
||||||
if playlist_path != day_playlist_path:
|
if playlist_path != day_playlist_path:
|
||||||
print("Time changed to day hours, switching playlist...")
|
print("Time changed to day hours, switching playlist...")
|
||||||
return
|
return
|
||||||
elif MORNING_START <= current_hour < MORNING_END:
|
elif MORNING_START <= current_hour < MORNING_END:
|
||||||
if playlist_path != morning_playlist_path:
|
if playlist_path != morning_playlist_path:
|
||||||
print("Time changed to morning hours, switching playlist...")
|
print("Time changed to morning hours, switching playlist...")
|
||||||
return
|
return
|
||||||
elif LATE_NIGHT_START <= current_hour < LATE_NIGHT_END:
|
elif LATE_NIGHT_START <= current_hour < LATE_NIGHT_END:
|
||||||
if playlist_path != late_night_playlist_path:
|
if playlist_path != late_night_playlist_path:
|
||||||
print("Time changed to late night hours, switching playlist...")
|
print("Time changed to late night hours, switching playlist...")
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
if playlist_path != night_playlist_path:
|
if playlist_path != night_playlist_path:
|
||||||
print("Time changed to night hours, switching playlist...")
|
print("Time changed to night hours, switching playlist...")
|
||||||
return
|
return
|
||||||
|
|
||||||
track_path = os.path.expanduser(track)
|
track_path = os.path.expanduser(track)
|
||||||
track_name = os.path.basename(track_path)
|
track_name = os.path.basename(track_path)
|
||||||
print(f"Now playing: {track_name}")
|
print(f"Now playing: {track_name}")
|
||||||
update_rds(track_name)
|
update_rds(track_name)
|
||||||
|
|
||||||
subprocess.run(['ffplay', '-nodisp', '-stats', '-hide_banner', '-autoexit', track_path])
|
subprocess.run(['ffplay', '-nodisp', '-stats', '-hide_banner', '-autoexit', track_path])
|
||||||
|
if can_delete_file("/tmp/radioPlayer_quit"):
|
||||||
|
os.remove("/tmp/radioPlayer_quit")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
def can_delete_file(filepath):
|
||||||
|
if not os.path.isfile(filepath):
|
||||||
|
return False
|
||||||
|
directory = os.path.dirname(os.path.abspath(filepath)) or '.'
|
||||||
|
return os.access(directory, os.W_OK | os.X_OK)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
arg = sys.argv[1] if len(sys.argv) > 1 else None
|
arg = sys.argv[1] if len(sys.argv) > 1 else None
|
||||||
play_newest_first = False
|
play_newest_first = False
|
||||||
pre_track_path = None
|
pre_track_path = None
|
||||||
|
|
||||||
if arg:
|
if arg:
|
||||||
if arg.lower() == "n":
|
if arg.lower() == "n":
|
||||||
play_newest_first = True
|
play_newest_first = True
|
||||||
print("Newest song will be played first")
|
print("Newest song will be played first")
|
||||||
elif os.path.isfile(arg):
|
elif os.path.isfile(arg):
|
||||||
pre_track_path = arg
|
pre_track_path = arg
|
||||||
print(f"Will play requested song first: {arg}")
|
print(f"Will play requested song first: {arg}")
|
||||||
else:
|
else:
|
||||||
print(f"Invalid argument or file not found: {arg}")
|
print(f"Invalid argument or file not found: {arg}")
|
||||||
|
|
||||||
if pre_track_path:
|
if pre_track_path:
|
||||||
track_name = os.path.basename(pre_track_path)
|
track_name = os.path.basename(pre_track_path)
|
||||||
print(f"Now playing: {track_name}")
|
print(f"Now playing: {track_name}")
|
||||||
update_rds(track_name)
|
update_rds(track_name)
|
||||||
subprocess.run(['ffplay', '-nodisp', '-stats', '-hide_banner', '-autoexit', pre_track_path])
|
subprocess.run(['ffplay', '-nodisp', '-stats', '-hide_banner', '-autoexit', pre_track_path])
|
||||||
|
if can_delete_file("/tmp/radioPlayer_quit"):
|
||||||
|
os.remove("/tmp/radioPlayer_quit")
|
||||||
|
exit()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
current_hour = get_current_hour()
|
current_hour = get_current_hour()
|
||||||
current_day = get_current_day()
|
current_day = get_current_day()
|
||||||
|
|
||||||
morning_playlist = os.path.join(playlist_dir, current_day, 'morning')
|
morning_playlist = os.path.join(playlist_dir, current_day, 'morning')
|
||||||
day_playlist = os.path.join(playlist_dir, current_day, 'day')
|
day_playlist = os.path.join(playlist_dir, current_day, 'day')
|
||||||
night_playlist = os.path.join(playlist_dir, current_day, 'night')
|
night_playlist = os.path.join(playlist_dir, current_day, 'night')
|
||||||
late_night_playlist = os.path.join(playlist_dir, current_day, 'late_night')
|
late_night_playlist = os.path.join(playlist_dir, current_day, 'late_night')
|
||||||
|
|
||||||
morning_dir = os.path.dirname(morning_playlist)
|
morning_dir = os.path.dirname(morning_playlist)
|
||||||
day_dir = os.path.dirname(day_playlist)
|
day_dir = os.path.dirname(day_playlist)
|
||||||
night_dir = os.path.dirname(night_playlist)
|
night_dir = os.path.dirname(night_playlist)
|
||||||
late_night_dir = os.path.dirname(late_night_playlist)
|
late_night_dir = os.path.dirname(late_night_playlist)
|
||||||
|
|
||||||
if not os.path.exists(morning_dir):
|
if not os.path.exists(morning_dir):
|
||||||
print(f"Creating directory: {morning_dir}")
|
print(f"Creating directory: {morning_dir}")
|
||||||
os.makedirs(morning_dir, exist_ok=True)
|
os.makedirs(morning_dir, exist_ok=True)
|
||||||
if not os.path.exists(day_dir):
|
if not os.path.exists(day_dir):
|
||||||
print(f"Creating directory: {day_dir}")
|
print(f"Creating directory: {day_dir}")
|
||||||
os.makedirs(day_dir, exist_ok=True)
|
os.makedirs(day_dir, exist_ok=True)
|
||||||
|
|
||||||
if not os.path.exists(night_dir):
|
if not os.path.exists(night_dir):
|
||||||
print(f"Creating directory: {night_dir}")
|
print(f"Creating directory: {night_dir}")
|
||||||
os.makedirs(night_dir, exist_ok=True)
|
os.makedirs(night_dir, exist_ok=True)
|
||||||
|
|
||||||
if not os.path.exists(late_night_dir):
|
if not os.path.exists(late_night_dir):
|
||||||
print(f"Creating directory: {late_night_dir}")
|
print(f"Creating directory: {late_night_dir}")
|
||||||
os.makedirs(late_night_dir, exist_ok=True)
|
os.makedirs(late_night_dir, exist_ok=True)
|
||||||
|
|
||||||
for playlist_path in [morning_playlist, day_playlist, night_playlist, late_night_playlist]:
|
for playlist_path in [morning_playlist, day_playlist, night_playlist, late_night_playlist]:
|
||||||
if not os.path.exists(playlist_path):
|
if not os.path.exists(playlist_path):
|
||||||
print(f"Creating empty playlist: {playlist_path}")
|
print(f"Creating empty playlist: {playlist_path}")
|
||||||
with open(playlist_path, 'w') as f:
|
with open(playlist_path, 'w') as f:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if DAY_START <= current_hour < DAY_END:
|
if DAY_START <= current_hour < DAY_END:
|
||||||
print(f"Playing {current_day} day playlist...")
|
print(f"Playing {current_day} day playlist...")
|
||||||
play_playlist(day_playlist, play_newest_first)
|
play_playlist(day_playlist, play_newest_first)
|
||||||
elif MORNING_START <= current_hour < MORNING_END:
|
elif MORNING_START <= current_hour < MORNING_END:
|
||||||
print(f"Playing {current_day} morning playlist...")
|
print(f"Playing {current_day} morning playlist...")
|
||||||
play_playlist(morning_playlist, play_newest_first)
|
play_playlist(morning_playlist, play_newest_first)
|
||||||
elif LATE_NIGHT_START <= current_hour < LATE_NIGHT_END:
|
elif LATE_NIGHT_START <= current_hour < LATE_NIGHT_END:
|
||||||
print(f"Playing {current_day} late_night playlist...")
|
print(f"Playing {current_day} late_night playlist...")
|
||||||
play_playlist(late_night_playlist, play_newest_first)
|
play_playlist(late_night_playlist, play_newest_first)
|
||||||
else:
|
else:
|
||||||
print(f"Playing {current_day} night playlist...")
|
print(f"Playing {current_day} night playlist...")
|
||||||
play_playlist(night_playlist, play_newest_first)
|
play_playlist(night_playlist, play_newest_first)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user