Page 2 of 2

Re: Adding webradios by writing files .pls .m3u into a direc

PostPosted: 16 Oct 2015, 22:35
by xan
A bit late but maybe someone is still interested... Here is not a python script but a small bash-script that scans the Webradio directory and adds the the radio stations to redis.

Code: Select all
#! /bin/bash

for FILE in /mnt/MPD/Webradio/*.pls;
do
   NAME=$(echo $FILE | grep -Po "(?<=/)[^/]+(?=\.)");
   echo "station: $NAME";
   URL=$(grep -Po "(?<=File1=).*$" "$FILE");
   echo "url: $URL";
   redis-cli hset "webradios" "${NAME}" "${URL}";
done;
mpc update webradio;

You can copy that into a file called "updateWebradios" in the /bin directory (/bin/updateWebradios) chmod it to 755 and then you can call it by just typing "updateWebradios" into the command line.

Or you can just copy that as one line into the command line and hit enter
Code: Select all
for FILE in /mnt/MPD/Webradio/*.pls; do NAME=$(echo $FILE | grep -Po "(?<=/)[^/]+(?=\.)"); echo "station: $NAME"; URL=$(grep -Po "(?<=File1=).*$" "$FILE"); echo "url: $URL"; redis-cli hset "webradios" "${NAME}" "${URL}"; done; mpc update webradio;


After you have run the script you still have to go to "RESOURCES" and click "update MPD library" to get the stations visible. I am working on that...

Re: Adding webradios by writing files .pls .m3u into a direc

PostPosted: 19 Oct 2015, 20:06
by surfninja
this is great xan. I was going to write a perl version for the forum but didn't know the redis set syntax. Now I do.

thanks for posting.

Re: Adding webradios by writing files .pls .m3u into a direc

PostPosted: 02 Nov 2015, 22:28
by Sprigo
Well done xan! This saves me so much time as I was having to add them manually each time. :D

Re: Adding webradios by writing files .pls .m3u into a direc

PostPosted: 25 Mar 2016, 11:00
by c-moll
I've added my webradio folder according to the above instructions. But now the stations are listed in a random order. How can I sort the list?

Chris

Re: Adding webradios by writing files .pls .m3u into a direc

PostPosted: 16 Oct 2019, 21:11
by gargonia
Even later than xan, but here's a Python script that will bulk add web radio stations. It can either do it from .pls files in /mnt/MPD/Webradio or from a .csv file:

Code: Select all
#!/usr/bin/env python2

import glob
import os
import string
import subprocess
import sys
import time

def read_file(file_name):
   file_handle = open(file_name,'r')
   file_lines = map(string.strip,file_handle.readlines())
   file_handle.close()
   return file_lines

def create_pls_files(stations):
   for station in stations:
      station_pls_file = open('/mnt/MPD/Webradio/%s.pls' % station,'w')
      station_pls_file.write("""\
[playlist]
NumberOfEntries=1
File1=%s
Title1=%s""" % (station,stations[station]))
   station_pls_file.close()

def get_pls_stations():
   station_files = glob.glob('/mnt/MPD/Webradio/*.pls')
   station_data = {}
   for station_file in station_files:
      station = read_file(station_file)
      station_name = station[3].split('=')[-1]
      station_url = station[2].split('=')[-1]
      station_data[station_name] = station_url
   return station_data

def get_csv_stations():
   station_file = raw_input('Please enter the path to the webradio station file: ')
   if not os.path.isfile(station_file):
      print('Unable to find the file... please check the path and run again.')
      sys.exit()
   else:
      station_data = read_file(station_file)
      stations = dict(station.split(',') for station in station_data)
      create_pls_files(stations)
      return stations

def update_rune(stations):
   time.sleep(5)
   subprocess.call(['mpc','update'])
   for station in sorted(stations.keys()):
      subprocess.call(['redis-cli','hset','webradios',station,stations[station]])

def main():
   update_type = raw_input('Update from PLS files or CSV (P/c)? ')
   stations = {}
   if update_type.lower() == 'c':
      stations = get_csv_stations()
   else:
      stations = get_pls_stations()
   update_rune(stations)

if __name__ == '__main__':
   main()


Hope this helps someone else encountering this problem.