|
a/rdpl2stream/RamPlaylistDecoder.py |
|
b/rdpl2stream/RamPlaylistDecoder.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 RamPlaylistDecoder:
|
31 |
class RamPlaylistDecoder:
|
|
|
32 |
def __init__(self):
|
|
|
33 |
self.log = Logger()
|
25 |
|
34 |
|
26 |
def __init__(self):
|
|
|
27 |
self.log = logging.getLogger('radiotray')
|
|
|
28 |
self.log.debug('RAM playlist decoder')
|
|
|
29 |
|
35 |
|
30 |
def isStreamValid(self, contentType, firstBytes):
|
36 |
def isStreamValid(self, contentType, firstBytes):
|
31 |
|
37 |
if 'audio/x-pn-realaudio' in contentType or \
|
32 |
if('audio/x-pn-realaudio' in contentType or 'audio/vnd.rn-realaudio' in contentType):
|
38 |
'audio/vnd.rn-realaudio' in contentType:
|
33 |
self.log.info('Stream is readable by RAM Playlist Decoder')
|
39 |
self.log.info('Stream is readable by RAM Playlist Decoder')
|
34 |
return True
|
40 |
return True
|
35 |
else:
|
41 |
else:
|
36 |
return False
|
42 |
return False
|
37 |
|
43 |
|
38 |
|
44 |
|
39 |
|
|
|
40 |
def extractPlaylist(self, url):
|
45 |
def extractPlaylist(self, url):
|
41 |
self.log.info('Downloading playlist...')
|
46 |
self.log.info('RAM: Downloading playlist...')
|
42 |
|
|
|
43 |
req = urllib2.Request(url)
|
47 |
req = UrlRequest(url)
|
44 |
req.add_header('User-Agent', USER_AGENT)
|
48 |
req.add_header('User-Agent', USER_AGENT)
|
45 |
f = urllib2.urlopen(req)
|
49 |
f = urUrlopen(req)
|
46 |
str = f.read()
|
50 |
str = f.read()
|
47 |
f.close()
|
51 |
f.close()
|
48 |
|
52 |
|
49 |
self.log.info('Playlist downloaded')
|
53 |
self.log.info('RAM Playlist downloaded, decoding...')
|
50 |
self.log.info('Decoding playlist...')
|
|
|
51 |
|
54 |
|
52 |
lines = str.splitlines()
|
55 |
lines = str.splitlines()
|
53 |
playlist = []
|
56 |
playlist = []
|
54 |
|
|
|
55 |
for line in lines:
|
57 |
for line in lines:
|
56 |
if line.startswith("#") == False and len(line) > 0:
|
58 |
if len(line) > 0 and not line.startswith(b"#"):
|
57 |
tmp = line.strip()
|
59 |
tmp = line.strip()
|
58 |
if(len(tmp) > 0):
|
60 |
if len(tmp) > 0:
|
59 |
playlist.append(line.strip())
|
61 |
playlist.append(line.strip())
|
60 |
|
62 |
|
61 |
return playlist
|
63 |
return playlist
|