|
a/rdpl2stream/PlsPlaylistDecoder.py |
|
b/rdpl2stream/PlsPlaylistDecoder.py |
|
... |
|
... |
15 |
#
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with Radio Tray. If not, see <http://www.gnu.org/licenses/>.
|
17 |
# along with Radio Tray. If not, see <http://www.gnu.org/licenses/>.
|
18 |
#
|
18 |
#
|
19 |
##########################################################################
|
19 |
##########################################################################
|
20 |
import urllib2
|
20 |
import sys
|
|
|
21 |
PY3 = sys.version > '3'
|
|
|
22 |
if PY3:
|
|
|
23 |
from urllib.request import Request as UrlRequest
|
|
|
24 |
from urllib.request import urlopen as urlUrlopen
|
|
|
25 |
else:
|
|
|
26 |
from urllib2 import Request as UrlRequest
|
|
|
27 |
from urllib2 import urlopen as urlUrlopen
|
|
|
28 |
|
21 |
from lib.common import USER_AGENT
|
29 |
from lib.common import USER_AGENT, Logger
|
22 |
import logging
|
|
|
23 |
|
30 |
|
24 |
class PlsPlaylistDecoder:
|
31 |
class PlsPlaylistDecoder:
|
25 |
|
|
|
26 |
def __init__(self):
|
32 |
def __init__(self):
|
27 |
self.log = logging.getLogger('radiotray')
|
33 |
self.log = Logger()
|
28 |
self.log.debug('PLS playlist decoder')
|
34 |
self.log.debug('PLS playlist decoder')
|
29 |
|
35 |
|
30 |
|
|
|
31 |
def isStreamValid(self, contentType, firstBytes):
|
36 |
def isStreamValid(self, contentType, firstBytes):
|
32 |
|
37 |
if 'audio/x-scpls' in contentType or \
|
33 |
if(('audio/x-scpls' in contentType) or ('application/pls+xml' in contentType) or (firstBytes.strip().lower().startswith('[playlist]'))):
|
38 |
'application/pls+xml' in contentType or \
|
|
|
39 |
firstBytes.strip().lower().startswith(b'[playlist]'):
|
34 |
self.log.info('Stream is readable by PLS Playlist Decoder')
|
40 |
self.log.info('Stream is readable by PLS Playlist Decoder')
|
35 |
return True
|
41 |
return True
|
36 |
else:
|
42 |
else:
|
37 |
return False
|
43 |
return False
|
38 |
|
44 |
|
39 |
|
|
|
40 |
|
|
|
41 |
def extractPlaylist(self, url):
|
45 |
def extractPlaylist(self, url):
|
42 |
|
46 |
|
43 |
self.log.info('Downloading playlist...')
|
47 |
self.log.info('Downloading playlist...')
|
44 |
|
48 |
|
45 |
req = urllib2.Request(url)
|
49 |
req = UrlRequest(url)
|
46 |
req.add_header('User-Agent', USER_AGENT)
|
50 |
req.add_header('User-Agent', USER_AGENT)
|
47 |
f = urllib2.urlopen(req)
|
51 |
f = urlUrlopen(req)
|
48 |
str = f.read()
|
52 |
str = f.read()
|
49 |
f.close()
|
53 |
f.close()
|
50 |
|
54 |
|
51 |
self.log.info('Playlist downloaded')
|
55 |
self.log.info('Playlist downloaded')
|
52 |
self.log.info('Decoding playlist...')
|
56 |
self.log.info('Decoding playlist...')
|
53 |
|
57 |
|
54 |
playlist = []
|
58 |
playlist = []
|
55 |
lines = str.splitlines()
|
59 |
lines = str.splitlines()
|
56 |
for line in lines:
|
60 |
for line in lines:
|
57 |
|
|
|
58 |
if line.startswith("File") == True:
|
61 |
if line.startswith(b"File") == True:
|
59 |
|
|
|
60 |
list = line.split("=", 1)
|
62 |
list = line.split(b"=", 1)
|
61 |
playlist.append(list[1])
|
63 |
playlist.append(list[1])
|
62 |
|
64 |
|
63 |
|
65 |
|
64 |
return playlist
|
66 |
return playlist
|
65 |
|
67 |
|
66 |
|
68 |
|