-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_playlists.py
572 lines (456 loc) · 15.2 KB
/
create_playlists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Raspberry Pi Internet Radio playlist utility
# $Id: create_playlists.py,v 1.21 2014/07/12 10:35:53 bob Exp $
#
# Create playlist files from the following url formats
# iPhone stream files (.asx)
# Playlist files (.pls)
# Extended M3U files (.m3u)
# Direct media streams (no extension)
#
# See Raspberry PI Radio constructors manual for instructions
#
# Author : Bob Rathbone
# Web site : http://www.bobrathbone.com
#
#
# License: GNU V3, See https://www.gnu.org/copyleft/gpl.html
#
# Disclaimer: Software is provided as is and absolutly no warranties are
# implied or given. The authors shall not be liable for any loss
# or damage however caused.
#
import os
import sys
import urllib2
from xml.dom.minidom import parseString
# Output errors to STDERR
stderr = sys.stderr.write
# File locations
PlsDirectory = '/var/lib/mpd/playlists/'
RadioDir = '/home/pi/radio/'
RadioLibDir = '/var/lib/radiod/'
StationList = RadioLibDir + 'stationlist'
DistFile = '/home/pi/radio/station.urls'
TempDir = '/tmp/radio_stream_files/'
PlaylistsDir = '/home/pi/radio/playlists/'
PodcastsFile = '/var/lib/radiod/podcasts'
duplicateCount = 0
# Execute system command
def execCommand(cmd):
p = os.popen(cmd)
return p.readline().rstrip('\n')
# Create the initial list of files
def createList():
if not os.path.isfile(StationList):
print ('Creating ' + StationList + '\n')
execCommand("mkdir -p " + RadioLibDir)
print ("cp " + DistFile + ' ' + StationList)
execCommand("cp " + DistFile + ' ' + StationList)
print
return
# Create the output from the title and URL
def createPlsOutput(title, url, filenumber):
lines = []
lines.append('File%s=%s' % (filenumber, url))
lines.append('Title%s=%s' % (filenumber, title))
lines.append('Length%s=-1' % filenumber)
return lines
# Create the PLS or M3U file
def createPlsFile(filename, output, nlines):
global duplicateCount
uniqueCount = 1
if len(filename) < 1:
filename = 'unknown'
outfile = TempDir + filename + '.pls'
# Create unique files
exists = True
while exists:
if os.path.exists(outfile):
print ("Warning: " + outfile + ' already exists')
outfile = TempDir + filename + '[' + str(uniqueCount) + '].pls'
uniqueCount += 1
duplicateCount += 1
else:
exists = False
try:
print ('Creating ' + outfile + '\n')
outfile = open(outfile, 'w')
outfile.writelines("[playlist]\n")
outfile.writelines("NumberOfEntries=%s\n" % nlines)
outfile.writelines("Version=2\n")
for item in output:
outstr = item.encode('utf8', 'replace')
outfile.write(outstr + "\n")
outfile.close()
except:
print ("Failed to create", outfile)
return
# Beautify HTML convert tags to lower case
def parseHTML(data):
lcdata = ''
for line in data:
lcline = ''
line = line.rstrip()
line = line.lstrip()
line.replace('href =', 'href=')
length = len(line)
if length < 1:
continue
tag1right = line.find('>')
if tag1right > 1:
start_tag = line[0:tag1right + 1]
lcline = lcline + start_tag.lower()
tag2left = line.find('<', tag1right + 1)
if tag2left > 1:
end_tag = line[tag2left:length]
parameter = line[tag1right + 1:tag2left]
lcline = lcline + parameter + end_tag.lower()
lcdata = lcdata + lcline
return lcdata
# Get XML/HTML parameter
def getParameter(line):
tag1right = line.find('>')
tag2left = line.find('<', tag1right + 1)
parameter = line[tag1right + 1:tag2left]
return parameter
# Create a PLS file from an ASX(XML) file
def parseAsx(title, url, data, filenumber):
global errorCount
global warningCount
lcdata = parseHTML(data)
try:
dom = parseString(lcdata)
except Exception as e:
print ("Error:", e)
print ("Error: Could not parse XML data from,", url + '\n')
errorCount += 1
return
try:
# If title undefined in the station list get it from the file
if len(title) < 1:
titleTag = dom.getElementsByTagName('title')[0].toxml()
title = getParameter(titleTag)
except:
print ("Warning: Title not found in", url)
pass
finally:
try:
urlTag = dom.getElementsByTagName('ref')[0].toxml()
url = urlTag.replace('<ref href=\"', '').replace('\"/>', '')
urls = url.split('?')
url = urls[0]
print ('Title:', title)
plsfile = title.replace(' ', '_')
output = createPlsOutput(title, url, filenumber)
except IndexError as e:
print ("Error:", e)
print ("Error parsing", url)
errorCount += 1
return "# DOM Error"
return output
# Create filename from URL
def createFileName(title, url):
if len(title) > 0:
name = title
name = name.replace('.', ' ')
name = name.replace(' ', '_')
else:
try:
urlparts = url.rsplit('/', 1)
site = urlparts[0]
siteparts = site.split('/')
name = siteparts[2]
siteparts = name.split(':')
name = siteparts[0]
except:
name = url
name = name.replace('www.', '')
name = name.replace('.com', '')
name = name.replace('.', '_')
name = name.replace('__', '_')
return name
# Create default title
def createTitle(url):
urlparts = url.rsplit('/', 1)
site = urlparts[0]
siteparts = site.split('/')
name = siteparts[2]
siteparts = name.split(':')
title = siteparts[0]
return title
# Direct radio stream (MP3 AAC etc)
def parseDirect(title, url, filenumber):
url = url.replace('(stream)', '')
if len(title) < 1:
title = createTitle(url)
print ("Title:", title)
output = createPlsOutput(title, url, filenumber)
return output
# Create PLS file in the temporary directory
def parsePls(title, url, lines, filenumber):
plstitle = ''
plsurl = ''
for line in lines:
if line.startswith('Title1='):
titleline = line.split('=')
plstitle = titleline[1]
if line.startswith('File1='):
fileline = line.split('=')
plsurl = fileline[1]
# If title undefined in the station list get it from the file
if len(title) < 1:
if len(plstitle) > 1:
title = plstitle
else:
title = createTitle(url)
plsfile = createFileName(title, url)
print ('Title:', title)
plsfile = title.replace(' ', '_')
output = createPlsOutput(title, plsurl, filenumber)
return output
# Convert M3U file to PLS output
def parseM3u(title, lines, filenumber):
info = 'Unknown'
output = []
for line in lines:
line = line.replace('\r', '')
line = line.replace('\n', '')
if line.startswith('http:'):
url = line
elif line.startswith('#EXTINF:'):
info = line
if len(title) < 1:
title = info
if len(title) < 1:
filename = createFileName(title, url)
else:
filename = title.replace(' ', '_')
print ('Title:', title)
output.append('Title%s=%s' % (filenumber, title))
output.append('File%s=%s' % (filenumber, url))
output.append('Length%s=-1' % filenumber)
return output
# Usage message
def usage():
stderr("\nUsage: %s [--delete_old] [--no_delete] [--help]\n" % sys.argv[0])
stderr("\tWhere: --delete_old Delete old playlists\n")
stderr("\t --no_delete Don't delete old playlists\n")
stderr("\t --help Display help message\n\n")
return
# Station definition help message
def format():
stderr("Start a playlist with the name between brackets. For example:\n")
stderr("(BBC Radio Stations)\n")
stderr("This will create a playlist called BBC_Radio_Stations.pls)\n")
stderr("\nThe above is followed by station definitions")
stderr(" which take the following format:\n")
stderr("\t[title] http://<url>\n")
stderr("\tExample:\n")
stderr("\t[BBC Radio 3] http://bbc.co.uk/radio/listen/live/r3.asx\n\n")
stderr("End a playlist by inserting a blank line at the end")
stderr(" of the list of stations\n")
stderr("or start a new playlist definition.\n\n")
return
# Start of MAIN script
if os.getuid() != 0:
print ("This program can only be run as root user or using sudo")
sys.exit(1)
deleteOld = False
noDelete = False
if len(sys.argv) > 2:
stderr("\nError: you may not define more than one parameter at a time\n")
usage()
sys.exit(1)
if len(sys.argv) > 1:
param = sys.argv[1]
if param == '--delete_old':
deleteOld = True
elif param == '--no_delete':
noDelete = True
elif param == '--help':
usage()
format()
sys.exit(0)
else:
stderr("Invalid parameter %s\n" % param)
usage()
sys.exit(1)
# Create station URL list
createList()
# Temporary directory - if it exists then delete all pls files from it
execCommand("mkdir -p " + TempDir)
execCommand("rm -f " + TempDir + '*')
# Open the list of URLs
print ("Creating PLS files from", StationList + '\n')
lineCount = 0 # Line being processed (Including comments)
errorCount = 0 # Errors
duplicateCount = 0 # Duplicate file names
warningCount = 0 # Warnings
processedCount = 0 # Processed station count
# Copy user stream files to temporary directory
print ("Copying user PLS and M3U files from " + PlaylistsDir + " to " \
+ TempDir + '\n')
if os.listdir(PlaylistsDir):
execCommand("cp -f " + PlaylistsDir + '* ' + TempDir)
# Playlist file name
filename = ''
pls_output = []
filenumber = 1
writeFile = False
url = ''
title = ''
# Main processing loop
for line in open(StationList, 'r'):
lineCount += 1
lines = []
newplaylist = ''
# Set url types to False
isASX = False
isM3U = False
isPLS = False
# Skip commented out or blank lines
line = line.rstrip() # Remove line feed
if line[:1] == '#':
continue
# Handle playlist definition in () brackets
elif line[:1] == '(':
newplaylist = line.strip('(') # Remove left bracket
newplaylist = newplaylist.strip(')') # Remove right bracket
playlistname = newplaylist
newplaylist = newplaylist.replace(' ', '_')
if len(filename) > 0:
writeFile = True
else:
print ("Playlist:", playlistname)
filename = newplaylist
filenumber = 1
continue
if len(line) < 1 or writeFile:
if len(filename) < 1 and len(url) > 0:
filename = createFileName(title, url)
if len(filename) > 0 and len(pls_output) > 0:
createPlsFile(filename, pls_output, filenumber - 1)
filenumber = 1
pls_output = []
filename = ''
url = ''
if len(newplaylist) > 0:
filename = newplaylist
continue
if writeFile and len(line) > 0:
writeFile = False
else:
continue
# Check start of title defined
elif line[:1] != '[':
stderr("Error: Missing left bracket [ in line")
stderr(" %s in %s\n" % (lineCount, StationList))
format()
errorCount += 1
continue
processedCount += 1
line = line.lstrip('[')
# Get title and URL parts
line = line.strip()
lineparts = line.split(']')
# Should be 2 parts (title and url)
if len(lineparts) != 2:
stderr("Error: Missing right bracket [ in line")
stderr(" %s in %s\n" % (lineCount, StationList))
format()
errorCount += 1
continue
# Get title and URL from station definition
title = lineparts[0].lstrip()
url = lineparts[1].lstrip()
# Get the published URL and determine its type
print ('Processing line ' + str(lineCount) + ': ' + url)
# Extended M3U (MPEG 3 URL) format
if url.endswith('.m3u'):
isM3U = True
# Advanced Stream Redirector (ASX)
elif url.endswith('.asx'):
isASX = True
# Playlist format
elif url.endswith('.pls'):
isPLS = True
# Advanced Audio Coding stream (Don't retrieve any URL)
else:
# Remove redundant (stream) parameter
url = url.replace('(stream)', '')
pls_output += parseDirect(title, url, filenumber)
if len(filename) < 1:
filename = createFileName(title, url)
writeFile = True
filenumber += 1
continue
# Get the published URL to the stream file
try:
file = urllib2.urlopen(url)
data = file.read()
file.close()
except:
print ("Error: Failed to retrieve ", url)
errorCount += 1
continue
# Creat list from data
lines = data.split('\n')
firstline = lines[0].rstrip()
# process lines accoording to URL type
if isPLS:
pls_output += parsePls(title, url, lines, filenumber)
elif isM3U:
pls_output += parseM3u(title, lines, filenumber)
elif isASX:
if firstline.startswith('<ASX'):
pls_output += parseAsx(title, url, lines, filenumber)
else:
print (url, "didn't return XML data")
continue
if len(filename) < 1:
filename = createFileName(title, url)
writeFile = True
filenumber += 1
# End of for line
# Write last file
if len(filename) < 1:
filename = createFileName(title, url)
if len(filename) > 0 and len(pls_output) > 0:
createPlsFile(filename, pls_output, filenumber - 1)
print ("Processed %s station URLs from %s" % (processedCount, StationList))
# Copy files from temporary directory to playlist directory
oldfiles = len(os.listdir(PlsDirectory))
if oldfiles > 0:
if not deleteOld and not noDelete:
stderr("There are %s old playlist files in the %s directory.\n" %
(oldfiles, PlsDirectory))
stderr("Do you wish to remove the old files y/n: ")
answer = raw_input("")
if answer == 'y':
deleteOld = True
if deleteOld:
stderr("\nRemoving old playlists from directory %s\n" % PlsDirectory)
execCommand("rm -f " + PlsDirectory + "*.pls")
execCommand("rm -f " + PlsDirectory + "*.m3u")
else:
print ("Old playlist files not removed")
copiedCount = len(os.listdir(TempDir))
print ("Copying %s new playlist files to directory %s" % (copiedCount,
PlsDirectory))
execCommand("cp -f " + TempDir + '* ' + PlsDirectory)
if os.path.isfile(PodcastsFile):
print ("\nCreating Podcast playlists from " + PodcastsFile)
execCommand(RadioDir + "create_podcasts.py")
# Create summary report
print ("\nNew radio playlist files will be found in " + PlsDirectory)
if errorCount > 0:
print (str(errorCount) + " error(s)")
if duplicateCount > 0:
print (str(duplicateCount) + " duplicate file name(s) found and renamed.")
warningCount += duplicateCount
if warningCount > 0:
print (str(warningCount) + " warning(s)")
# End of script