From 0b676393249d1f46dcfbe9718a08a0fff96d7aee Mon Sep 17 00:00:00 2001 From: d4m4s74 Date: Sat, 8 Mar 2014 22:32:23 +0100 Subject: [PATCH] Added Streaming Internet Radio support Removed all dependencies, ready for merge. --- PirateRadio.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/PirateRadio.py b/PirateRadio.py index afad1f8..9217609 100755 --- a/PirateRadio.py +++ b/PirateRadio.py @@ -43,7 +43,7 @@ def build_file_list(): folders.sort() files.sort() for filename in files: - if re.search(".(aac|mp3|wav|flac|m4a)$", filename) != None: + if re.search(".(aac|mp3|wav|flac|m4a|pls|m3u)$", filename) != None: file_list.append(os.path.join(root, filename)) return file_list @@ -53,14 +53,25 @@ def play_songs(file_list): print("Playing songs to frequency ", str(frequency)) print("Shuffle is " + on_off[shuffle]) print("Repeat All is " + on_off[repeat_all]) - #print("Stereo playback is " + on_off[play_stereo]) + # print("Stereo playback is " + on_off[play_stereo]) if shuffle == True: random.shuffle(file_list) with open(os.devnull, "w") as dev_null: for filename in file_list: print("Playing ",filename) - subprocess.call(["ffmpeg","-i",filename,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null) + if re.search(".pls$", filename) != None: + streamurl = parse_pls(filename, 1) + if streamurl != None: + print("streaming radio from " + streamurl) + subprocess.call(["ffmpeg","-i",streamurl,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null) + elif re.search(".m3u$", filename) != None: + streamurl = parse_m3u(filename, 1) + if streamurl != None: + print("streaming radio from " + streamurl) + subprocess.call(["ffmpeg","-i",streamurl,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null) + else: + subprocess.call(["ffmpeg","-i",filename,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null) @@ -81,6 +92,44 @@ def read_config(): shuffle = config.getboolean("pirateradio",'shuffle',fallback=False) repeat_all = config.getboolean("pirateradio",'repeat_all', fallback=False) +def parse_pls(src, titleindex): + # breaking up the pls file in separate strings + lines = [] + with open( src, "r" ) as f: + lines = f.readlines() + + # and parse the lines + if lines != None: + for line in lines: + # search for the URI + match = re.match( "^[ \\t]*file" + str(titleindex) + "[ \\t]*=[ \\t]*(.*$)", line, flags=re.IGNORECASE ) + if match != None: + if match.group( 1 ) != None: + # URI found, it's saved in the second match group + # output the URI to the destination file + return match.group( 1 ) + + return None + +def parse_m3u(src, titleindex): + # create a list of strings, one per line in the source file + lines = [] + searchindex = int(1) + with open( src, "r" ) as f: + lines = f.readlines() + + if lines != None: + for line in lines: + # search for the URI + if '://' in line: + if searchindex == titleindex: + return line + else: + searchindex += 1 + + return None + + def daemonize(): fpid=os.fork()