Switch to unified view

a b/rdpl2stream/AsfPlaylistDecoder.py
1
##########################################################################
2
# Copyright 2009 Carlos Ribeiro
3
#
4
# This file is part of Radio Tray
5
#
6
# Radio Tray is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 1 of the License, or
9
# (at your option) any later version.
10
#
11
# Radio Tray is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
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/>.
18
#
19
##########################################################################
20
import urllib2
21
from lib.common import USER_AGENT
22
from lxml import etree
23
from lxml import objectify
24
from StringIO import StringIO
25
import logging
26
27
class AsfPlaylistDecoder:
28
29
    def __init__(self):
30
        self.log = logging.getLogger('radiotray')
31
        self.log.debug('Initializing ASF playlist decoder')
32
33
34
    def isStreamValid(self, contentType, firstBytes):
35
36
        if('video/x-ms-asf' in contentType and firstBytes.strip().lower().startswith('[reference]')):
37
            self.log.info('Stream is readable by ASF Playlist Decoder')
38
            return True
39
        else:
40
            return False
41
42
        
43
    def extractPlaylist(self,  url):
44
45
        self.log.info('Downloading playlist..')
46
47
        req = urllib2.Request(url)
48
        req.add_header('User-Agent', USER_AGENT)
49
        f = urllib2.urlopen(req)
50
        str = f.read()
51
        f.close()
52
53
        self.log.info('Playlist downloaded')
54
        self.log.info('Decoding playlist...')
55
56
        playlist = []
57
        lines = str.split("\n")
58
        for line in lines:
59
60
            if (line.startswith("Ref") == True):
61
62
                list = line.split("=", 1)
63
                tmp = list[1].strip()
64
65
                if (tmp.endswith("?MSWMExt=.asf")):
66
                    playlist.append(tmp.replace("http", "mms"))
67
                else:
68
                    playlist.append(tmp)
69
         
70
        return playlist
71