Switch to unified view

a/rdpl2stream/AsfPlaylistDecoder.py b/rdpl2stream/AsfPlaylistDecoder.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
from StringIO import StringIO
23
import logging
24
30
25
class AsfPlaylistDecoder:
31
class AsfPlaylistDecoder:
26
27
    def __init__(self):
32
    def __init__(self):
28
        self.log = logging.getLogger('radiotray')
33
        self.log = Logger()
29
        self.log.debug('Initializing ASF playlist decoder')
30
31
34
32
    def isStreamValid(self, contentType, firstBytes):
35
    def isStreamValid(self, contentType, firstBytes):
33
36
        if 'video/x-ms-asf' in contentType and \
34
        if('video/x-ms-asf' in contentType and firstBytes.strip().lower().startswith('[reference]')):
37
           firstBytes.strip().lower().startswith('b[reference]'):
35
            self.log.info('Stream is readable by ASF Playlist Decoder')
38
            self.log.info('Stream is readable by ASF Playlist Decoder')
36
            return True
39
            return True
37
        else:
40
        else:
38
            return False
41
            return False
39
42
40
        
43
        
41
    def extractPlaylist(self,  url):
44
    def extractPlaylist(self,  url):
42
43
        self.log.info('Downloading playlist..')
45
        self.log.info('ASF: downloading playlist..')
44
45
        req = urllib2.Request(url)
46
        req = UrlRequest(url)
46
        req.add_header('User-Agent', USER_AGENT)
47
        req.add_header('User-Agent', USER_AGENT)
47
        f = urllib2.urlopen(req)
48
        f = urlUrlopen(req)
48
        str = f.read()
49
        str = f.read()
49
        f.close()
50
        f.close()
50
51
51
        self.log.info('Playlist downloaded')
52
        self.log.info('ASF: playlist downloaded, decoding...')
52
        self.log.info('Decoding playlist...')
53
53
54
        playlist = []
54
        playlist = []
55
        lines = str.split("\n")
55
        lines = str.splitlines()
56
        for line in lines:
56
        for line in lines:
57
58
            if (line.startswith("Ref") == True):
57
            if line.startswith(b"Ref"):
59
60
                list = line.split("=", 1)
58
                list = line.split(b"=", 1)
61
                tmp = list[1].strip()
59
                tmp = list[1].strip()
62
63
                if (tmp.endswith("?MSWMExt=.asf")):
60
                if tmp.endswith(b"?MSWMExt=.asf"):
64
                    playlist.append(tmp.replace("http", "mms"))
61
                    playlist.append(tmp.replace(b"http", b"mms"))
65
                else:
62
                else:
66
                    playlist.append(tmp)
63
                    playlist.append(tmp)
67
         
64
         
68
        return playlist
65
        return playlist
69