-
Notifications
You must be signed in to change notification settings - Fork 3
/
streamTools.py
105 lines (98 loc) · 3.42 KB
/
streamTools.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
#-------------------------------------------------------------------------------
# Copyright 2010 B. Kroon <bart@tarmack.eu>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
'''
This module contains tools for stream handling.
'''
try:
from lxml import etree
except ImportError:
import xml.etree.ElementTree as etree
import StringIO
import httplib
class ParseError(Exception):
pass
def getStreamList(url):
response = _retreiveURL(url)
mime_type = response.getheader('content-type').split(';')[0]
if mime_type == 'audio/x-scpls':
data = response.read()
if data:
adrlist = parsePLS(data)
elif mime_type == 'audio/x-mpegurl':
data = response.read()
if data:
adrlist = parseM3U(data)
elif mime_type == 'application/xspf+xml':
data = response.read()
if data:
adrlist = parseXSPF(data)
else:
adrlist = [url]
return adrlist
def parsePLS(data):
''' Parse a PLS playlist. Returns a list with stream addresses.'''
adrlist = []
state = ''
data = data.split('\n')
while data:
line = data.pop(0)
if state == '' and line == '[playlist]':
state = 'playlist'
elif state == 'playlist':
if '=' in line:
key, value = line.split('=', 1)
if key.startswith('File'):
adrlist.append(value)
else:
raise ParseError('Encountered error during parsing of the playlist.')
return adrlist
def parseXSPF(data):
''' Parse a XSPF playlist. Returns a list with stream addresses.
XSPF spec: http://www.xspf.org/xspf-v1.html
Currently we only want the location URLs, so that
is all we parse for.
'''
xml = etree.parse(StringIO.StringIO(data))
root = xml.getroot()
locations = root.findall('.//{http://xspf.org/ns/0/}location')
adrlist = [adr.text.strip() for adr in locations
if adr.text.startswith('http://')]
if not adrlist:
raise ParseError('Encountered error during '
'parsing of the playlist.')
return adrlist
def parseM3U(data):
''' Parse a M3U playlist. Returns a list with stream addresses.'''
adrlist = []
data = data.split('\n')
while data:
line = data.pop(0)
if line.startswith('http://'):
adrlist.append(line.strip())
if not adrlist:
raise ParseError('Encountered error during parsing of the playlist.')
return adrlist
def _retreiveURL(url):
if url.startswith('http://'):
url = url[7:]
server, path = url.split('/', 1)
conn = httplib.HTTPConnection(server)
conn.request("GET", '/'+path)
resp = conn.getresponse()
if resp.status == 200:
return resp
else:
raise httplib.HTTPException('Got bad status code while retrieving url: "%s".' % url)