def test_handle_url_with_json_and_stream_url(self, console, args):
        stream = Mock()
        streams = dict(worst=Mock(), best=stream)
        plugin = Mock(FakePlugin(),
                      module="fake",
                      arguments=[],
                      streams=Mock(return_value=streams))

        with patch("streamlink_cli.main.streamlink",
                   resolve_url=Mock(return_value=plugin)):
            handle_url()
            self.assertEqual(console.msg.mock_calls, [])
            self.assertEqual(console.msg_json.mock_calls,
                             [call(dict(plugin="fake", streams=streams))])
            self.assertEqual(console.error.mock_calls, [])
            if is_py3:
                console.msg_json.mock_calls.clear()

                console.json = False
                handle_url()
                self.assertEqual(console.msg.mock_calls,
                                 [call("{0}", stream.to_manifest_url())])
                self.assertEqual(console.msg_json.mock_calls, [])
                self.assertEqual(console.error.mock_calls, [])
                console.msg_json.mock_calls.clear()
                """ fails
 def test_error(self, side_effect, expected):
     with patch("streamlink_cli.main.args", Mock(url="fakeurl")), \
          patch("streamlink_cli.main.streamlink", resolve_url=Mock(side_effect=side_effect)), \
          patch("streamlink_cli.main.console", exit=Mock(side_effect=SystemExit)) as mock_console:
         with pytest.raises(SystemExit):
             handle_url()
         assert mock_console.exit.mock_calls == [call(expected)]
Exemple #3
0
def cli_main(output):

    error_code = 0
    parser = cli.build_parser()

    cli.setup_args(parser, ignore_unknown=True)
    cli.args.output = output
    cli.args.url = 'https://pccold'
    cli.args.stream = ['default']
    # print(cli.args)

    # Console output should be on stderr if we are outputting
    # a stream to stdout.
    if cli.args.stdout or cli.args.output == "-":
        console_out = sys.stderr
    else:
        console_out = sys.stdout

    # We don't want log output when we are printing JSON or a command-line.
    silent_log = any(getattr(cli.args, attr) for attr in cli.QUIET_OPTIONS)
    log_level = cli.args.loglevel if not silent_log else "none"
    cli.setup_logging(console_out, log_level)
    cli.setup_console(console_out)

    cli.setup_streamlink()
    cli.setup_plugin_args(cli.streamlink, parser)
    path = os.path.abspath(os.path.dirname(__file__))[:-6] + 'plugins'
    print('dir', path)
    cli.load_plugins([path])

    # update the logging level if changed by a plugin specific config
    log_level = cli.args.loglevel if not silent_log else "none"
    cli.logger.root.setLevel(log_level)

    cli.setup_http_session()

    try:
        cli.setup_options()
        cli.handle_url()
    except KeyboardInterrupt:
        # Close output
        if cli.output:
            cli.output.close()
        cli.console.msg("Interrupted! Exiting...")
        error_code = 130
    finally:
        if cli.stream_fd:
            try:
                cli.log.info("Closing currently open stream...")
                cli.stream_fd.close()
            except KeyboardInterrupt:
                error_code = 130

    sys.exit(error_code)
Exemple #4
0
    def test_handle_url_with_json_and_stream_url(self, console, args):
        stream = Mock()
        streams = dict(worst=Mock(), best=stream)
        plugin = Mock(FakePlugin(), module="fake", arguments=[], streams=Mock(return_value=streams))

        with patch("streamlink_cli.main.streamlink", resolve_url=Mock(return_value=plugin)):
            handle_url()
            self.assertEqual(console.msg.mock_calls, [])
            self.assertEqual(console.msg_json.mock_calls, [call(dict(plugin="fake", streams=streams))])
            self.assertEqual(console.error.mock_calls, [])
            console.msg_json.mock_calls.clear()

            console.json = False
            handle_url()
            self.assertEqual(console.msg.mock_calls, [call("{0}", stream.to_manifest_url())])
            self.assertEqual(console.msg_json.mock_calls, [])
            self.assertEqual(console.error.mock_calls, [])
            console.msg.mock_calls.clear()

            stream.to_manifest_url.side_effect = TypeError()
            handle_url()
            self.assertEqual(console.msg.mock_calls, [])
            self.assertEqual(console.msg_json.mock_calls, [])
            self.assertEqual(console.exit.mock_calls, [call("The stream specified cannot be translated to a URL")])
            console.exit.mock_calls.clear()
Exemple #5
0
    def test_handle_url_with_json_and_stream_url(self, console, args):
        stream = Mock()
        streams = dict(worst=Mock(), best=stream)

        class _FakePlugin(FakePlugin):
            _streams = streams

        with patch("streamlink_cli.main.streamlink",
                   resolve_url=Mock(return_value=(_FakePlugin, ""))):
            handle_url()
            self.assertEqual(console.msg.mock_calls, [])
            self.assertEqual(console.msg_json.mock_calls, [
                call(plugin="fake",
                     metadata=dict(id="test-id-1234-5678",
                                   author="Tѥst Āuƭhǿr",
                                   category=None,
                                   title="Test Title"),
                     streams=streams)
            ])
            self.assertEqual(console.error.mock_calls, [])
            console.msg_json.mock_calls.clear()

            args.json = False
            handle_url()
            self.assertEqual(console.msg.mock_calls,
                             [call(stream.to_manifest_url())])
            self.assertEqual(console.msg_json.mock_calls, [])
            self.assertEqual(console.error.mock_calls, [])
            console.msg.mock_calls.clear()

            stream.to_manifest_url.side_effect = TypeError()
            handle_url()
            self.assertEqual(console.msg.mock_calls, [])
            self.assertEqual(console.msg_json.mock_calls, [])
            self.assertEqual(
                console.exit.mock_calls,
                [call("The stream specified cannot be translated to a URL")])
            console.exit.mock_calls.clear()