-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_podcasts.py
executable file
·218 lines (181 loc) · 5.46 KB
/
create_podcasts.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Raspberry Pi Internet Radio podcast utility
# $Id: create_podcasts.py,v 1.2 2014/07/13 11:13:44 bob Exp $
#
# 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
import unicodedata
from xml.dom.minidom import parseString
# Output errors to STDERR
stderr = sys.stderr.write;
# File locations
PlsDirectory = '/var/lib/mpd/playlists/'
RadioLibDir = '/var/lib/radiod/'
PodList = RadioLibDir + 'podcasts'
# MPD won't load greater than 350 entries in a playlist
MAX_ENTRIES = 250 # Maximum amount of entries in a playlist
# Execute system command
def execCommand(cmd):
p = os.popen(cmd)
return p.readline().rstrip('\n')
# Create the output from the title, URL, length and file number
def createPlsOutput(title,url,length,filenumber):
output = []
output.append('File%s=%s' % (filenumber,url))
output.append('Title%s=%s' % (filenumber,title))
output.append('Length%s=%s' % (filenumber,length))
return output
# Create the PLS or M3U file
def createPlsFile(basename,output,nlines):
global duplicateCount
uniqueCount = 1
# Limit very large playlists
if nlines > MAX_ENTRIES:
nlines = MAX_ENTRIES
if len(basename) < 1:
filename = 'unknown'
plsfile = basename + '.pls'
filename = PlsDirectory + plsfile
try:
print 'Creating ' + filename
outfile = open(filename,'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()
# Load the new podcast into MPD
print "Loading", plsfile + '\n'
execCommand("mpc load " + plsfile)
except:
print "Failed to create",outfile
return
# Execute system command
def execCommand(cmd):
p = os.popen(cmd)
return p.readline().rstrip('\n')
def parseXml(url,filename,data,count):
global errorCount
global warningCount
output = []
try:
dom = parseString(data)
except Exception,e:
print "Error:",e
print "Error: Could not parse XML data from,", url + '\n'
errorCount += 1
return
try:
# Get all the items in the podcast
items = dom.getElementsByTagName('item')
filenumber = 0
if count < 1:
count = len(items)
# Get the title and enclosure
for node in items:
filenumber += 1
title = node.getElementsByTagName('title')[0].firstChild.nodeValue
dtitle = title.encode('utf8', 'replace')
print 'Title',filenumber,dtitle
enclosure = node.getElementsByTagName('enclosure')[0]
attributes = enclosure.attributes.items()
url = dict(attributes).get('url')
length = dict(attributes).get('length')
type = dict(attributes).get('type')
durl = url.encode('utf8', 'replace')
print durl,length
output += createPlsOutput(title,url,length,filenumber)
# Limit using count
count -= 1
if count < 1:
break
# Now create the file
createPlsFile(filename,output,filenumber)
except IndexError,e:
print "Error:",e
print "Error parsing", url
errorCount += 1
return "# DOM Error"
return output
# Start of main routine
if os.getuid() != 0:
print "This program can only be run as root user or using sudo"
sys.exit(1)
lineCount = 0 # Line being processed (Including comments)
errorCount = 0 # Errors
warningCount = 0 # Warnings
processedCount = 0 # Processed station count
# Set up playlist directory
if not os.path.exists(PlsDirectory):
execCommand ("mkdir -p " + PlsDirectory )
# Main processing loop
for line in open(PodList,'r'):
lineCount += 1
lines = []
# Skip commented out or blank lines
line = line.rstrip() # Remove line feed
if line[:1] == '#':
continue
# Ignore blank lines
if len(line) < 1:
continue
# Check start of title defined
elif line[:1] != '[':
stderr("Error: Missing left bracket [ in line %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 %s in %s\n" % (lineCount,StationList))
format()
errorCount += 1
continue
# Get title and URL from station definition
title = lineparts[0].lstrip()
print title
# Extract number of feeds if present
if ':' in title:
titleparts = title.split(':')
title = titleparts[0]
count = int(titleparts[1])
else:
count = 0 # Means get all streams
# Get the URL
url = lineparts[1].lstrip()
# Get the published URL and determine its type
print 'Processing podcast ' + str(lineCount) + ': ' + url
# 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
# Create list from data
filename = title.replace(' ', '_')
parseXml(url,filename,data,count)
# End of script