Parent: [77bba9] (diff)

Child: [aedaf9] (diff)

Download this file

StreamDecoder.py    146 lines (122 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
##########################################################################
# Copyright 2009 Carlos Ribeiro
#
# This file is part of Radio Tray
#
# Radio Tray is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 1 of the License, or
# (at your option) any later version.
#
# Radio Tray is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radio Tray. If not, see <http://www.gnu.org/licenses/>.
#
##########################################################################
from __future__ import print_function
import sys
PY3 = sys.version > '3'
if PY3:
from urllib.request import Request as UrlRequest
import urllib.request, urllib.error, urllib.parse
from urllib.error import HTTPError as HTTPError
from urllib.error import URLError as URLError
from http.client import BadStatusLine as BadStatusLine
from urllib.request import build_opener as urlBuild_opener
from urllib.request import HTTPSHandler
else:
from urllib2 import Request as UrlRequest
from urllib2 import HTTPError as HTTPError
from urllib2 import URLError as URLError
from urllib2 import build_opener as urlBuild_opener
from urllib2 import HTTPSHandler
class BadStatusLine:
pass
import ssl
from lib.common import USER_AGENT, Logger
from lib.DummyMMSHandler import DummyMMSHandler
from PlsPlaylistDecoder import PlsPlaylistDecoder
from M3uPlaylistDecoder import M3uPlaylistDecoder
from AsxPlaylistDecoder import AsxPlaylistDecoder
from XspfPlaylistDecoder import XspfPlaylistDecoder
from AsfPlaylistDecoder import AsfPlaylistDecoder
from RamPlaylistDecoder import RamPlaylistDecoder
from UrlInfo import UrlInfo
class StreamDecoder:
def __init__(self, cfg_provider):
plsDecoder = PlsPlaylistDecoder()
m3uDecoder = M3uPlaylistDecoder()
asxDecoder = AsxPlaylistDecoder()
xspfDecoder = XspfPlaylistDecoder()
asfDecoder = AsfPlaylistDecoder()
ramDecoder = RamPlaylistDecoder()
self.log = Logger()
self.decoders = [plsDecoder, asxDecoder, asfDecoder, xspfDecoder, ramDecoder, m3uDecoder]
self.url_timeout = None
try:
self.url_timeout = cfg_provider.getConfigValue("url_timeout")
if (self.url_timeout == None):
self.log.warn("Couldn't find url_timeout configuration")
self.url_timeout = 100
cfg_provider.setConfigValue("url_timeout", str(self.url_timeout))
except Exception as e:
self.log.warn("Couldn't find url_timeout configuration")
self.url_timeout = 100
cfg_provider.setConfigValue("url_timeout", str(self.url_timeout))
self.log.info('Using url timeout = %s'% str(self.url_timeout))
def getMediaStreamInfo(self, url):
if type(url) != type(u""):
url = url.decode('utf-8')
if url.startswith("http") == False:
self.log.info('Not an HTTP url. Maybe direct stream...')
return UrlInfo(url, False, None)
self.log.info('Requesting stream... %s'% url)
req = UrlRequest(url)
req.add_header('User-Agent', USER_AGENT)
try:
opener = urlBuild_opener(
DummyMMSHandler(),
HTTPSHandler(context = ssl._create_unverified_context()))
f = opener.open(req, timeout=float(self.url_timeout))
except HTTPError as e:
self.log.warn('HTTP Error for %s: %s' % (url, e))
return None
except URLError as e:
self.log.info('URLError for %s: %s ' % (url, e))
if str(e.reason).startswith('MMS REDIRECT'):
newurl = e.reason.split("MMS REDIRECT:",1)[1]
self.log.info('Found mms redirect for: %s' % newurl)
return UrlInfo(newurl, False, None)
else:
return None
except BadStatusLine as e:
if str(e).startswith('ICY 200'):
self.log.info('Found ICY stream')
return UrlInfo(url, False, None)
else:
return None
except Exception as e:
self.log.warn('%s: for %s: Error %s: %s' % (type(e), url, e))
return None
metadata = f.info()
firstbytes = f.read(500)
f.close()
try:
contentType = metadata["content-type"]
self.log.info('Content-Type: %s'% contentType)
except Exception as e:
self.log.info("Couldn't read content-type. Maybe direct stream...")
return UrlInfo(url, False, None)
for decoder in self.decoders:
self.log.info('Checking decoder')
if decoder.isStreamValid(contentType, firstbytes):
return UrlInfo(url, True, contentType, decoder)
# no playlist decoder found. Maybe a direct stream
self.log.info('No playlist decoder could handle the stream. Maybe direct stream...')
return UrlInfo(url, False, contentType)
def getPlaylist(self, urlInfo):
return urlInfo.getDecoder().extractPlaylist(urlInfo.getUrl())