def start(self, *mocked, **params): with requests_mock.Mocker() as mock: mocked_users = mock.get( "https://api.twitch.tv/kraken/users.json?login=foo", json={"users": [{ "_id": 1234 }]}) mocked_stream = mock.get( "https://api.twitch.tv/kraken/streams/1234.json", json={"stream": None} if params.pop("offline", False) else { "stream": { "stream_type": params.pop("stream_type", "live"), "broadcast_platform": params.pop("broadcast_platform", "live"), "channel": { "broadcaster_software": params.pop("broadcaster_software", "") } } }) session = Streamlink() Twitch.bind(session, "tests.plugins.test_twitch") plugin = Twitch("https://www.twitch.tv/foo") plugin.options.set("disable-reruns", params.pop("disable", True)) try: streams = plugin.streams() except TestTwitchReruns.StopError: streams = True pass return streams, mocked_users, mocked_stream, mocked[0]
def subject(self, channel, hosts=None, disable=False): with requests_mock.Mocker() as mock: mock.get("https://api.twitch.tv/kraken/users?login=foo", json={"users": [{ "_id": 1 }]}) if hosts is None: mock.get("https://tmi.twitch.tv/hosts", json={}) else: mock.get("https://tmi.twitch.tv/hosts", [{ "json": { "hosts": [ dict(host_id=host_id, target_id=target_id, target_login=target_login, target_display_name=target_display_name) ] } } for host_id, target_id, target_login, target_display_name in hosts]) session = Streamlink() Twitch.bind(session, "tests.plugins.test_twitch") plugin = Twitch("https://twitch.tv/{0}".format(channel)) plugin.options.set("disable-hosting", disable) res = plugin._switch_to_hosted_channel() return res, plugin.channel, plugin._channel_id, plugin.author
def subject(channel, hosts=None, disable=False): with requests_mock.Mocker() as mock: mock.register_uri(requests_mock.ANY, requests_mock.ANY, exc=requests_mock.exceptions.InvalidRequest) if hosts is None: mock.post("https://gql.twitch.tv/gql", json={}) else: mock.post("https://gql.twitch.tv/gql", response_list=[{ "json": { "data": { "user": { "id": host[0], "hosting": None if not host[1:3] else { "login": host[1], "displayName": host[2] } } } } } for host in hosts]) session = Streamlink() Twitch.bind(session, "tests.plugins.test_twitch") plugin = Twitch("https://twitch.tv/{0}".format(channel)) plugin.options.set("disable-hosting", disable) res = plugin._switch_to_hosted_channel() return res, plugin.channel, plugin.author
def subject(self, **params): with requests_mock.Mocker() as mock: mock.get("https://api.twitch.tv/kraken/users?login=foo", json={"users": [{ "_id": 1234 }]}) mock.get( "https://api.twitch.tv/kraken/streams/1234", json={"stream": None} if params.pop("offline", False) else { "stream": { "stream_type": params.pop("stream_type", "live"), "broadcast_platform": params.pop("broadcast_platform", "live"), "channel": { "broadcaster_software": params.pop("broadcaster_software", "") } } }) session = Streamlink() Twitch.bind(session, "tests.plugins.test_twitch") plugin = Twitch("https://www.twitch.tv/foo") plugin.options.set("disable-reruns", params.pop("disable", True)) return plugin._check_for_rerun()
def subject(self, **params): with patch("streamlink.plugins.twitch.TwitchAPI.stream_metadata") as mock: mock.return_value = None if params.pop("offline", False) else {"type": params.pop("stream_type", "live")} session = Streamlink() Twitch.bind(session, "tests.plugins.test_twitch") plugin = Twitch("https://www.twitch.tv/foo") plugin.options.set("disable-reruns", params.pop("disable", True)) return plugin._check_for_rerun()
def test_can_handle_url(self): should_match = [ 'https://www.twitch.tv/twitch', 'https://www.twitch.tv/videos/150942279', 'https://clips.twitch.tv/ObservantBenevolentCarabeefPhilosoraptor', ] for url in should_match: self.assertTrue(Twitch.can_handle_url(url))
def test_stream_weight(): session = Streamlink() Twitch.bind(session, "tests.plugins.test_twitch") plugin = Twitch("http://twitch.tv/foo") with text("hls/test_master_twitch_vod.m3u8") as fh: playlist = fh.read() with requests_mock.Mocker() as mocker: mocker.register_uri(requests_mock.ANY, requests_mock.ANY, exc=requests_mock.exceptions.InvalidRequest) mocker.request(method="GET", url="http://mocked/master.m3u8", text=playlist) streams = TwitchHLSStream.parse_variant_playlist(session, "http://mocked/master.m3u8") with patch.object(plugin, "_get_streams", return_value=streams): data = plugin.streams() assert list(data.keys()) == ["audio", "160p30", "360p30", "480p30", "720p30", "720p60", "source", "worst", "best"] assert data["best"] is data["source"] assert data["worst"] is data["160p30"]
def subject(self, url): session = Streamlink() Twitch.bind(session, "tests.plugins.test_twitch") plugin = Twitch(url) return plugin.get_author(), plugin.get_title(), plugin.get_category()
def test_can_handle_url_negative(self): should_not_match = [ 'https://www.twitch.tv', ] for url in should_not_match: self.assertFalse(Twitch.can_handle_url(url))