Download this file

StreamDecoder.py    144 lines (114 with data), 5.1 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
##########################################################################
# 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 urllib2
import sys
import ssl
from lib.common import USER_AGENT
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 Logger:
def mprint(self, m):
#print("%s"%m, file=sys.stderr)
pass
def error(self, m):
self.mprint(m)
def warn(self, m):
self.mprint(m)
def info(self, m):
self.mprint(m)
def debug(self, m):
self.mprint(m)
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, 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 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 = urllib2.Request(url)
req.add_header('User-Agent', USER_AGENT)
try:
opener = urllib2.build_opener(
DummyMMSHandler(),
urllib2.HTTPSHandler(context =
ssl._create_unverified_context()))
f = opener.open(req, timeout=float(self.url_timeout))
except urllib2.HTTPError, e:
self.log.warn('HTTP Error: No radio stream found for %s - %s' %
(url, str(e)))
return None
except urllib2.URLError, e:
self.log.info('No radio stream found for %s'% url)
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 Exception, e:
self.log.warn('No radio stream found. Error: %s'% str(e))
return None
metadata = f.info()
firstbytes = f.read(500)
f.close()
try:
self.log.debug('Metadata obtained...')
contentType = metadata["Content-Type"]
self.log.info('Content-Type: %s'% contentType)
except Exception, e:
self.log.info("Couldn't read content-type. Maybe direct stream...")
self.log.info('Error: %s'%e)
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())