def test_fetch_hash_via_modern_player(self, mock_urlopen, mock_build_opener): hash_api_response = '{"hash":"c2VydmVyX3RpbWU9MTIvMTYvMjAyMCA0OjMxOjQ2IFBNJmhhc2hfdmFsdWU9WEkyeDYvd2F6UVFIRkpBTmpTamlqUT09JnZhbGlkbWludXRlcz0yNDAmaWQ9dmlldzI0Ny0xNDkx","expire":200}' # Mock so that the hash API returns a 500 cm = MagicMock() cm.getcode.return_value = 500 cm.read.return_value = None cm.__enter__.return_value = cm mock_urlopen.return_value = cm mock_urlopen.side_effect = HTTPError( "https://auth.smoothstreams.tv/hash_api.php?username=fake&site=view247&password=fake", 500, "Internal Error", {}, None, ) # Mock for the modern player. cm2 = MagicMock() cm2.code.return_value = 200 cm2.open.return_value.code = 200 cm2.open.return_value.read.return_value = hash_api_response cm2.__enter__.return_value = cm2 mock_build_opener.return_value = cm2 a = AuthSign(service=Service.LIVE247, auth=("fake", "fake")) a.fetch_hash() mock_build_opener.assert_called() self.assertEqual( "c2VydmVyX3RpbWU9MTIvMTYvMjAyMCA0OjMxOjQ2IFBNJmhhc2hfdmFsdWU9WEkyeDYvd2F6UVFIRkpBTmpTamlqUT09JnZhbGlkbWludXRlcz0yNDAmaWQ9dmlldzI0Ny0xNDkx", a.hash, )
def test_build_stream_url_rtsp(self): a = AuthSign(service=Service.STREAMTVNOW, auth=("fake", "fake")) a.expiration_date = datetime.now() + timedelta(minutes=240) a.hash = "abc1234" generated = self.g.build_stream_url(Server.EU_MIX, 3, a, Quality.LQ, Protocol.RTSP) self.assertEqual( "rtsp://deu.smoothstreams.tv:2935/viewstvn/ch03q3.stream/playlist.m3u8?wmsAuthSign=abc1234", generated, )
def test_build_stream_url_live247_dash(self): a = AuthSign(service=Service.LIVE247, auth=("fake", "fake")) # set hash and expiration manually a.expiration_date = datetime.now() + timedelta(minutes=240) a.hash = "abc1234" generated = self.g.build_stream_url(Server.NA_EAST_NY, 44, a, Quality.HD, Protocol.DASH) self.assertEqual( "https://dnae2.smoothstreams.tv:443/view247/ch44.smil/manifest.mpd?wmsAuthSign=abc1234", generated, )
def test_build_stream_url_streamtvnow_mpeg(self): a = AuthSign(service=Service.STREAMTVNOW, auth=("fake", "fake")) # set hash and expiration manually a.expiration_date = datetime.now() + timedelta(minutes=240) a.hash = "abc1234" generated = self.g.build_stream_url(Server.EU_MIX, 3, a, Quality.LQ, Protocol.MPEG) self.assertEqual( "https://deu.smoothstreams.tv:443/viewstvn/ch03q3.stream/mpeg.2ts?wmsAuthSign=abc1234", generated, )
def test_build_stream_url_streamtvnow_hls(self): a = AuthSign(service=Service.STREAMTVNOW, auth=("fake", "fake")) # set hash and expiration manually a.expiration_date = datetime.now() + timedelta(minutes=240) a.hash = "abc1234" generated = self.g.build_stream_url(Server.ASIA_MIX, 10, a, Quality.LQ, Protocol.HLS) self.assertEqual( "https://dAP.smoothstreams.tv:443/viewstvn/ch10q3.stream/playlist.m3u8?wmsAuthSign=abc1234", generated, )
def test_build_stream_url_live247_rtmp(self): a = AuthSign(service=Service.LIVE247, auth=("fake", "fake")) # set hash and expiration manually a.expiration_date = datetime.now() + timedelta(minutes=240) a.hash = "abc1234" generated = self.g.build_stream_url(Server.NA_EAST_NY, 44, a, Quality.HD, Protocol.RTMP) self.assertEqual( "rtmp://dnae2.smoothstreams.tv:3625/view247/ch44q1.stream/playlist.m3u8?wmsAuthSign=abc1234", generated, )
def test_generate_m3u_playlist(self, mock_urlopen): a = AuthSign(service=Service.LIVE247, auth=("fake", "fake")) a.expiration_date = datetime.now() + timedelta(minutes=240) a.hash = "abc1234" p = Playlist(a, self.g) with open("./tests/test_sample.m3u") as f: self.assertEqual( f.read(), p.generate_m3u_playlist(Server.NA_EAST_NY, a, Quality.HD, Protocol.HLS), )
def test_generate_streams(self): a = AuthSign(service=Service.STREAMTVNOW, auth=("fake", "fake")) with self.assertRaises(InvalidServer) as context: self.g.generate_streams("FakeServer", Quality.HD, a, protocol=Protocol.HLS) self.assertTrue( "FakeServer is not a valid server!" in str(context.exception)) with self.assertRaises(InvalidQuality) as context: self.g.generate_streams(Server.EU_MIX, 29, a, protocol=Protocol.HLS) self.assertTrue("29 is not a valid quality!" in str(context.exception)) with self.assertRaises(InvalidProtocol) as context: self.g.generate_streams(Server.EU_MIX, Quality.LQ, a, protocol="abc") self.assertTrue( "abc is not a valid protocol!" in str(context.exception))
def test_fetch_hash_via_hash_api(self, mock_urlopen): hash_api_response = '{"hash":"c2VydmVyX3RpbWU9MTIvMTYvMjAyMCA0OjMxOjQ2IFBNJmhhc2hfdmFsdWU9WEkyeDYvd2F6UVFIRkpBTmpTamlqUT09JnZhbGlkbWludXRlcz0yNDAmaWQ9dmlldzI0Ny0xNDkx","valid":240,"code":"1"}' cm = MagicMock() cm.getcode.return_value = 200 cm.read.return_value = hash_api_response cm.__enter__.return_value = cm mock_urlopen.return_value = cm a = AuthSign(service=Service.LIVE247, auth=("fake", "fake")) a.fetch_hash() self.assertEqual( "c2VydmVyX3RpbWU9MTIvMTYvMjAyMCA0OjMxOjQ2IFBNJmhhc2hfdmFsdWU9WEkyeDYvd2F6UVFIRkpBTmpTamlqUT09JnZhbGlkbWludXRlcz0yNDAmaWQ9dmlldzI0Ny0xNDkx", a.hash, )
def test_total_failure_when_fetching_hash(self, mock_urlopen): hash_api_response = '{"hash":"c2VydmVyX3RpbWU9MTIvMTYvMjAyMCA0OjMxOjQ2IFBNJmhhc2hfdmFsdWU9WEkyeDYvd2F6UVFIRkpBTmpTamlqUT09JnZhbGlkbWludXRlcz0yNDAmaWQ9dmlldzI0Ny0xNDkx","valid":200}' cm = MagicMock() cm.read.return_value = hash_api_response cm.__enter__.return_value = cm mock_urlopen.return_value = cm mock_urlopen.side_effect = HTTPError( "https://auth.smoothstreams.tv/hash_api.php?username=fake&site=view247&password=fake", 404, "Not Found", {}, None, ) a = AuthSign(service=Service.LIVE247, auth=("fake", "fake")) with self.assertRaises(Exception): a.fetch_hash()
channel_name = element.find('display-name').text logo_path = 'static/logos/' + channel_name + '.png' if os.path.isfile(logo_path): element.find('icon').attrib['src'] = request.url_root + 'logos/' + channel_name + '.png' return ET.tostring(tree) if __name__ == '__main__': if app.config['SERVICE'] == "live247": service = Service.LIVE247 elif app.config['SERVICE'] == "starstreams": service = Service.STARSTREAMS elif app.config['SERVICE'] == "streamtvnow": service = Service.STREAMTVNOW elif app.config['SERVICE'] == "mmatv": service = Service.MMATV else: raise InvalidService username = app.config['USERNAME'] password = app.config['PASSWORD'] server = app.config['SERVER'] quality = app.config['QUALITY'] auth_sign = AuthSign(service=service, auth=(username, password)) guide = Guide() app.run(host='0.0.0.0', port=5004)
def test_fetch_hash(self): with self.assertRaises(InvalidService) as context: AuthSign(service="ABC", auth=("fake", "password")) self.assertTrue("ABC is not a valid service!" in str(context.exception))