Switch to unified view

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