Exemplo n.º 1
0
    def test_num(self):
        # (value, func, result)
        test_data = [
            ('33', num(int, 5, 120), 33),
            ('234', num(int, min=10), 234),
            ('50.222', num(float, 10, 120), 50.222),
        ]

        for _v, _f, _r in test_data:
            self.assertEqual(_f(_v), _r)
    def test_num(self):
        # (value, func, result)
        test_data = [
            ('33', num(int, 5, 120), 33),
            ('234', num(int, min=10), 234),
            ('50.222', num(float, 10, 120), 50.222),
        ]

        for _v, _f, _r in test_data:
            self.assertEqual(_f(_v), _r)
    def test_num_error(self):
        with self.assertRaises(ArgumentTypeError):
            func = num(int, 5, 10)
            func('3')

        with self.assertRaises(ArgumentTypeError):
            func = num(int, max=11)
            func('12')

        with self.assertRaises(ArgumentTypeError):
            func = num(int, min=15)
            func('8')

        with self.assertRaises(ArgumentTypeError):
            func = num(float, 10, 20)
            func('40.222')
Exemplo n.º 4
0
    def test_num_error(self):
        with self.assertRaises(ArgumentTypeError):
            func = num(int, 5, 10)
            func('3')

        with self.assertRaises(ArgumentTypeError):
            func = num(int, max=11)
            func('12')

        with self.assertRaises(ArgumentTypeError):
            func = num(int, min=15)
            func('8')

        with self.assertRaises(ArgumentTypeError):
            func = num(float, 10, 20)
            func('40.222')
Exemplo n.º 5
0
def build_parser():
    parser = ArgumentParser(
        prog="streamlink",
        fromfile_prefix_chars="@",
        formatter_class=HelpFormatter,
        add_help=False,
        usage="%(prog)s [OPTIONS] <URL> [STREAM]",
        description=dedent("""
        Streamlink is a command-line utility that extracts streams from various
        services and pipes them into a video player of choice.
        """),
        epilog=dedent("""
        For more in-depth documentation see:
          https://streamlink.github.io

        Please report broken plugins or bugs to the issue tracker on Github:
          https://github.com/streamlink/streamlink/issues
        """)
    )

    positional = parser.add_argument_group("Positional arguments")
    positional.add_argument(
        "url",
        metavar="URL",
        nargs="?",
        help="""
        A URL to attempt to extract streams from.

        Usually, the protocol of http(s) URLs can be omitted ("https://"),
        depending on the implementation of the plugin being used.

        Alternatively, the URL can also be specified by using the --url option.
        """
    )
    positional.add_argument(
        "stream",
        metavar="STREAM",
        nargs="?",
        type=comma_list,
        help="""
        Stream to play.

        Use ``best`` or ``worst`` for selecting the highest or lowest available
        quality.

        Fallback streams can be specified by using a comma-separated list:

          "720p,480p,best"

        If no stream is specified and --default-stream is not used, then a list
        of available streams will be printed.
        """
    )

    general = parser.add_argument_group("General options")
    general.add_argument(
        "-h", "--help",
        action="store_true",
        help="""
        Show this help message and exit.
        """
    )
    general.add_argument(
        "-V", "--version",
        action="version",
        version=f"%(prog)s {streamlink_version}",
        help="""
        Show version number and exit.
        """
    )
    general.add_argument(
        "--plugins",
        action="store_true",
        help="""
        Print a list of all currently installed plugins.
        """
    )
    general.add_argument(
        "--plugin-dirs",
        metavar="DIRECTORY",
        type=comma_list,
        help="""
        Attempts to load plugins from these directories.

        Multiple directories can be used by separating them with a comma.
        """
    )
    general.add_argument(
        "--can-handle-url",
        metavar="URL",
        help="""
        Check if Streamlink has a plugin that can handle the specified URL.

        Returns status code 1 for false and 0 for true.

        Useful for external scripting.
        """
    )
    general.add_argument(
        "--can-handle-url-no-redirect",
        metavar="URL",
        help="""
        Same as --can-handle-url but without following redirects when looking up
        the URL.
        """
    )
    general.add_argument(
        "--config",
        action="append",
        metavar="FILENAME",
        help="""
        Load options from this config file.

        Can be repeated to load multiple files, in which case the options are
        merged on top of each other where the last config has highest priority.
        """
    )
    general.add_argument(
        "-l", "--loglevel",
        metavar="LEVEL",
        choices=logger.levels,
        default="info",
        help="""
        Set the log message threshold.

        Valid levels are: none, error, warning, info, debug, trace
        """
    )
    general.add_argument(
        "--logfile",
        metavar="FILE",
        help="""
        Append log output to FILE instead of writing to stdout/stderr.

        User prompts and download progress won't be written to FILE.

        A value of ``-`` will set the file name to an ISO8601-like string
        and will choose the following default log directories.

        Windows:

          %%TEMP%%\\streamlink\\logs

        macOS:

          ${HOME}/Library/Logs/streamlink

        Linux/BSD:

          ${XDG_STATE_HOME:-${HOME}/.local/state}/streamlink/logs
        """
    )
    general.add_argument(
        "-Q", "--quiet",
        action="store_true",
        help="""
        Hide all log output.

        Alias for "--loglevel none".
        """
    )
    general.add_argument(
        "-j", "--json",
        action="store_true",
        help="""
        Output JSON representations instead of the normal text output.

        Useful for external scripting.
        """
    )
    general.add_argument(
        "--auto-version-check",
        type=boolean,
        metavar="{yes,true,1,on,no,false,0,off}",
        default=False,
        help="""
        Enable or disable the automatic check for a new version of Streamlink.

        Default is "no".
        """
    )
    general.add_argument(
        "--version-check",
        action="store_true",
        help="""
        Runs a version check and exits.
        """
    )
    general.add_argument(
        "--locale",
        type=str,
        metavar="LOCALE",
        help="""
        The preferred locale setting, for selecting the preferred subtitle and
        audio language.

        The locale is formatted as [language_code]_[country_code], eg. en_US or
        es_ES.

        Default is system locale.
        """
    )
    general.add_argument(
        "--interface",
        type=str,
        metavar="INTERFACE",
        help="""
        Set the network interface.
        """
    )
    general.add_argument(
        "-4", "--ipv4",
        action="store_true",
        help="""
        Resolve address names to IPv4 only. This option overrides :option:`-6`.
        """
    )
    general.add_argument(
        "-6", "--ipv6",
        action="store_true",
        help="""
        Resolve address names to IPv6 only. This option overrides :option:`-4`.
        """
    )

    player = parser.add_argument_group("Player options")
    player.add_argument(
        "-p", "--player",
        metavar="COMMAND",
        default=find_default_player(),
        help="""
        Player to feed stream data to. By default, VLC will be used if it can be
        found in its default location.

        This is a shell-like syntax to support using a specific player:

          %(prog)s --player=vlc <url> [stream]

        Absolute or relative paths can also be passed via this option in the
        event the player's executable can not be resolved:

          %(prog)s --player=/path/to/vlc <url> [stream]
          %(prog)s --player=./vlc-player/vlc <url> [stream]

        To use a player that is located in a path with spaces you must quote the
        parameter or its value:

          %(prog)s "--player=/path/with spaces/vlc" <url> [stream]
          %(prog)s --player "C:\\path\\with spaces\\mpc-hc64.exe" <url> [stream]

        Options may also be passed to the player. For example:

          %(prog)s --player "vlc --file-caching=5000" <url> [stream]

        As an alternative to this, see the --player-args parameter, which does
        not log any custom player arguments.
        """
    )
    player.add_argument(
        "-a", "--player-args",
        metavar="ARGUMENTS",
        default="",
        help=f"""
        This option allows you to customize the default arguments which are put
        together with the value of --player to create a command to execute.

        It's usually enough to only use --player instead of this unless you need
        to add arguments after the player's input argument or if you don't want
        any of the player arguments to be logged.

        The value can contain formatting variables surrounded by curly braces,
        {{ and }}. If you need to include a brace character, it can be escaped
        by doubling, e.g. {{{{ and }}}}.

        Formatting variables available:

        {{{PLAYER_ARGS_INPUT_DEFAULT}}}
            This is the input that the player will use. For standard input (stdin),
            it is ``-``, but it can also be a URL, depending on the options used.

        {{{PLAYER_ARGS_INPUT_FALLBACK}}}
            The old fallback variable name with the same functionality.

        Example:

          %(prog)s -p vlc -a "--play-and-exit {{{PLAYER_ARGS_INPUT_DEFAULT}}}" <url> [stream]

        Note: When neither of the variables are found, ``{{{PLAYER_ARGS_INPUT_DEFAULT}}}``
        will be appended to the whole parameter value, to ensure that the player
        always receives an input argument.
        """
    )
    player.add_argument(
        "-v", "--verbose-player",
        action="store_true",
        help="""
        Allow the player to display its console output.
        """
    )
    player.add_argument(
        "-n", "--player-fifo", "--fifo",
        action="store_true",
        help="""
        Make the player read the stream through a named pipe instead of the
        stdin pipe.
        """
    )
    player.add_argument(
        "--player-http",
        action="store_true",
        help="""
        Make the player read the stream through HTTP instead of the stdin pipe.
        """
    )
    player.add_argument(
        "--player-continuous-http",
        action="store_true",
        help="""
        Make the player read the stream through HTTP, but unlike --player-http
        it will continuously try to open the stream if the player requests it.

        This makes it possible to handle stream disconnects if your player is
        capable of reconnecting to a HTTP stream. This is usually done by
        setting your player to a "repeat mode".
        """
    )
    player.add_argument(
        "--player-external-http",
        action="store_true",
        help="""
        Serve stream data through HTTP without running any player. This is
        useful to allow external devices like smartphones or streaming boxes to
        watch streams they wouldn't be able to otherwise.

        Behavior will be similar to the continuous HTTP option, but no player
        program will be started, and the server will listen on all available
        connections instead of just in the local (loopback) interface.

        The URLs that can be used to access the stream will be printed to the
        console, and the server can be interrupted using CTRL-C.
        """
    )
    player.add_argument(
        "--player-external-http-port",
        metavar="PORT",
        type=num(int, min=0, max=65535),
        default=0,
        help="""
        A fixed port to use for the external HTTP server if that mode is
        enabled. Omit or set to 0 to use a random high ( >1024) port.
        """
    )
    player.add_argument(
        "--player-passthrough",
        metavar="TYPES",
        type=comma_list_filter(STREAM_PASSTHROUGH),
        default=[],
        help="""
        A comma-delimited list of stream types to pass to the player as a URL to
        let it handle the transport of the stream instead.

        Stream types that can be converted into a playable URL are:

        - {0}

        Make sure your player can handle the stream type when using this.
        """.format("\n        - ".join(STREAM_PASSTHROUGH))
    )
    player.add_argument(
        "--player-no-close",
        action="store_true",
        help="""
        By default Streamlink will close the player when the stream
        ends. This is to avoid "dead" GUI players lingering after a
        stream ends.

        It does however have the side-effect of sometimes closing a
        player before it has played back all of its cached data.

        This option will instead let the player decide when to exit.
        """
    )
    player.add_argument(
        "-t", "--title",
        metavar="TITLE",
        help="""
        This option allows you to supply a title to be displayed in the
        title bar of the window that the video player is launched in.

        This value can contain formatting variables surrounded by curly braces,
        {{ and }}. If you need to include a brace character, it can be escaped
        by doubling, e.g. {{{{ and }}}}.

        This option is only supported for the following players: {0}.

        VLC specific information:
            VLC has certain codes you can use inside your title.
            These are accessible inside --title by using a backslash
            before the dollar sign VLC uses to denote a format character.

            e.g. to put the current date in your VLC window title,
            the string "\\$A" could be inserted inside your --title string.

            A full list of the format codes VLC uses is available here:
            https://wiki.videolan.org/Documentation:Format_String/

        mpv specific information:
            mpv has certain codes you can use inside your title.
            These are accessible inside --title by using a backslash
            before the dollar sign mpv uses to denote a format character.

            e.g. to put the current version of mpv running inside your
            mpv window title, the string "\\${{{{mpv-version}}}}" could be
            inserted inside your --title string.

            A full list of the format codes mpv uses is available here:
            https://mpv.io/manual/stable/#property-list

        Formatting variables available to use in --title:

        {{title}}
            If available, this is the title of the stream.
            Otherwise, it is the string "{1}"

        {{author}}
            If available, this is the author of the stream.
            Otherwise, it is the string "{2}"

        {{category}}
            If available, this is the category the stream has been placed into.

            - For Twitch, this is the game being played
            - For YouTube, it's the category e.g. Gaming, Sports, Music...

            Otherwise, it is the string "{3}"

        {{game}}
            This is just a synonym for {{category}} which may make more sense for
            gaming oriented platforms. "Game being played" is a way to categorize
            the stream, so it doesn't need its own separate handling.

        {{url}}
            URL of the stream.

        Examples:

            %(prog)s -p vlc --title "{{title}} -!- {{author}} -!- {{category}} \\$A" <url> [stream]
            %(prog)s -p mpv --title "{{title}} -- {{author}} -- {{category}} -- (\\${{{{mpv-version}}}})" <url> [stream]

        """.format(', '.join(sorted(SUPPORTED_PLAYERS.keys())),
                   DEFAULT_STREAM_METADATA['title'],
                   DEFAULT_STREAM_METADATA['author'],
                   DEFAULT_STREAM_METADATA['category'])
    )

    output = parser.add_argument_group("File output options")
    output.add_argument(
        "-o", "--output",
        metavar="FILENAME",
        help="""
        Write stream data to FILENAME instead of playing it.

        You will be prompted if the file already exists.
        """
    )
    output.add_argument(
        "-f", "--force",
        action="store_true",
        help="""
        When using -o or -r, always write to file even if it already exists.
        """
    )
    output.add_argument(
        "--force-progress",
        action="store_true",
        help="""
        When using -o or -r,
        show the download progress bar even if there is no terminal.
        """
    )
    output.add_argument(
        "-O", "--stdout",
        action="store_true",
        help="""
        Write stream data to stdout instead of playing it.
        """
    )
    output.add_argument(
        "-r", "--record",
        metavar="FILENAME",
        help="""
        Open the stream in the player, while at the same time writing it to FILENAME.

        You will be prompted if the file already exists.
        """
    )
    output.add_argument(
        "-R", "--record-and-pipe",
        metavar="FILENAME",
        help="""
        Write stream data to stdout, while at the same time writing it to FILENAME.

        You will be prompted if the file already exists.
        """
    )

    stream = parser.add_argument_group("Stream options")
    stream.add_argument(
        "--url",
        dest="url_param",
        metavar="URL",
        help="""
        A URL to attempt to extract streams from.

        Usually, the protocol of http(s) URLs can be omitted (https://),
        depending on the implementation of the plugin being used.

        This is an alternative to setting the URL using a positional argument
        and can be useful if set in a config file.
        """
    )
    stream.add_argument(
        "--default-stream",
        type=comma_list,
        metavar="STREAM",
        help="""
        Stream to play.

        Use ``best`` or ``worst`` for selecting the highest or lowest available
        quality.

        Fallback streams can be specified by using a comma-separated list:

          "720p,480p,best"

        This is an alternative to setting the stream using a positional argument
        and can be useful if set in a config file.
        """
    )
    stream.add_argument(
        "--stream-url",
        action="store_true",
        help="""
        If possible, translate the resolved stream to a URL and print it.
        """
    )
    stream.add_argument(
        "--retry-streams",
        metavar="DELAY",
        type=num(float, min=0),
        help="""
        Retry fetching the list of available streams until streams are found
        while waiting DELAY second(s) between each attempt. If unset, only one
        attempt will be made to fetch the list of streams available.

        The number of fetch retry attempts can be capped with --retry-max.
        """
    )
    stream.add_argument(
        "--retry-max",
        metavar="COUNT",
        type=num(int, min=-1),
        help="""
        When using --retry-streams, stop retrying the fetch after COUNT retry
        attempt(s). Fetch will retry infinitely if COUNT is zero or unset.

        If --retry-max is set without setting --retry-streams, the delay between
        retries will default to 1 second.
        """
    )
    stream.add_argument(
        "--retry-open",
        metavar="ATTEMPTS",
        type=num(int, min=0),
        default=1,
        help="""
        After a successful fetch, try ATTEMPTS time(s) to open the stream until
        giving up.

        Default is 1.
        """
    )
    stream.add_argument(
        "--stream-types", "--stream-priority",
        metavar="TYPES",
        type=comma_list,
        help="""
        A comma-delimited list of stream types to allow.

        The order will be used to separate streams when there are multiple
        streams with the same name but different stream types. Any stream type
        not listed will be omitted from the available streams list.  A ``*`` can
        be used as a wildcard to match any other type of stream, eg. muxed-stream.

        Default is "rtmp,hls,hds,http,akamaihd,*".
        """
    )
    stream.add_argument(
        "--stream-sorting-excludes",
        metavar="STREAMS",
        type=comma_list,
        help="""
        Fine tune the ``best`` and ``worst`` stream name synonyms by excluding unwanted streams.

        If all of the available streams get excluded, ``best`` and ``worst`` will become
        inaccessible and new special stream synonyms ``best-unfiltered`` and ``worst-unfiltered``
        can be used as a fallback selection method.

        Uses a filter expression in the format:

          [operator]<value>

        Valid operators are ``>``, ``>=``, ``<`` and ``<=``. If no operator is specified then
        equality is tested.

        For example this will exclude streams ranked higher than "480p":

          ">480p"

        Multiple filters can be used by separating each expression with a comma.

        For example this will exclude streams from two quality types:

          ">480p,>medium"

        """
    )

    transport = parser.add_argument_group("Stream transport options")
    transport.add_argument(
        "--hds-live-edge",
        type=num(float, min=0),
        metavar="SECONDS",
        help="""
        The time live HDS streams will start from the edge of stream.

        Default is 10.0.
        """
    )
    transport.add_argument(
        "--hds-segment-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
        help="""
        How many attempts should be done to download each HDS segment before
        giving up.

        Default is 3.
        """
    )
    transport.add_argument(
        "--hds-segment-threads",
        type=num(int, max=10),
        metavar="THREADS",
        help="""
        The size of the thread pool used to download HDS segments. Minimum value
        is 1 and maximum is 10.

        Default is 1.
        """
    )
    transport.add_argument(
        "--hds-segment-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        HDS segment connect and read timeout.

        Default is 10.0.
        """
    )
    transport.add_argument(
        "--hds-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from HDS streams.

        Default is 60.0.
        """
    )
    transport.add_argument(
        "--hls-live-edge",
        type=num(int, min=0),
        metavar="SEGMENTS",
        help="""
        How many segments from the end to start live HLS streams on.

        The lower the value the lower latency from the source you will be,
        but also increases the chance of buffering.

        Default is 3.
        """
    )
    transport.add_argument(
        "--hls-segment-stream-data",
        action="store_true",
        help="""
        Immediately write segment data into output buffer while downloading.
        """
    )
    transport.add_argument(
        "--hls-segment-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
        help="""
        How many attempts should be done to download each HLS segment before
        giving up.

        Default is 3.
        """
    )
    transport.add_argument(
        "--hls-playlist-reload-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
        help="""
        How many attempts should be done to reload the HLS playlist before
        giving up.

        Default is 3.
        """
    )
    transport.add_argument(
        "--hls-playlist-reload-time",
        metavar="TIME",
        help="""
        Set a custom HLS playlist reload time value, either in seconds
        or by using one of the following keywords:

            segment: The duration of the last segment in the current playlist
            live-edge: The sum of segment durations of the live edge value minus one
            default: The playlist's target duration metadata

        Default is default.
        """
    )
    transport.add_argument(
        "--hls-segment-threads",
        type=num(int, max=10),
        metavar="THREADS",
        help="""
        The size of the thread pool used to download HLS segments. Minimum value
        is 1 and maximum is 10.

        Default is 1.
        """
    )
    transport.add_argument(
        "--hls-segment-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        HLS segment connect and read timeout.

        Default is 10.0.
        """)
    transport.add_argument(
        "--hls-segment-ignore-names",
        metavar="NAMES",
        type=comma_list,
        help="""
        A comma-delimited list of segment names that will get filtered out.

        Example: --hls-segment-ignore-names 000,001,002

        This will ignore every segment that ends with 000.ts, 001.ts and 002.ts

        Default is None.
        """
    )
    transport.add_argument(
        "--hls-segment-key-uri",
        metavar="URI",
        type=str,
        help="""
        URI to segment encryption key. If no URI is specified, the URI contained
        in the segments will be used.

        URI can be templated using the following variables, which will be
        replaced with its respective part from the source segment URI:

          {url} {scheme} {netloc} {path} {query}

        Examples:

          --hls-segment-key-uri "https://example.com/hls/encryption_key"
          --hls-segment-key-uri "{scheme}://1.2.3.4{path}{query}"
          --hls-segment-key-uri "{scheme}://{netloc}/custom/path/to/key"

        Default is None.
        """
    )
    transport.add_argument(
        "--hls-audio-select",
        type=comma_list,
        metavar="CODE",
        help="""
        Selects a specific audio source or sources, by language code or name,
        when multiple audio sources are available. Can be * to download all
        audio sources.

        Examples:

          --hls-audio-select "English,German"
          --hls-audio-select "en,de"
          --hls-audio-select "*"

        Note: This is only useful in special circumstances where the regular
        locale option fails, such as when multiple sources of the same language
        exists.
        """)
    transport.add_argument(
        "--hls-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from HLS streams.

        Default is 60.0.
        """)
    transport.add_argument(
        "--hls-start-offset",
        type=hours_minutes_seconds,
        metavar="[HH:]MM:SS",
        default=None,
        help="""
        Amount of time to skip from the beginning of the stream. For live
        streams, this is a negative offset from the end of the stream (rewind).

        Default is 00:00:00.
        """)
    transport.add_argument(
        "--hls-duration",
        type=hours_minutes_seconds,
        metavar="[HH:]MM:SS",
        default=None,
        help="""
        Limit the playback duration, useful for watching segments of a stream.
        The actual duration may be slightly longer, as it is rounded to the
        nearest HLS segment.

        Default is unlimited.
        """)
    transport.add_argument(
        "--hls-live-restart",
        action="store_true",
        help="""
        Skip to the beginning of a live stream, or as far back as possible.
        """)
    transport.add_argument(
        "--http-stream-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from HTTP streams.

        Default is 60.0.
        """)
    transport.add_argument(
        "--ringbuffer-size",
        metavar="SIZE",
        type=filesize,
        help="""
        The maximum size of ringbuffer. Add a M or K suffix to specify mega or
        kilo bytes instead of bytes.

        The ringbuffer is used as a temporary storage between the stream and the
        player. This is to allows us to download the stream faster than the
        player wants to read it.

        The smaller the size, the higher chance of the player buffering if there
        are download speed dips and the higher size the more data we can use as
        a storage to catch up from speed dips.

        It also allows you to temporary pause as long as the ringbuffer doesn't
        get full since we continue to download the stream in the background.

        Default is "16M".

        Note: A smaller size is recommended on lower end systems (such as
        Raspberry Pi) when playing stream types that require some extra
        processing (such as HDS) to avoid unnecessary background processing.
        """)
    transport.add_argument(
        "--rtmp-proxy",
        metavar="PROXY",
        help="""
        A SOCKS proxy that RTMP streams will use.

        Example: 127.0.0.1:9050
        """
    )
    transport.add_argument(
        "--rtmp-rtmpdump",
        metavar="FILENAME",
        help="""
        RTMPDump is used to access RTMP streams. You can specify the
        location of the rtmpdump executable if it is not in your PATH.

        Example: "/usr/local/bin/rtmpdump"
        """
    )
    transport.add_argument("--rtmpdump", help=argparse.SUPPRESS)
    transport.add_argument(
        "--rtmp-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from RTMP streams.

        Default is 60.0.
        """
    )
    transport.add_argument(
        "--stream-segment-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
        help="""
        How many attempts should be done to download each segment before giving
        up.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 3.
        """
    )
    transport.add_argument(
        "--stream-segment-threads",
        type=num(int, max=10),
        metavar="THREADS",
        help="""
        The size of the thread pool used to download segments. Minimum value is
        1 and maximum is 10.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 1.
        """
    )
    transport.add_argument(
        "--stream-segment-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Segment connect and read timeout.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 10.0.
        """)
    transport.add_argument(
        "--stream-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from streams.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 60.0.
        """)
    transport.add_argument(
        "--subprocess-cmdline",
        action="store_true",
        help="""
        Print the command-line used internally to play the stream.

        This is only available on RTMP streams.
        """
    )
    transport.add_argument(
        "--subprocess-errorlog",
        action="store_true",
        help="""
        Log possible errors from internal subprocesses to a temporary file. The
        file will be saved in your systems temporary directory.

        Useful when debugging rtmpdump related issues.
        """
    )
    transport.add_argument(
        "--subprocess-errorlog-path",
        type=str,
        metavar="PATH",
        help="""
        Log the subprocess errorlog to a specific file rather than a temporary
        file. Takes precedence over subprocess-errorlog.

        Useful when debugging rtmpdump related issues.
        """
    )
    transport.add_argument(
        "--ffmpeg-ffmpeg",
        metavar="FILENAME",
        help="""
        FFMPEG is used to access or mux separate video and audio streams. You
        can specify the location of the ffmpeg executable if it is not in your
        PATH.

        Example: "/usr/local/bin/ffmpeg"
        """
    )
    transport.add_argument(
        "--ffmpeg-verbose",
        action="store_true",
        help="""
        Write the console output from ffmpeg to the console.
        """
    )
    transport.add_argument(
        "--ffmpeg-verbose-path",
        type=str,
        metavar="PATH",
        help="""
        Path to write the output from the ffmpeg console.
        """
    )
    transport.add_argument(
        "--ffmpeg-fout",
        type=str,
        metavar="OUTFORMAT",
        help="""
        When muxing streams, set the output format to OUTFORMAT.

        Default is "matroska".

        Example: "mpegts"
        """
    )
    transport.add_argument(
        "--ffmpeg-video-transcode",
        metavar="CODEC",
        help="""
        When muxing streams, transcode the video to CODEC.

        Default is "copy".

        Example: "h264"
        """
    )
    transport.add_argument(
        "--ffmpeg-audio-transcode",
        metavar="CODEC",
        help="""
        When muxing streams, transcode the audio to CODEC.

        Default is "copy".

        Example: "aac"
        """
    )
    transport.add_argument(
        "--ffmpeg-copyts",
        action="store_true",
        help="""
        Forces the -copyts ffmpeg option and does not remove
        the initial start time offset value.
        """
    )
    transport.add_argument(
        "--ffmpeg-start-at-zero",
        action="store_true",
        help="""
        Enable the -start_at_zero ffmpeg option when using copyts.
        """
    )
    transport.add_argument(
        "--mux-subtitles",
        action="store_true",
        help="""
        Automatically mux available subtitles into the output stream.

        Needs to be supported by the used plugin.
        """
    )

    http = parser.add_argument_group("HTTP options")
    http.add_argument(
        "--http-proxy",
        metavar="HTTP_PROXY",
        help="""
        A HTTP proxy to use for all HTTP requests, including WebSocket connections.
        By default this proxy will be used for all HTTPS requests too.

        Example: "http://hostname:port/"
        """
    )
    http.add_argument(
        "--https-proxy",
        metavar="HTTPS_PROXY",
        help="""
        A HTTPS capable proxy to use for all HTTPS requests.

        Example: "https://hostname:port/"
        """
    )
    http.add_argument(
        "--http-cookie",
        metavar="KEY=VALUE",
        type=keyvalue,
        action="append",
        help="""
        A cookie to add to each HTTP request.

        Can be repeated to add multiple cookies.
        """
    )
    http.add_argument(
        "--http-header",
        metavar="KEY=VALUE",
        type=keyvalue,
        action="append",
        help="""
        A header to add to each HTTP request.

        Can be repeated to add multiple headers.
        """
    )
    http.add_argument(
        "--http-query-param",
        metavar="KEY=VALUE",
        type=keyvalue,
        action="append",
        help="""
        A query parameter to add to each HTTP request.

        Can be repeated to add multiple query parameters.
        """
    )
    http.add_argument(
        "--http-ignore-env",
        action="store_true",
        help="""
        Ignore HTTP settings set in the environment such as environment
        variables (HTTP_PROXY, etc) or ~/.netrc authentication.
        """
    )
    http.add_argument(
        "--http-no-ssl-verify",
        action="store_true",
        help="""
        Don't attempt to verify SSL certificates.

        Usually a bad idea, only use this if you know what you're doing.
        """
    )
    http.add_argument(
        "--http-disable-dh",
        action="store_true",
        help="""
        Disable Diffie Hellman key exchange

        Usually a bad idea, only use this if you know what you're doing.
        """
    )
    http.add_argument(
        "--http-ssl-cert",
        metavar="FILENAME",
        help="""
        SSL certificate to use.

        Expects a .pem file.
        """
    )
    http.add_argument(
        "--http-ssl-cert-crt-key",
        metavar=("CRT_FILENAME", "KEY_FILENAME"),
        nargs=2,
        help="""
        SSL certificate to use.

        Expects a .crt and a .key file.
        """
    )
    http.add_argument(
        "--http-timeout",
        metavar="TIMEOUT",
        type=num(float, min=0),
        help="""
        General timeout used by all HTTP requests except the ones covered by
        other options.

        Default is 20.0.
        """
    )

    return parser
Exemplo n.º 6
0
class HLSSessionPlugin(Plugin):
    _url_re = re.compile(r'(hlssession://)(.+(?:\.m3u8)?.*)')

    arguments = PluginArguments(
        PluginArgument(
            'ignore_number',
            # dest='hls-segment-ignore-number',
            type=num(int, min=5),
            metavar='SEGMENTS',
            help='''
            Ignore invalid segment numbers,
            this option is the max. difference between the valid and invalid number.

            If the valid segment is 100 and this option is set to 20,

            only a segment of 1-80 will be allowed and added,
            everything between 81-99 will be invalid and not added.

            Default is Disabled.
            '''
        ),
        PluginArgument(
            'segment',
            # dest='hls-session-reload-segment',
            action='store_true',
            help='''
            Reloads a Streamlink session, if the playlist reload fails twice.

            Default is False.

            Note: This command is meant as a fallback for --hlssession-time
            if the time is set incorrectly, it might not work for every stream.
            '''
        ),
        PluginArgument(
            'time',
            # dest='hls-session-reload-time',
            type=hours_minutes_seconds,
            metavar='HH:MM:SS',
            default=None,
            help='''
            Reloads a Streamlink session after the given time.

            Useful for playlists that expire after an amount of time.

            Default is Disabled.

            Note: --hlssession-ignore-number can be used for new playlists
            that contain different segment numbers
            '''
        ),
    )

    @classmethod
    def can_handle_url(cls, url):
        return cls._url_re.match(url)

    def _get_streams(self):
        self.session.http.headers.update({'User-Agent': useragents.FIREFOX})
        log.debug('Version 2018-07-12')
        log.info('This is a custom plugin. '
                 'For support visit https://github.com/back-to/plugins')

        url, params = parse_url_params(self.url)
        urlnoproto = self._url_re.match(url).group(2)
        urlnoproto = update_scheme('http://', urlnoproto)

        if not hasattr(TempData, 'sequence_ignore_number'):
            TempData.sequence_ignore_number = (
                HLSSessionPlugin.get_option('ignore_number')
                or 0)

        if not hasattr(TempData, 'session_reload_segment'):
            TempData.session_reload_segment = (
                HLSSessionPlugin.get_option('segment')
                or False)
        if not hasattr(TempData, 'session_reload_segment_status'):
            TempData.session_reload_segment_status = False

        if not hasattr(TempData, 'session_reload_time'):
            TempData.session_reload_time = int(
                HLSSessionPlugin.get_option('time')
                or 0)

        if not hasattr(TempData, 'cached_data'):
            TempData.cached_data = {}
            # set a timestamp, if it was not set previously
            if not TempData.cached_data.get('timestamp'):
                TempData.cached_data.update({'timestamp': int(time())})

            TempData.cached_data.update({'stream_name': 'best'})
            TempData.cached_data.update({'url': urlnoproto})

        streams = self.session.streams(
            urlnoproto, stream_types=['hls'])

        if not streams:
            log.debug('No stream found for hls-session-reload,'
                      ' stream is not available.')
            return

        stream = streams['best']
        urlnoproto = stream.url

        self.logger.debug('URL={0}; params={1}', urlnoproto, params)
        streams = HLSSessionHLSStream.parse_variant_playlist(self.session, urlnoproto, **params)
        if not streams:
            return {'live': HLSSessionHLSStream(self.session, urlnoproto, **params)}
        else:
            return streams
Exemplo n.º 7
0
class Generic(Plugin):
    # iframes
    _iframe_re = re.compile(r'''(?isx)
        <ifr(?:["']\s?\+\s?["'])?ame
        (?!\sname=["']g_iFrame).*?src=
        ["'](?P<url>[^"'\s<>]+)\s?["']
        [^<>]*?>
    ''')
    # playlists
    _playlist_re = re.compile(r'''(?sx)
        (?:["']|=|&quot;)(?P<url>
            (?<!title=["'])
            (?<!["']title["']:["'])
                [^"'<>\s\;{}]+\.(?:m3u8|mp3|mp4|mpd)
            (?:\?[^"'<>\s\\{}]+)?)/?
        (?:\\?["']|(?<!;)\s|>|\\&quot;)
    ''')
    # mp3 and mp4 files
    _httpstream_bitrate_re = re.compile(r'''(?x)
        (?:_|\.|/|-)
        (?:
            (?P<bitrate>\d{1,4})(?:k)?
            |
            (?P<resolution>\d{1,4}p)
            (?:\.h26(?:4|5))?
        )
        \.mp(?:3|4)
    ''')
    _httpstream_common_resolution_list = [
        '2160', '1440', '1080', '720', '576', '480', '360', '240',
    ]
    # javascript redirection
    _window_location_re = re.compile(r'''(?sx)
        <script[^<]+window\.location\.href\s?=\s?["']
        (?P<url>[^"']+)["'];[^<>]+
    ''')
    # obviously ad paths
    _ads_path_re = re.compile(r'''(?x)
        /ads?/?(?:\w+)?
        (?:\d+x\d+)?
        (?:_\w+)?\.(?:html?|php)$
    ''')

    # START - _make_url_list
    # Not allowed at the end of the parsed url path
    blacklist_endswith = (
        '.gif',
        '.jpg',
        '.png',
        '.svg',
        '.vtt',
        '/chat.html',
        '/chat',
        '/novideo.mp4',
        '/vidthumb.mp4',
        '/ads-iframe-display.php',
    )
    # Not allowed at the end of the parsed url netloc
    blacklist_netloc = (
        '127.0.0.1',
        'a.adtng.com',
        'about:blank',
        'abv.bg',
        'adfox.ru',
        'cbox.ws',
        'googletagmanager.com',
        'javascript:false',
        'accounts.google.com',
    )
    # END - _make_url_list

    arguments = PluginArguments(
        PluginArgument(
            'playlist-max',
            metavar='NUMBER',
            type=num(int, min=0, max=25),
            default=5,
            help='''
            Number of how many playlist URLs of the same type
            are allowed to be resolved with this plugin.

            Default is 5
            '''
        ),
        PluginArgument(
            'playlist-referer',
            metavar='URL',
            help='''
            Set a custom referer URL for the playlist URLs.

            This only affects playlist URLs of this plugin.

            Default is the URL of the last website.
            '''
        ),
        PluginArgument(
            'blacklist-netloc',
            metavar='NETLOC',
            type=comma_list,
            help='''
            Blacklist domains that should not be used,
            by using a comma-separated list:

              'example.com,localhost,google.com'

            Useful for websites with a lot of iframes.
            '''
        ),
        PluginArgument(
            'blacklist-path',
            metavar='PATH',
            type=comma_list,
            help='''
            Blacklist the path of a domain that should not be used,
            by using a comma-separated list:

              'example.com/mypath,localhost/example,google.com/folder'

            Useful for websites with different iframes of the same domain.
            '''
        ),
        PluginArgument(
            'blacklist-filepath',
            metavar='FILEPATH',
            type=comma_list,
            help='''
            Blacklist file names for iframes and playlists
            by using a comma-separated list:

              'index.html,ignore.m3u8,/ad/master.m3u8'

            Sometimes there are invalid URLs in the result list,
            this can be used to remove them.
            '''
        ),
        PluginArgument(
            'whitelist-netloc',
            metavar='NETLOC',
            type=comma_list,
            help='''
            Whitelist domains that should only be searched for iframes,
            by using a comma-separated list:

              'example.com,localhost,google.com'

            Useful for websites with lots of iframes,
            where the main iframe always has the same hosting domain.
            '''
        ),
        PluginArgument(
            'whitelist-path',
            metavar='PATH',
            type=comma_list,
            help='''
            Whitelist the path of a domain that should only be searched
            for iframes, by using a comma-separated list:

              'example.com/mypath,localhost/example,google.com/folder'

            Useful for websites with different iframes of the same domain,
            where the main iframe always has the same path.
            '''
        ),
        PluginArgument(
            'ignore-same-url',
            action='store_true',
            help='''
            Do not remove URLs from the valid list if they were already used.

            Sometimes needed as a workaround for --player-external-http issues.

            Be careful this might result in an infinity loop.
            '''
        ),
        PluginArgument(
            'ytdl-disable',
            action='store_true',
            help='''
            Disable youtube-dl fallback.
            '''
        ),
        PluginArgument(
            'ytdl-only',
            action='store_true',
            help='''
            Disable generic plugin and use only youtube-dl.
            '''
        ),
        PluginArgument(
            'debug',
            action='store_true',
            help='''
            Developer Command!

            Saves unpacked HTML code of all opened URLs to the local hard drive for easier debugging.
            '''
        ),
    )

    def __init__(self, url):
        super(Generic, self).__init__(url)
        self.url = update_scheme('http://', self.match.group('url'), force=False)
        self.html_text = ''

        # START - cache every used url and set a referer
        if hasattr(GenericCache, 'cache_url_list'):
            GenericCache.cache_url_list += [self.url]
            # set the last url as a referer
            self.referer = GenericCache.cache_url_list[-2]
        else:
            GenericCache.cache_url_list = [self.url]
            self.referer = self.url
        self.session.http.headers.update({'Referer': self.referer})
        # END

        # START - how often _get_streams already run
        self._run = len(GenericCache.cache_url_list)
        # END

    def compare_url_path(self, parsed_url, check_list,
                         path_status='startswith'):
        status = False
        for netloc, path in check_list:
            if path_status == '==':
                if (parsed_url.netloc.endswith(netloc) and parsed_url.path == path):
                    status = True
                    break
            elif path_status == 'startswith':
                if (parsed_url.netloc.endswith(netloc) and parsed_url.path.startswith(path)):
                    status = True
                    break

        return status

    def merge_path_list(self, static, user):
        for _path_url in user:
            if not _path_url.startswith(('http', '//')):
                _path_url = update_scheme('http://', _path_url, force=False)
            _parsed_path_url = urlparse(_path_url)
            if _parsed_path_url.netloc and _parsed_path_url.path:
                static += [(_parsed_path_url.netloc, _parsed_path_url.path)]
        return static

    def repair_url(self, url, base_url, stream_base=''):
        # remove \
        new_url = url.replace('\\', '')
        # repairs broken scheme
        if new_url.startswith('http&#58;//'):
            new_url = 'http:' + new_url[9:]
        elif new_url.startswith('https&#58;//'):
            new_url = 'https:' + new_url[10:]
        new_url = unquote(new_url)
        # creates a valid url from path only urls
        # and adds missing scheme for // urls
        if stream_base and new_url[1] != '/':
            if new_url[0] == '/':
                new_url = new_url[1:]
            new_url = urljoin(stream_base, new_url)
        else:
            new_url = urljoin(base_url, new_url)
        return new_url

    def _make_url_list(self, old_list, base_url, url_type=''):
        # START - List for not allowed URL Paths
        # --generic-blacklist-path
        if not hasattr(GenericCache, 'blacklist_path'):

            # static list
            blacklist_path = [
                ('facebook.com', '/connect'),
                ('facebook.com', '/plugins'),
                ('google.com', '/recaptcha/'),
                ('haber7.com', '/radyohome/station-widget/'),
                ('static.tvr.by', '/upload/video/atn/promo'),
                ('twitter.com', '/widgets'),
                ('vesti.ru', '/native_widget.html'),
                ('www.blogger.com', '/static'),
                ('youtube.com', '/['),
            ]

            # merge user and static list
            blacklist_path_user = self.get_option('blacklist_path')
            if blacklist_path_user is not None:
                blacklist_path = self.merge_path_list(
                    blacklist_path, blacklist_path_user)

            GenericCache.blacklist_path = blacklist_path
        # END

        blacklist_path_same = [
            ('player.vimeo.com', '/video/'),
            ('youtube.com', '/embed/'),
        ]

        # START - List of only allowed URL Paths for Iframes
        # --generic-whitelist-path
        if not hasattr(GenericCache, 'whitelist_path'):
            whitelist_path = []
            whitelist_path_user = self.get_option('whitelist_path')
            if whitelist_path_user is not None:
                whitelist_path = self.merge_path_list(
                    [], whitelist_path_user)
            GenericCache.whitelist_path = whitelist_path
        # END

        allow_same_url = (self.get_option('ignore_same_url'))

        new_list = []
        for url in old_list:
            new_url = self.repair_url(url, base_url)
            # parse the url
            parse_new_url = urlparse(new_url)

            # START
            REMOVE = False
            if new_url in GenericCache.cache_url_list and not allow_same_url:
                # Removes an already used url
                # ignored if --hls-session-reload is used
                REMOVE = 'SAME-URL'
            elif (not parse_new_url.scheme.startswith(('http'))):
                # Allow only an url with a valid scheme
                REMOVE = 'SCHEME'
            elif (url_type == 'iframe'
                    and self.get_option('whitelist_netloc')
                    and parse_new_url.netloc.endswith(tuple(self.get_option('whitelist_netloc'))) is False):
                # Allow only whitelisted domains for iFrames
                # --generic-whitelist-netloc
                REMOVE = 'WL-netloc'
            elif (url_type == 'iframe'
                    and GenericCache.whitelist_path
                    and self.compare_url_path(parse_new_url, GenericCache.whitelist_path) is False):
                # Allow only whitelisted paths from a domain for iFrames
                # --generic-whitelist-path
                REMOVE = 'WL-path'
            elif (parse_new_url.netloc.endswith(self.blacklist_netloc)):
                # Removes blacklisted domains from a static list
                # self.blacklist_netloc
                REMOVE = 'BL-static'
            elif (self.get_option('blacklist_netloc')
                  and parse_new_url.netloc.endswith(tuple(self.get_option('blacklist_netloc')))):
                # Removes blacklisted domains
                # --generic-blacklist-netloc
                REMOVE = 'BL-netloc'
            elif (self.compare_url_path(parse_new_url, GenericCache.blacklist_path) is True):
                # Removes blacklisted paths from a domain
                # --generic-blacklist-path
                REMOVE = 'BL-path'
            elif (parse_new_url.path.endswith(self.blacklist_endswith)):
                # Removes unwanted endswith images and chatrooms
                REMOVE = 'BL-ew'
            elif (self.get_option('blacklist_filepath')
                  and parse_new_url.path.endswith(tuple(self.get_option('blacklist_filepath')))):
                # Removes blacklisted file paths
                # --generic-blacklist-filepath
                REMOVE = 'BL-filepath'
            elif (self._ads_path_re.search(parse_new_url.path) or parse_new_url.netloc.startswith(('ads.'))):
                # Removes obviously AD URL
                REMOVE = 'ADS'
            elif (self.compare_url_path(parse_new_url, blacklist_path_same, path_status='==') is True):
                # Removes blacklisted same paths from a domain
                REMOVE = 'BL-path-same'
            elif parse_new_url.netloc == 'cdn.embedly.com' and parse_new_url.path == '/widgets/media.html':
                # do not use the direct URL for 'cdn.embedly.com', search the query for a new URL
                params = dict(parse_qsl(parse_new_url.query))
                embedly_new_url = params.get('url') or params.get('src')
                if embedly_new_url:
                    new_list += [embedly_new_url]
                else:
                    log.error('Missing params URL or SRC for {0}'.format(new_url))
                continue
            else:
                # valid URL
                new_list += [new_url]
                continue

            log.debug('{0} - Removed: {1}'.format(REMOVE, new_url))
            # END

        # Remove duplicates
        log.debug('List length: {0} (with duplicates)'.format(len(new_list)))
        new_list = sorted(list(set(new_list)))
        return new_list

    def _window_location(self):
        match = self._window_location_re.search(self.html_text)
        if match:
            temp_url = urljoin(self.url, match.group('url'))
            if temp_url not in GenericCache.cache_url_list:
                log.debug('Found window_location: {0}'.format(temp_url))
                return temp_url

        log.trace('No window_location')
        return False

    def _resolve_playlist(self, playlist_all):
        playlist_referer = self.get_option('playlist_referer') or self.url
        self.session.http.headers.update({'Referer': playlist_referer})

        playlist_max = self.get_option('playlist_max') or 5
        count_playlist = {
            'dash': 0,
            'hls': 0,
            'http': 0,
        }

        o = urlparse(self.url)
        origin_tuple = (
            '.cloudfront.net',
        )

        for url in playlist_all:
            parsed_url = urlparse(url)
            if parsed_url.netloc.endswith(origin_tuple):
                self.session.http.headers.update({
                    'Origin': '{0}://{1}'.format(o.scheme, o.netloc),
                })

            if (parsed_url.path.endswith(('.m3u8'))
                    or parsed_url.query.endswith(('.m3u8'))):
                if count_playlist['hls'] >= playlist_max:
                    log.debug('Skip - {0}'.format(url))
                    continue
                try:
                    streams = HLSStream.parse_variant_playlist(self.session, url).items()
                    if not streams:
                        yield 'live', HLSStream(self.session, url)
                    for s in streams:
                        yield s
                    log.debug('HLS URL - {0}'.format(url))
                    count_playlist['hls'] += 1
                except Exception as e:
                    log.error('Skip HLS with error {0}'.format(str(e)))
            elif (parsed_url.path.endswith(('.mp3', '.mp4'))
                    or parsed_url.query.endswith(('.mp3', '.mp4'))):
                if count_playlist['http'] >= playlist_max:
                    log.debug('Skip - {0}'.format(url))
                    continue
                try:
                    name = 'vod'
                    m = self._httpstream_bitrate_re.search(url)
                    if m:
                        bitrate = m.group('bitrate')
                        resolution = m.group('resolution')
                        if bitrate:
                            if bitrate in self._httpstream_common_resolution_list:
                                name = '{0}p'.format(m.group('bitrate'))
                            else:
                                name = '{0}k'.format(m.group('bitrate'))
                        elif resolution:
                            name = resolution
                    yield name, HTTPStream(self.session, url)
                    log.debug('HTTP URL - {0}'.format(url))
                    count_playlist['http'] += 1
                except Exception as e:
                    log.error('Skip HTTP with error {0}'.format(str(e)))
            elif (parsed_url.path.endswith(('.mpd'))
                    or parsed_url.query.endswith(('.mpd'))):
                if count_playlist['dash'] >= playlist_max:
                    log.debug('Skip - {0}'.format(url))
                    continue
                try:
                    for s in DASHStream.parse_manifest(self.session,
                                                       url).items():
                        yield s
                    log.debug('DASH URL - {0}'.format(url))
                    count_playlist['dash'] += 1
                except Exception as e:
                    log.error('Skip DASH with error {0}'.format(str(e)))
            else:
                log.error('parsed URL - {0}'.format(url))

    def _res_text(self, url):
        try:
            res = self.session.http.get(url, allow_redirects=True)
        except Exception as e:
            if 'Received response with content-encoding: gzip' in str(e):
                headers = {
                    'User-Agent': useragents.FIREFOX,
                    'Accept-Encoding': 'deflate'
                }
                res = self.session.http.get(url, headers=headers, allow_redirects=True)
            elif '403 Client Error' in str(e):
                log.error('Website Access Denied/Forbidden, you might be geo-'
                          'blocked or other params are missing.')
                raise NoStreamsError(self.url)
            elif '404 Client Error' in str(e):
                log.error('Website was not found, the link is broken or dead.')
                raise NoStreamsError(self.url)
            else:
                raise e

        if res.history:
            for resp in res.history:
                log.debug('Redirect: {0} - {1}'.format(resp.status_code, resp.url))
            log.debug('URL: {0}'.format(res.url))
        return res.text

    def get_author(self):
        parsed = urlparse(self.url)
        split_username = list(filter(None, parsed.path.split('/')))
        if len(split_username) == 1:
            return split_username[0]
        elif parsed.fragment:
            return parsed.fragment
        return super().get_author()

    def get_title(self):
        if self.title is None:
            if not self.html_text:
                self.html_text = self._res_text(self.url)
            _og_title_re = re.compile(r'<meta\s*property="og:title"\s*content="(?P<title>[^<>]+)"\s*/?>')
            _title_re = re.compile(r'<title[^<>]*>(?P<title>[^<>]+)</title>')
            m = _og_title_re.search(self.html_text) or _title_re.search(self.html_text)
            if m:
                self.title = re.sub(r'[\s]+', ' ', m.group('title'))
                self.title = re.sub(r'^\s*|\s*$', '', self.title)
                self.title = html_unescape(self.title)
            if self.title is None:
                # fallback if there is no <title>
                self.title = self.url
        return self.title

    def ytdl_fallback(self):
        '''Basic support for m3u8 URLs with youtube-dl'''
        log.debug(f'Fallback {youtube_dl.__name__} {youtube_dl.version.__version__}')

        class YTDL_Logger(object):
            def debug(self, msg):
                log.debug(msg)

            def warning(self, msg):
                log.warning(msg)

            def error(self, msg):
                log.trace(msg)

        ydl_opts = {
            'call_home': False,
            'forcejson': True,
            'logger': YTDL_Logger(),
            'no_color': True,
            'noplaylist': True,
            'no_warnings': True,
            'verbose': False,
            'quiet': True,
        }

        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            try:
                info = ydl.extract_info(self.url, download=False)
            except Exception:
                return

            if not info or not info.get('formats'):
                return

        self.title = info['title']

        streams = []
        for stream in info['formats']:
            if stream['protocol'] in ['m3u8', 'm3u8_native'] and stream['ext'] == 'mp4':
                log.trace('{0!r}'.format(stream))
                name = stream.get('height') or stream.get('width')
                if name:
                    name = '{0}p'.format(name)
                    streams.append((name, HLSStream(self.session,
                                                    stream['url'],
                                                    headers=stream['http_headers'])))

        if not streams:
            if ('youtube.com' in self.url
                    and info.get('requested_formats')
                    and len(info.get('requested_formats')) == 2
                    and MuxedStream.is_usable(self.session)):
                audio_url = audio_format = video_url = video_format = video_name = None
                for stream in info.get('requested_formats'):
                    if stream.get('format_id') == '135':
                        url = stream.get('manifest_url')
                        if not url:
                            return
                        return DASHStream.parse_manifest(self.session, url).items()
                    if not stream.get('height'):
                        audio_url = stream.get('url')
                        audio_format = stream.get('format_id')
                    if stream.get('height'):
                        video_url = stream.get('url')
                        video_format = stream.get('format_id')
                        video_name = '{0}p'.format(stream.get('height'))

                log.debug('MuxedStream: v {video} a {audio} = {name}'.format(
                    audio=audio_format,
                    name=video_name,
                    video=video_format,
                ))
                streams.append((video_name,
                                MuxedStream(self.session,
                                            HTTPStream(self.session, video_url, headers=stream['http_headers']),
                                            HTTPStream(self.session, audio_url, headers=stream['http_headers']))
                                ))
        return streams

    def _get_streams(self):
        if HAS_YTDL and not self.get_option('ytdl-disable') and self.get_option('ytdl-only'):
            ___streams = self.ytdl_fallback()
            if ___streams and len(___streams) >= 1:
                return (s for s in ___streams)
            if self.get_option('ytdl-only'):
                return

        if self._run <= 1:
            log.info('Version {0} - https://github.com/back-to/generic'.format(GENERIC_VERSION))

        new_url = False
        log.info('  {0}. URL={1}'.format(self._run, self.url))

        # GET website content
        self.html_text = self._res_text(self.url)
        # unpack common javascript codes
        self.html_text = unpack(self.html_text)

        if self.get_option('debug'):
            _valid_filepath = re.sub(r'(?u)[^-\w.]', '', str(self.url).strip().replace(' ', '_'))
            _new_file = os.path.join(Path().absolute(),
                                     f'{self._run}_{_valid_filepath}.html')
            log.warning(f'NEW DEBUG FILE! {_new_file}')
            try:
                with open(_new_file, 'w+') as f:
                    f.write(str(self.html_text))
            except OSError:
                pass

        # Playlist URL
        playlist_all = self._playlist_re.findall(self.html_text)
        if playlist_all:
            log.debug('Found Playlists: {0}'.format(len(playlist_all)))
            playlist_list = self._make_url_list(playlist_all,
                                                self.url,
                                                url_type='playlist',
                                                )
            if playlist_list:
                log.info('Found Playlists: {0} (valid)'.format(
                    len(playlist_list)))
                return self._resolve_playlist(playlist_list)
        else:
            log.trace('No Playlists')

        # iFrame URL
        iframe_list = self._iframe_re.findall(self.html_text)
        if iframe_list:
            log.debug('Found Iframes: {0}'.format(len(iframe_list)))
            # repair and filter iframe url list
            new_iframe_list = self._make_url_list(iframe_list,
                                                  self.url,
                                                  url_type='iframe')
            if new_iframe_list:
                number_iframes = len(new_iframe_list)
                if number_iframes == 1:
                    new_url = new_iframe_list[0]
                else:
                    log.info('--- IFRAMES ---')
                    for i, item in enumerate(new_iframe_list, start=1):
                        log.info('{0} - {1}'.format(i, item))
                    log.info('--- IFRAMES ---')

                    try:
                        number = int(self.input_ask(
                            'Choose an iframe number from above').split(' ')[0])
                        new_url = new_iframe_list[number - 1]
                    except FatalPluginError:
                        new_url = new_iframe_list[0]
                    except ValueError:
                        log.error('invalid input answer')
                    except (IndexError, TypeError):
                        log.error('invalid input number')

                    if not new_url:
                        new_url = new_iframe_list[0]
        else:
            log.trace('No iframes')

        if not new_url:
            # search for window.location.href
            new_url = self._window_location()

        if new_url:
            # the Dailymotion Plugin does not work with this Referer
            if 'dailymotion.com' in new_url:
                del self.session.http.headers['Referer']
            return self.session.streams(new_url)

        if HAS_YTDL and not self.get_option('ytdl-disable') and not self.get_option('ytdl-only'):
            ___streams = self.ytdl_fallback()
            if ___streams and len(___streams) >= 1:
                return (s for s in ___streams)

        raise NoPluginError
Exemplo n.º 8
0
def build_parser():
    global PARSER
    PARSER = ArgumentParser(fromfile_prefix_chars="@",
                            formatter_class=HelpFormatter,
                            add_help=False,
                            usage="%(prog)s [OPTIONS] <URL> [STREAM]",
                            description=dedent("""
        Streamlink is command-line utility that extracts streams from
        various services and pipes them into a video player of choice.
        """),
                            epilog=dedent("""
        For more in-depth documentation see:
        https://streamlink.github.io
        Please report broken plugins or bugs to the issue tracker on Github:
        https://github.com/streamlink/streamlink/issues
        """))

    general = PARSER.add_argument_group("General options")
    general.add_argument(
        "-h",
        "--help",
        action="store_true",
    )
    general.add_argument(
        "--locale",
        type=str,
        metavar="LOCALE",
    )
    general.add_argument(
        "-l",
        "--loglevel",
        metavar="LEVEL",
        choices=logger.levels,
        default=DEFAULT_LEVEL,
    )
    general.add_argument(
        "--interface",
        type=str,
        metavar="INTERFACE",
    )
    general.add_argument(
        "-4",
        "--ipv4",
        action="store_true",
    )
    general.add_argument(
        "-6",
        "--ipv6",
        action="store_true",
    )

    player = PARSER.add_argument_group("Player options")
    player.add_argument(
        "--player-passthrough",
        metavar="TYPES",
        type=comma_list_filter(STREAM_PASSTHROUGH),
        default=[],
    )

    http = PARSER.add_argument_group("HTTP options")
    http.add_argument(
        "--http-proxy",
        metavar="HTTP_PROXY",
    )
    http.add_argument(
        "--https-proxy",
        metavar="HTTPS_PROXY",
    )
    http.add_argument(
        "--http-cookie",
        metavar="KEY=VALUE",
        type=keyvalue,
        action="append",
    )
    http.add_argument(
        "--http-header",
        metavar="KEY=VALUE",
        type=keyvalue,
        action="append",
    )
    http.add_argument(
        "--http-query-param",
        metavar="KEY=VALUE",
        type=keyvalue,
        action="append",
    )
    http.add_argument(
        "--http-ignore-env",
        action="store_true",
    )
    http.add_argument(
        "--http-no-ssl-verify",
        action="store_true",
    )
    http.add_argument(
        "--http-disable-dh",
        action="store_true",
    )
    http.add_argument(
        "--http-ssl-cert",
        metavar="FILENAME",
    )
    http.add_argument(
        "--http-ssl-cert-crt-key",
        metavar=("CRT_FILENAME", "KEY_FILENAME"),
        nargs=2,
    )
    http.add_argument(
        "--http-timeout",
        metavar="TIMEOUT",
        type=num(float, min=0),
    )

    transport = PARSER.add_argument_group("Stream transport options")
    transport.add_argument(
        "--hds-live-edge",
        type=num(float, min=0),
        metavar="SECONDS",
    )
    transport.add_argument(
        "--hds-segment-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
    )
    transport.add_argument(
        "--hds-segment-threads",
        type=num(int, max=10),
        metavar="THREADS",
    )
    transport.add_argument(
        "--hds-segment-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
    )
    transport.add_argument(
        "--hds-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
    )
    transport.add_argument(
        "--hls-live-edge",
        type=num(int, min=0),
        metavar="SEGMENTS",
    )
    transport.add_argument(
        "--hls-segment-stream-data",
        action="store_true",
    )
    transport.add_argument(
        "--hls-segment-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
    )
    transport.add_argument(
        "--hls-playlist-reload-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
    )
    transport.add_argument(
        "--hls-playlist-reload-time",
        metavar="TIME",
    )
    transport.add_argument(
        "--hls-segment-threads",
        type=num(int, max=10),
        metavar="THREADS",
    )
    transport.add_argument(
        "--hls-segment-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
    )
    transport.add_argument(
        "--hls-segment-ignore-names",
        metavar="NAMES",
        type=comma_list,
    )
    transport.add_argument(
        "--hls-segment-key-uri",
        metavar="URI",
        type=str,
    )
    transport.add_argument(
        "--hls-audio-select",
        type=comma_list,
        metavar="CODE",
    )
    transport.add_argument(
        "--hls-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
    )
    transport.add_argument(
        "--hls-start-offset",
        type=hours_minutes_seconds,
        metavar="HH:MM:SS",
        default=None,
    )
    transport.add_argument(
        "--hls-duration",
        type=hours_minutes_seconds,
        metavar="HH:MM:SS",
        default=None,
    )
    transport.add_argument(
        "--hls-live-restart",
        action="store_true",
    )
    transport.add_argument(
        "--http-stream-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
    )
    transport.add_argument(
        "--ringbuffer-size",
        metavar="SIZE",
        type=filesize,
    )
    transport.add_argument(
        "--rtmp-proxy",
        metavar="PROXY",
    )
    transport.add_argument(
        "--rtmp-rtmpdump",
        metavar="FILENAME",
    )
    transport.add_argument("--rtmpdump", help=argparse.SUPPRESS)
    transport.add_argument(
        "--rtmp-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
    )
    transport.add_argument(
        "--stream-segment-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
    )
    transport.add_argument(
        "--stream-segment-threads",
        type=num(int, max=10),
        metavar="THREADS",
    )
    transport.add_argument(
        "--stream-segment-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
    )
    transport.add_argument(
        "--stream-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
    )
    transport.add_argument(
        "--subprocess-errorlog",
        action="store_true",
    )
    transport.add_argument(
        "--subprocess-errorlog-path",
        type=str,
        metavar="PATH",
    )
    transport.add_argument(
        "--ffmpeg-ffmpeg",
        metavar="FILENAME",
    )
    transport.add_argument(
        "--ffmpeg-verbose",
        action="store_true",
    )
    transport.add_argument(
        "--ffmpeg-verbose-path",
        type=str,
        metavar="PATH",
    )
    transport.add_argument(
        "--ffmpeg-fout",
        type=str,
        metavar="OUTFORMAT",
    )
    transport.add_argument(
        "--ffmpeg-video-transcode",
        metavar="CODEC",
    )
    transport.add_argument(
        "--ffmpeg-audio-transcode",
        metavar="CODEC",
    )
    transport.add_argument(
        "--ffmpeg-copyts",
        action="store_true",
    )
    transport.add_argument(
        "--ffmpeg-start-at-zero",
        action="store_true",
    )
    transport.add_argument(
        "--mux-subtitles",
        action="store_true",
    )

    stream = PARSER.add_argument_group("Stream options")
    stream.add_argument(
        "--stream-types",
        "--stream-priority",
        metavar="TYPES",
        type=comma_list,
    )
    stream.add_argument(
        "--stream-sorting-excludes",
        metavar="STREAMS",
        type=comma_list,
    )
    stream.add_argument(
        "--retry-open",
        metavar="ATTEMPTS",
        type=num(int, min=0),
        default=1,
    )

    return PARSER
Exemplo n.º 9
0
def build_parser():
    parser = ArgumentParser(prog="streamlink",
                            fromfile_prefix_chars="@",
                            formatter_class=HelpFormatter,
                            add_help=False,
                            usage="%(prog)s [OPTIONS] <URL> [STREAM]",
                            description=dedent("""
        Streamlink is command-line utility that extracts streams from various
        services and pipes them into a video player of choice.
        """),
                            epilog=dedent("""
        For more in-depth documentation see:
          https://streamlink.github.io

        Please report broken plugins or bugs to the issue tracker on Github:
          https://github.com/streamlink/streamlink/issues
        """))

    positional = parser.add_argument_group("Positional arguments")
    positional.add_argument("url",
                            metavar="URL",
                            nargs="?",
                            help="""
        A URL to attempt to extract streams from.

        Usually, the protocol of http(s) URLs can be omitted ("https://"),
        depending on the implementation of the plugin being used.

        Alternatively, the URL can also be specified by using the --url option.
        """)
    positional.add_argument("stream",
                            metavar="STREAM",
                            nargs="?",
                            type=comma_list,
                            help="""
        Stream to play.

        Use "best" or "worst" for selecting the highest or lowest available
        quality.

        Fallback streams can be specified by using a comma-separated list:

          "720p,480p,best"

        If no stream is specified and --default-stream is not used, then a list
        of available streams will be printed.
        """)

    general = parser.add_argument_group("General options")
    general.add_argument("-h",
                         "--help",
                         action="store_true",
                         help="""
        Show this help message and exit.
        """)
    general.add_argument("-V",
                         "--version",
                         action="version",
                         version="%(prog)s {0}".format(LIVESTREAMER_VERSION),
                         help="""
        Show version number and exit.
        """)
    general.add_argument("--plugins",
                         action="store_true",
                         help="""
        Print a list of all currently installed plugins.
        """)
    general.add_argument("--plugin-dirs",
                         metavar="DIRECTORY",
                         type=comma_list,
                         help="""
        Attempts to load plugins from these directories.

        Multiple directories can be used by separating them with a comma.
        """)
    general.add_argument("--can-handle-url",
                         metavar="URL",
                         help="""
        Check if Streamlink has a plugin that can handle the specified URL.

        Returns status code 1 for false and 0 for true.

        Useful for external scripting.
        """)
    general.add_argument("--can-handle-url-no-redirect",
                         metavar="URL",
                         help="""
        Same as --can-handle-url but without following redirects when looking up
        the URL.
        """)
    general.add_argument("--config",
                         action="append",
                         metavar="FILENAME",
                         help="""
        Load options from this config file.

        Can be repeated to load multiple files, in which case the options are
        merged on top of each other where the last config has highest priority.
        """)
    general.add_argument("-l",
                         "--loglevel",
                         metavar="LEVEL",
                         choices=logger.levels,
                         default="info",
                         help="""
        Set the log message threshold.

        Valid levels are: none, error, warning, info, debug, trace
        """)
    general.add_argument("-Q",
                         "--quiet",
                         action="store_true",
                         help="""
        Hide all log output.

        Alias for "--loglevel none".
        """)
    general.add_argument("-j",
                         "--json",
                         action="store_true",
                         help="""
        Output JSON representations instead of the normal text output.

        Useful for external scripting.
        """)
    general.add_argument("--auto-version-check",
                         type=boolean,
                         metavar="{yes,true,1,on,no,false,0,off}",
                         default=False,
                         help="""
        Enable or disable the automatic check for a new version of Streamlink.

        Default is "no".
        """)
    general.add_argument("--version-check",
                         action="store_true",
                         help="""
        Runs a version check and exits.
        """)
    general.add_argument("--locale",
                         type=str,
                         metavar="LOCALE",
                         help="""
        The preferred locale setting, for selecting the preferred subtitle and
        audio language.

        The locale is formatted as [language_code]_[country_code], eg. en_US or
        es_ES.

        Default is system locale.
        """)
    general.add_argument("--twitch-oauth-authenticate",
                         action="store_true",
                         help="""
        Open a web browser where you can grant Streamlink access to your Twitch
        account which creates a token for use with --twitch-oauth-token.
        """)

    player = parser.add_argument_group("Player options")
    player.add_argument("-p",
                        "--player",
                        metavar="COMMAND",
                        default=find_default_player(),
                        help="""
        Player to feed stream data to. By default, VLC will be used if it can be
        found in its default location.

        This is a shell-like syntax to support using a specific player:

          %(prog)s --player=vlc <url> [stream]

        Absolute or relative paths can also be passed via this option in the
        event the player's executable can not be resolved:

          %(prog)s --player=/path/to/vlc <url> [stream]
          %(prog)s --player=./vlc-player/vlc <url> [stream]

        To use a player that is located in a path with spaces you must quote the
        parameter or its value:

          %(prog)s "--player=/path/with spaces/vlc" <url> [stream]
          %(prog)s --player "C:\\path\\with spaces\\mpc-hc64.exe" <url> [stream]

        Options may also be passed to the player. For example:

          %(prog)s --player "vlc --file-caching=5000" <url> [stream]

        As an alternative to this, see the --player-args parameter, which does
        not log any custom player arguments.
        """)
    player.add_argument("-a",
                        "--player-args",
                        metavar="ARGUMENTS",
                        default=DEFAULT_PLAYER_ARGUMENTS,
                        help="""
        This option allows you to customize the default arguments which are put
        together with the value of --player to create a command to execute.
        Unlike the --player parameter, custom player arguments will not be logged.

        This value can contain formatting variables surrounded by curly braces,
        {{ and }}. If you need to include a brace character, it can be escaped
        by doubling, e.g. {{{{ and }}}}.

        Formatting variables available:

        filename
            This is the filename that the player will use. It's usually "-"
            (stdin), but can also be a URL or a file depending on the options
            used.

        It's usually enough to use --player instead of this unless you need to
        add arguments after the filename.

        Default is "{0}".

        Example:

          %(prog)s -p vlc -a "--play-and-exit {{filename}}" <url> [stream]

        """.format(DEFAULT_PLAYER_ARGUMENTS))
    player.add_argument("-v",
                        "--verbose-player",
                        action="store_true",
                        help="""
        Allow the player to display its console output.
        """)
    player.add_argument("-n",
                        "--player-fifo",
                        "--fifo",
                        action="store_true",
                        help="""
        Make the player read the stream through a named pipe instead of the
        stdin pipe.
        """)
    player.add_argument("--player-http",
                        action="store_true",
                        help="""
        Make the player read the stream through HTTP instead of the stdin pipe.
        """)
    player.add_argument("--player-continuous-http",
                        action="store_true",
                        help="""
        Make the player read the stream through HTTP, but unlike --player-http
        it will continuously try to open the stream if the player requests it.

        This makes it possible to handle stream disconnects if your player is
        capable of reconnecting to a HTTP stream. This is usually done by
        setting your player to a "repeat mode".
        """)
    player.add_argument("--player-external-http",
                        action="store_true",
                        help="""
        Serve stream data through HTTP without running any player. This is
        useful to allow external devices like smartphones or streaming boxes to
        watch streams they wouldn't be able to otherwise.

        Behavior will be similar to the continuous HTTP option, but no player
        program will be started, and the server will listen on all available
        connections instead of just in the local (loopback) interface.

        The URLs that can be used to access the stream will be printed to the
        console, and the server can be interrupted using CTRL-C.
        """)
    player.add_argument("--player-external-http-port",
                        metavar="PORT",
                        type=num(int, min=0, max=65535),
                        default=0,
                        help="""
        A fixed port to use for the external HTTP server if that mode is
        enabled. Omit or set to 0 to use a random high ( >1024) port.
        """)
    player.add_argument("--player-passthrough",
                        metavar="TYPES",
                        type=comma_list_filter(STREAM_PASSTHROUGH),
                        default=[],
                        help="""
        A comma-delimited list of stream types to pass to the player as a URL to
        let it handle the transport of the stream instead.

        Stream types that can be converted into a playable URL are:

        - {0}

        Make sure your player can handle the stream type when using this.
        """.format("\n        - ".join(STREAM_PASSTHROUGH)))
    player.add_argument("--player-no-close",
                        action="store_true",
                        help="""
        By default Streamlink will close the player when the stream
        ends. This is to avoid "dead" GUI players lingering after a
        stream ends.

        It does however have the side-effect of sometimes closing a
        player before it has played back all of its cached data.

        This option will instead let the player decide when to exit.
        """)

    output = parser.add_argument_group("File output options")
    output.add_argument("-o",
                        "--output",
                        metavar="FILENAME",
                        help="""
        Write stream data to FILENAME instead of playing it.

        You will be prompted if the file already exists.
        """)
    output.add_argument("-f",
                        "--force",
                        action="store_true",
                        help="""
        When using -o, always write to file even if it already exists.
        """)
    output.add_argument("-O",
                        "--stdout",
                        action="store_true",
                        help="""
        Write stream data to stdout instead of playing it.
        """)

    stream = parser.add_argument_group("Stream options")
    stream.add_argument("--url",
                        dest="url_param",
                        metavar="URL",
                        help="""
        A URL to attempt to extract streams from.

        Usually, the protocol of http(s) URLs can be omitted (https://),
        depending on the implementation of the plugin being used.

        This is an alternative to setting the URL using a positional argument
        and can be useful if set in a config file.
        """)
    stream.add_argument("--default-stream",
                        type=comma_list,
                        metavar="STREAM",
                        help="""
        Stream to play.

        Use "best" or "worst" for selecting the highest or lowest available
        quality.

        Fallback streams can be specified by using a comma-separated list:

          "720p,480p,best"

        This is an alternative to setting the stream using a positional argument
        and can be useful if set in a config file.
        """)
    stream.add_argument("--retry-streams",
                        metavar="DELAY",
                        type=num(float, min=0),
                        help="""
        Retry fetching the list of available streams until streams are found
        while waiting DELAY second(s) between each attempt. If unset, only one
        attempt will be made to fetch the list of streams available.

        The number of fetch retry attempts can be capped with --retry-max.
        """)
    stream.add_argument("--retry-max",
                        metavar="COUNT",
                        type=num(int, min=-1),
                        help="""
        When using --retry-streams, stop retrying the fetch after COUNT retry
        attempt(s). Fetch will retry infinitely if COUNT is zero or unset.

        If --retry-max is set without setting --retry-streams, the delay between
        retries will default to 1 second.
        """)
    stream.add_argument("--retry-open",
                        metavar="ATTEMPTS",
                        type=num(int, min=0),
                        default=1,
                        help="""
        After a successful fetch, try ATTEMPTS time(s) to open the stream until
        giving up.

        Default is 1.
        """)
    stream.add_argument("--stream-types",
                        "--stream-priority",
                        metavar="TYPES",
                        type=comma_list,
                        help="""
        A comma-delimited list of stream types to allow.

        The order will be used to separate streams when there are multiple
        streams with the same name but different stream types. Any stream type
        not listed will be omitted from the available streams list.  A ``*`` can
        be used as a wildcard to match any other type of stream, eg. muxed-stream.

        Default is "rtmp,hls,hds,http,akamaihd,*".
        """)
    stream.add_argument("--stream-sorting-excludes",
                        metavar="STREAMS",
                        type=comma_list,
                        help="""
        Fine tune best/worst synonyms by excluding unwanted streams.

        Uses a filter expression in the format:

          [operator]<value>

        Valid operators are >, >=, < and <=. If no operator is specified then
        equality is tested.

        For example this will exclude streams ranked higher than "480p":

          ">480p"

        Multiple filters can be used by separating each expression with a comma.

        For example this will exclude streams from two quality types:

          ">480p,>medium"

        """)

    transport = parser.add_argument_group("Stream transport options")
    transport.add_argument("--hds-live-edge",
                           type=num(float, min=0),
                           metavar="SECONDS",
                           help="""
        The time live HDS streams will start from the edge of stream.

        Default is 10.0.
        """)
    transport.add_argument("--hds-segment-attempts",
                           type=num(int, min=0),
                           metavar="ATTEMPTS",
                           help="""
        How many attempts should be done to download each HDS segment before
        giving up.

        Default is 3.
        """)
    transport.add_argument("--hds-segment-threads",
                           type=num(int, max=10),
                           metavar="THREADS",
                           help="""
        The size of the thread pool used to download HDS segments. Minimum value
        is 1 and maximum is 10.

        Default is 1.
        """)
    transport.add_argument("--hds-segment-timeout",
                           type=num(float, min=0),
                           metavar="TIMEOUT",
                           help="""
        HDS segment connect and read timeout.

        Default is 10.0.
        """)
    transport.add_argument("--hds-timeout",
                           type=num(float, min=0),
                           metavar="TIMEOUT",
                           help="""
        Timeout for reading data from HDS streams.

        Default is 60.0.
        """)
    transport.add_argument("--hls-live-edge",
                           type=num(int, min=0),
                           metavar="SEGMENTS",
                           help="""
        How many segments from the end to start live HLS streams on.

        The lower the value the lower latency from the source you will be,
        but also increases the chance of buffering.

        Default is 3.
        """)
    transport.add_argument("--hls-segment-attempts",
                           type=num(int, min=0),
                           metavar="ATTEMPTS",
                           help="""
        How many attempts should be done to download each HLS segment before
        giving up.

        Default is 3.
        """)
    transport.add_argument("--hls-playlist-reload-attempts",
                           type=num(int, min=0),
                           metavar="ATTEMPTS",
                           help="""
        How many attempts should be done to reload the HLS playlist before
        giving up.

        Default is 3.
        """)
    transport.add_argument("--hls-segment-threads",
                           type=num(int, max=10),
                           metavar="THREADS",
                           help="""
        The size of the thread pool used to download HLS segments. Minimum value
        is 1 and maximum is 10.

        Default is 1.
        """)
    transport.add_argument("--hls-segment-timeout",
                           type=num(float, min=0),
                           metavar="TIMEOUT",
                           help="""
        HLS segment connect and read timeout.

        Default is 10.0.
        """)
    transport.add_argument("--hls-segment-ignore-names",
                           metavar="NAMES",
                           type=comma_list,
                           help="""
        A comma-delimited list of segment names that will not be fetched.

        Example: --hls-segment-ignore-names 000,001,002

        This will ignore every segment that ends with 000.ts, 001.ts and 002.ts

        Default is None.

        Note: The --hls-timeout must be increased, to a time that is longer than
        the ignored break.
        """)
    transport.add_argument("--hls-audio-select",
                           type=comma_list,
                           metavar="CODE",
                           help="""
        Selects a specific audio source or sources, by language code or name,
        when multiple audio sources are available. Can be * to download all
        audio sources.

        Examples:

          --hls-audio-select "English,German"
          --hls-audio-select "en,de"
          --hls-audio-select "*"

        Note: This is only useful in special circumstances where the regular
        locale option fails, such as when multiple sources of the same language
        exists.
        """)
    transport.add_argument("--hls-timeout",
                           type=num(float, min=0),
                           metavar="TIMEOUT",
                           help="""
        Timeout for reading data from HLS streams.

        Default is 60.0.
        """)
    transport.add_argument("--hls-start-offset",
                           type=hours_minutes_seconds,
                           metavar="HH:MM:SS",
                           default=None,
                           help="""
        Amount of time to skip from the beginning of the stream. For live
        streams, this is a negative offset from the end of the stream (rewind).

        Default is 00:00:00.
        """)
    transport.add_argument("--hls-duration",
                           type=hours_minutes_seconds,
                           metavar="HH:MM:SS",
                           default=None,
                           help="""
        Limit the playback duration, useful for watching segments of a stream.
        The actual duration may be slightly longer, as it is rounded to the
        nearest HLS segment.

        Default is unlimited.
        """)
    transport.add_argument("--hls-live-restart",
                           action="store_true",
                           help="""
        Skip to the beginning of a live stream, or as far back as possible.
        """)
    transport.add_argument("--http-stream-timeout",
                           type=num(float, min=0),
                           metavar="TIMEOUT",
                           help="""
        Timeout for reading data from HTTP streams.

        Default is 60.0.
        """)
    transport.add_argument("--ringbuffer-size",
                           metavar="SIZE",
                           type=filesize,
                           help="""
        The maximum size of ringbuffer. Add a M or K suffix to specify mega or
        kilo bytes instead of bytes.

        The ringbuffer is used as a temporary storage between the stream and the
        player. This is to allows us to download the stream faster than the
        player wants to read it.

        The smaller the size, the higher chance of the player buffering if there
        are download speed dips and the higher size the more data we can use as
        a storage to catch up from speed dips.

        It also allows you to temporary pause as long as the ringbuffer doesn't
        get full since we continue to download the stream in the background.

        Default is "16M".

        Note: A smaller size is recommended on lower end systems (such as
        Raspberry Pi) when playing stream types that require some extra
        processing (such as HDS) to avoid unnecessary background processing.
        """)
    transport.add_argument("--rtmp-proxy",
                           "--rtmpdump-proxy",
                           metavar="PROXY",
                           help="""
        A SOCKS proxy that RTMP streams will use.

        Example: 127.0.0.1:9050
        """)
    transport.add_argument("--rtmp-rtmpdump",
                           "--rtmpdump",
                           "-r",
                           metavar="FILENAME",
                           help="""
        RTMPDump is used to access RTMP streams. You can specify the
        location of the rtmpdump executable if it is not in your PATH.

        Example: "/usr/local/bin/rtmpdump"
        """)
    transport.add_argument("--rtmp-timeout",
                           type=num(float, min=0),
                           metavar="TIMEOUT",
                           help="""
        Timeout for reading data from RTMP streams.

        Default is 60.0.
        """)
    transport.add_argument("--stream-segment-attempts",
                           type=num(int, min=0),
                           metavar="ATTEMPTS",
                           help="""
        How many attempts should be done to download each segment before giving
        up.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 3.
        """)
    transport.add_argument("--stream-segment-threads",
                           type=num(int, max=10),
                           metavar="THREADS",
                           help="""
        The size of the thread pool used to download segments. Minimum value is
        1 and maximum is 10.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 1.
        """)
    transport.add_argument("--stream-segment-timeout",
                           type=num(float, min=0),
                           metavar="TIMEOUT",
                           help="""
        Segment connect and read timeout.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 10.0.
        """)
    transport.add_argument("--stream-timeout",
                           type=num(float, min=0),
                           metavar="TIMEOUT",
                           help="""
        Timeout for reading data from streams.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 60.0.
        """)
    transport.add_argument("--stream-url",
                           action="store_true",
                           help="""
        If possible, translate the stream to a URL and print it.
        """)
    transport.add_argument("--subprocess-cmdline",
                           "--cmdline",
                           "-c",
                           action="store_true",
                           help="""
        Print the command-line used internally to play the stream.

        This is only available on RTMP streams.
        """)
    transport.add_argument("--subprocess-errorlog",
                           "--errorlog",
                           "-e",
                           action="store_true",
                           help="""
        Log possible errors from internal subprocesses to a temporary file. The
        file will be saved in your systems temporary directory.

        Useful when debugging rtmpdump related issues.
        """)
    transport.add_argument("--subprocess-errorlog-path",
                           "--errorlog-path",
                           type=str,
                           metavar="PATH",
                           help="""
        Log the subprocess errorlog to a specific file rather than a temporary
        file. Takes precedence over subprocess-errorlog.

        Useful when debugging rtmpdump related issues.
        """)
    transport.add_argument("--ffmpeg-ffmpeg",
                           metavar="FILENAME",
                           help="""
        FFMPEG is used to access or mux separate video and audio streams. You
        can specify the location of the ffmpeg executable if it is not in your
        PATH.

        Example: "/usr/local/bin/ffmpeg"
        """)
    transport.add_argument("--ffmpeg-verbose",
                           action="store_true",
                           help="""
        Write the console output from ffmpeg to the console.
        """)
    transport.add_argument("--ffmpeg-verbose-path",
                           type=str,
                           metavar="PATH",
                           help="""
        Path to write the output from the ffmpeg console.
        """)
    transport.add_argument("--ffmpeg-video-transcode",
                           metavar="CODEC",
                           help="""
        When muxing streams transcode the video to this CODEC.

        Default is "copy".

        Example: "h264"
        """)
    transport.add_argument("--ffmpeg-audio-transcode",
                           metavar="CODEC",
                           help="""
        When muxing streams transcode the audio to this CODEC.

        Default is "copy".

        Example: "aac"
        """)

    http = parser.add_argument_group("HTTP options")
    http.add_argument("--http-proxy",
                      metavar="HTTP_PROXY",
                      help="""
        A HTTP proxy to use for all HTTP requests.

        Example: "http://hostname:port/"
        """)
    http.add_argument("--https-proxy",
                      metavar="HTTPS_PROXY",
                      help="""
        A HTTPS capable proxy to use for all HTTPS requests.

        Example: "https://hostname:port/"
        """)
    http.add_argument("--http-cookie",
                      metavar="KEY=VALUE",
                      type=keyvalue,
                      action="append",
                      help="""
        A cookie to add to each HTTP request.

        Can be repeated to add multiple cookies.
        """)
    http.add_argument("--http-header",
                      metavar="KEY=VALUE",
                      type=keyvalue,
                      action="append",
                      help="""
        A header to add to each HTTP request.

        Can be repeated to add multiple headers.
        """)
    http.add_argument("--http-query-param",
                      metavar="KEY=VALUE",
                      type=keyvalue,
                      action="append",
                      help="""
        A query parameter to add to each HTTP request.

        Can be repeated to add multiple query parameters.
        """)
    http.add_argument("--http-ignore-env",
                      action="store_true",
                      help="""
        Ignore HTTP settings set in the environment such as environment
        variables (HTTP_PROXY, etc) or ~/.netrc authentication.
        """)
    http.add_argument("--http-no-ssl-verify",
                      action="store_true",
                      help="""
        Don't attempt to verify SSL certificates.

        Usually a bad idea, only use this if you know what you're doing.
        """)
    http.add_argument("--http-disable-dh",
                      action="store_true",
                      help="""
        Disable Diffie Hellman key exchange

        Usually a bad idea, only use this if you know what you're doing.
        """)
    http.add_argument("--http-ssl-cert",
                      metavar="FILENAME",
                      help="""
        SSL certificate to use.

        Expects a .pem file.
        """)
    http.add_argument("--http-ssl-cert-crt-key",
                      metavar=("CRT_FILENAME", "KEY_FILENAME"),
                      nargs=2,
                      help="""
        SSL certificate to use.

        Expects a .crt and a .key file.
        """)
    http.add_argument("--http-timeout",
                      metavar="TIMEOUT",
                      type=num(float, min=0),
                      help="""
        General timeout used by all HTTP requests except the ones covered by
        other options.

        Default is 20.0.
        """)

    # Deprecated options
    stream.add_argument("--best-stream-default",
                        action="store_true",
                        help=argparse.SUPPRESS)
    player.add_argument("-q",
                        "--quiet-player",
                        action="store_true",
                        help=argparse.SUPPRESS)
    transport.add_argument("--hds-fragment-buffer",
                           type=int,
                           metavar="fragments",
                           help=argparse.SUPPRESS)
    http.add_argument("--http-cookies",
                      metavar="COOKIES",
                      help=argparse.SUPPRESS)
    http.add_argument("--http-headers",
                      metavar="HEADERS",
                      help=argparse.SUPPRESS)
    http.add_argument("--http-query-params",
                      metavar="PARAMS",
                      help=argparse.SUPPRESS)
    general.add_argument("--no-version-check",
                         action="store_true",
                         help=argparse.SUPPRESS)
    return parser
Exemplo n.º 10
0
class Resolve(Plugin):

    _url_re = re.compile(r'''(resolve://)?(?P<url>.+)''')

    # regex for iframes
    _iframe_re = re.compile(
        r'''
        <ifr(?:["']\s?\+\s?["'])?ame
        (?!\sname=["']g_iFrame).*?src=
        ["'](?P<url>[^"'\s<>]+)["']
        [^<>]*?>
        ''', re.VERBOSE | re.IGNORECASE | re.DOTALL)

    # regex for playlists
    _playlist_re = re.compile(
        r'''
        (?:["']|=|&quot;)(?P<url>
            (?<!title=["'])
            (?<!["']title["']:["'])
                [^"'<>\s\;{}]+\.(?:m3u8|f4m|mp3|mp4|mpd)
            (?:\?[^"'<>\s\\{}]+)?)
        (?:\\?["']|(?<!;)\s|>|\\&quot;)
        ''', re.DOTALL | re.VERBOSE)

    # regex for mp3 and mp4 files
    _httpstream_bitrate_re = re.compile(
        r'''
        (?:_|\.)
        (?:
            (?P<bitrate>\d{1,4})
            |
            (?P<resolution>\d{1,4}p)
        )
        \.mp(?:3|4)
        ''', re.VERBOSE)

    # Regex for: javascript redirection
    _window_location_re = re.compile(
        r'''
        <script[^<]+window\.location\.href\s?=\s?["']
        (?P<url>[^"']+)["'];[^<>]+
        ''', re.DOTALL | re.VERBOSE)
    _unescape_iframe_re = re.compile(
        r'''
        unescape\050["']
        (?P<data>%3C(?:
            iframe|%69%66%72%61%6d%65
        )%20[^"']+)["']
        ''', re.IGNORECASE | re.VERBOSE)

    _unescape_hls_re = re.compile(
        r'''
        unescape\050["']
        (?P<data>%3C(?:
            [^"']+m3u8[^"']+
        )%20[^"']+)["']
        ''', re.IGNORECASE | re.VERBOSE)

    # Regex for obviously ad paths
    _ads_path_re = re.compile(
        r'''
        (?:/(?:static|\d+))?
        /ads?/?(?:\w+)?
        (?:\d+x\d+)?
        (?:_\w+)?\.(?:html?|php)
        ''', re.VERBOSE)

    # START - _make_url_list
    # Not allowed at the end of the parsed url path
    blacklist_endswith = (
        '.gif',
        '.jpg',
        '.png',
        '.svg',
        '.vtt',
        '/chat.html',
        '/chat',
        '/novideo.mp4',
        '/vidthumb.mp4',
    )
    # Not allowed at the end of the parsed url netloc
    blacklist_netloc = (
        '127.0.0.1',
        'about:blank',
        'abv.bg',
        'adfox.ru',
        'cbox.ws',
        'googletagmanager.com',
        'javascript:false',
    )
    # END - _make_url_list

    arguments = PluginArguments(
        PluginArgument('playlist-max',
                       metavar='NUMBER',
                       type=num(int, min=0, max=25),
                       default=5,
                       help='''
            Number of how many playlist URLs of the same type
            are allowed to be resolved with this plugin.

            Default is 5
            '''),
        PluginArgument('playlist-referer',
                       metavar='URL',
                       help='''
            Set a custom referer URL for the playlist URLs.

            This only affects playlist URLs of this plugin.

            Default URL of the last website.
            '''),
        PluginArgument('blacklist-netloc',
                       metavar='NETLOC',
                       type=comma_list,
                       help='''
            Blacklist domains that should not be used,
            by using a comma-separated list:

              'example.com,localhost,google.com'

            Useful for websites with a lot of iframes.
            '''),
        PluginArgument('blacklist-path',
                       metavar='PATH',
                       type=comma_list,
                       help='''
            Blacklist the path of a domain that should not be used,
            by using a comma-separated list:

              'example.com/mypath,localhost/example,google.com/folder'

            Useful for websites with different iframes of the same domain.
            '''),
        PluginArgument('blacklist-filepath',
                       metavar='FILEPATH',
                       type=comma_list,
                       help='''
            Blacklist file names for iframes and playlists
            by using a comma-separated list:

              'index.html,ignore.m3u8,/ad/master.m3u8'

            Sometimes there are invalid URLs in the result list,
            this can be used to remove them.
            '''),
        PluginArgument('whitelist-netloc',
                       metavar='NETLOC',
                       type=comma_list,
                       help='''
            Whitelist domains that should only be searched for iframes,
            by using a comma-separated list:

              'example.com,localhost,google.com'

            Useful for websites with lots of iframes,
            where the main iframe always has the same hosting domain.
            '''),
        PluginArgument('whitelist-path',
                       metavar='PATH',
                       type=comma_list,
                       help='''
            Whitelist the path of a domain that should only be searched
            for iframes, by using a comma-separated list:

              'example.com/mypath,localhost/example,google.com/folder'

            Useful for websites with different iframes of the same domain,
            where the main iframe always has the same path.
            '''),
    )

    def __init__(self, url):
        super(Resolve, self).__init__(url)
        ''' generates default options
            and caches them into ResolveCache class
        '''
        self.url = update_scheme('http://',
                                 self._url_re.match(self.url).group('url'))

        self.html_text = ''
        self.title = None

        # START - cache every used url and set a referer
        if hasattr(ResolveCache, 'cache_url_list'):
            ResolveCache.cache_url_list += [self.url]
            # set the last url as a referer
            self.referer = ResolveCache.cache_url_list[-2]
        else:
            ResolveCache.cache_url_list = [self.url]
            self.referer = self.url
        self.session.http.headers.update({'Referer': self.referer})
        # END

        # START - how often _get_streams already run
        self._run = len(ResolveCache.cache_url_list)
        # END

    @classmethod
    def priority(cls, url):
        '''
        Returns
        - NO priority if the URL is not prefixed
        - HIGH priority if the URL is prefixed
        :param url: the URL to find the plugin priority for
        :return: plugin priority for the given URL
        '''
        m = cls._url_re.match(url)
        if m:
            prefix, url = cls._url_re.match(url).groups()
            if prefix is not None:
                return HIGH_PRIORITY
        return NO_PRIORITY

    @classmethod
    def can_handle_url(cls, url):
        m = cls._url_re.match(url)
        if m:
            return m.group('url') is not None

    def compare_url_path(self, parsed_url, check_list):
        '''compare a parsed url, if it matches an item from a list

        Args:
           parsed_url: an URL that was used with urlparse
           check_list: a list of URLs as a tuple
                       [('foo.bar', '/path/'), ('foo2.bar', '/path/')]

        Returns:
            True
                if parsed_url in check_list
            False
                if parsed_url not in check_list
        '''
        status = False
        for netloc, path in check_list:
            if (parsed_url.netloc.endswith(netloc)
                    and parsed_url.path.startswith(path)):
                status = True
                break
        return status

    def merge_path_list(self, static, user):
        '''merge the static list, with an user list

        Args:
           static (list): static list from this plugin
           user (list): list from an user command

        Returns:
            A new valid list
        '''
        for _path_url in user:
            if not _path_url.startswith(('http', '//')):
                _path_url = update_scheme('http://', _path_url)
            _parsed_path_url = urlparse(_path_url)
            if _parsed_path_url.netloc and _parsed_path_url.path:
                static += [(_parsed_path_url.netloc, _parsed_path_url.path)]
        return static

    def repair_url(self, url, base_url, stream_base=''):
        '''repair a broken url'''
        # remove \
        new_url = url.replace('\\', '')
        # repairs broken scheme
        if new_url.startswith('http&#58;//'):
            new_url = 'http:' + new_url[9:]
        elif new_url.startswith('https&#58;//'):
            new_url = 'https:' + new_url[10:]
        # creates a valid url from path only urls
        # and adds missing scheme for // urls
        if stream_base and new_url[1] is not '/':
            if new_url[0] is '/':
                new_url = new_url[1:]
            new_url = urljoin(stream_base, new_url)
        else:
            new_url = urljoin(base_url, new_url)
        return new_url

    def _make_url_list(self, old_list, base_url, url_type=''):
        '''removes unwanted URLs and creates a list of valid URLs

        Args:
            old_list: list of URLs
            base_url: URL that will get used for scheme and netloc repairs
            url_type: can be ... and is used for ...
                - iframe
                    --resolve-whitelist-netloc
                - playlist
                    Not used
        Returns:
            (list) A new valid list of urls.
        '''
        # START - List for not allowed URL Paths
        # --resolve-blacklist-path
        if not hasattr(ResolveCache, 'blacklist_path'):

            # static list
            blacklist_path = [
                ('bigo.tv', '/show.mp4'),
                ('expressen.se', '/_livetvpreview/'),
                ('facebook.com', '/connect'),
                ('facebook.com', '/plugins'),
                ('haber7.com', '/radyohome/station-widget/'),
                ('static.tvr.by', '/upload/video/atn/promo'),
                ('twitter.com', '/widgets'),
                ('vesti.ru', '/native_widget.html'),
                ('youtube.com', '/['),
            ]

            # merge user and static list
            blacklist_path_user = self.get_option('blacklist_path')
            if blacklist_path_user is not None:
                blacklist_path = self.merge_path_list(blacklist_path,
                                                      blacklist_path_user)

            ResolveCache.blacklist_path = blacklist_path
        # END

        # START - List of only allowed URL Paths for Iframes
        # --resolve-whitelist-path
        if not hasattr(ResolveCache, 'whitelist_path'):
            whitelist_path = []
            whitelist_path_user = self.get_option('whitelist_path')
            if whitelist_path_user is not None:
                whitelist_path = self.merge_path_list([], whitelist_path_user)
            ResolveCache.whitelist_path = whitelist_path
        # END

        # sorted after the way streamlink will try to remove an url
        status_remove = [
            'SAME-URL',
            'SCHEME',
            'WL-netloc',
            'WL-path',
            'BL-static',
            'BL-netloc',
            'BL-path',
            'BL-ew',
            'BL-filepath',
            'ADS',
        ]

        new_list = []
        for url in old_list:
            new_url = self.repair_url(url, base_url)
            # parse the url
            parse_new_url = urlparse(new_url)

            # START - removal of unwanted urls
            REMOVE = False
            count = 0

            # status_remove must be updated on changes
            for url_status in (
                    # Removes an already used iframe url
                (new_url in ResolveCache.cache_url_list),
                    # Allow only an url with a valid scheme
                (not parse_new_url.scheme.startswith(('http'))),
                    # Allow only whitelisted domains for iFrames
                    # --resolve-whitelist-netloc
                (url_type == 'iframe' and self.get_option('whitelist_netloc')
                 and parse_new_url.netloc.endswith(
                     tuple(self.get_option('whitelist_netloc'))) is False),
                    # Allow only whitelisted paths from a domain for iFrames
                    # --resolve-whitelist-path
                (url_type == 'iframe'
                 and ResolveCache.whitelist_path and self.compare_url_path(
                     parse_new_url, ResolveCache.whitelist_path) is False),
                    # Removes blacklisted domains from a static list
                    # self.blacklist_netloc
                (parse_new_url.netloc.endswith(self.blacklist_netloc)),
                    # Removes blacklisted domains
                    # --resolve-blacklist-netloc
                (self.get_option('blacklist_netloc')
                 and parse_new_url.netloc.endswith(
                     tuple(self.get_option('blacklist_netloc')))),
                    # Removes blacklisted paths from a domain
                    # --resolve-blacklist-path
                (self.compare_url_path(parse_new_url,
                                       ResolveCache.blacklist_path) is True),
                    # Removes unwanted endswith images and chatrooms
                (parse_new_url.path.endswith(self.blacklist_endswith)),
                    # Removes blacklisted file paths
                    # --resolve-blacklist-filepath
                (self.get_option('blacklist_filepath')
                 and parse_new_url.path.endswith(
                     tuple(self.get_option('blacklist_filepath')))),
                    # Removes obviously AD URL
                (self._ads_path_re.match(parse_new_url.path)),
            ):

                count += 1
                if url_status:
                    REMOVE = True
                    break

            if REMOVE is True:
                log.debug('{0} - Removed: {1}'.format(status_remove[count - 1],
                                                      new_url))
                continue
            # END - removal of unwanted urls

            # Add repaired url
            new_list += [new_url]
        # Remove duplicates
        log.debug('List length: {0} (with duplicates)'.format(len(new_list)))
        new_list = sorted(list(set(new_list)))
        return new_list

    def _unescape_type(self, _re, _type_re):
        '''search for unescaped iframes or m3u8 URLs'''
        unescape_type = _re.findall(self.html_text)
        if unescape_type:
            unescape_text = []
            for data in unescape_type:
                unescape_text += [unquote(data)]
            unescape_text = ','.join(unescape_text)
            unescape_type = _type_re.findall(unescape_text)
            if unescape_type:
                log.debug('Found unescape_type: {0}'.format(
                    len(unescape_type)))
                return unescape_type
        log.trace('No unescape_type')
        return False

    def _window_location(self):
        '''Try to find a script with window.location.href

        Args:
            res_text: Content from self._res_text

        Returns:
            (str) url
              or
            False
                if no url was found.
        '''

        match = self._window_location_re.search(self.html_text)
        if match:
            temp_url = urljoin(self.url, match.group('url'))
            log.debug('Found window_location: {0}'.format(temp_url))
            return temp_url

        log.trace('No window_location')
        return False

    def _resolve_playlist(self, playlist_all):
        ''' create streams '''
        playlist_referer = self.get_option('playlist_referer') or self.url
        self.session.http.headers.update({'Referer': playlist_referer})

        playlist_max = self.get_option('playlist_max') or 5
        count_playlist = {
            'dash': 0,
            'hds': 0,
            'hls': 0,
            'http': 0,
        }
        for url in playlist_all:
            parsed_url = urlparse(url)
            if (parsed_url.path.endswith(('.m3u8'))
                    or parsed_url.query.endswith(('.m3u8'))):
                if count_playlist['hls'] >= playlist_max:
                    log.debug('Skip - {0}'.format(url))
                    continue
                try:
                    streams = HLSStream.parse_variant_playlist(
                        self.session, url).items()
                    if not streams:
                        yield 'live', HLSStream(self.session, url)
                    for s in streams:
                        yield s
                    log.debug('HLS URL - {0}'.format(url))
                    count_playlist['hls'] += 1
                except Exception as e:
                    log.error('Skip HLS with error {0}'.format(str(e)))
            elif (parsed_url.path.endswith(('.f4m'))
                  or parsed_url.query.endswith(('.f4m'))):
                if count_playlist['hds'] >= playlist_max:
                    log.debug('Skip - {0}'.format(url))
                    continue
                try:
                    for s in HDSStream.parse_manifest(self.session,
                                                      url).items():
                        yield s
                    log.debug('HDS URL - {0}'.format(url))
                    count_playlist['hds'] += 1
                except Exception as e:
                    log.error('Skip HDS with error {0}'.format(str(e)))
            elif (parsed_url.path.endswith(('.mp3', '.mp4'))
                  or parsed_url.query.endswith(('.mp3', '.mp4'))):
                if count_playlist['http'] >= playlist_max:
                    log.debug('Skip - {0}'.format(url))
                    continue
                try:
                    name = 'vod'
                    m = self._httpstream_bitrate_re.search(url)
                    if m:
                        bitrate = m.group('bitrate')
                        resolution = m.group('resolution')
                        if bitrate:
                            name = '{0}k'.format(m.group('bitrate'))
                        elif resolution:
                            name = resolution
                    yield name, HTTPStream(self.session, url)
                    log.debug('HTTP URL - {0}'.format(url))
                    count_playlist['http'] += 1
                except Exception as e:
                    log.error('Skip HTTP with error {0}'.format(str(e)))
            elif (parsed_url.path.endswith(('.mpd'))
                  or parsed_url.query.endswith(('.mpd'))):
                if count_playlist['dash'] >= playlist_max:
                    log.debug('Skip - {0}'.format(url))
                    continue
                try:
                    for s in DASHStream.parse_manifest(self.session,
                                                       url).items():
                        yield s
                    log.debug('DASH URL - {0}'.format(url))
                    count_playlist['dash'] += 1
                except Exception as e:
                    log.error('Skip DASH with error {0}'.format(str(e)))
            else:
                log.error('parsed URL - {0}'.format(url))

    def _res_text(self, url):
        '''Content of a website

        Args:
            url: URL with an embedded Video Player.

        Returns:
            Content of the response
        '''
        try:
            res = self.session.http.get(url, allow_redirects=True)
        except Exception as e:
            if 'Received response with content-encoding: gzip' in str(e):
                headers = {
                    'User-Agent': useragents.FIREFOX,
                    'Accept-Encoding': 'deflate'
                }
                res = self.session.http.get(url,
                                            headers=headers,
                                            allow_redirects=True)
            elif '403 Client Error' in str(e):
                log.error('Website Access Denied/Forbidden, you might be geo-'
                          'blocked or other params are missing.')
                raise NoStreamsError(self.url)
            elif '404 Client Error' in str(e):
                log.error('Website was not found, the link is broken or dead.')
                raise NoStreamsError(self.url)
            else:
                raise e

        if res.history:
            for resp in res.history:
                log.debug('Redirect: {0} - {1}'.format(resp.status_code,
                                                       resp.url))
            log.debug('URL: {0}'.format(res.url))
        return res.text

    def settings_url(self):
        '''store custom settings for URLs'''
        o = urlparse(self.url)

        # User-Agent
        _android = []
        _chrome = []
        _ipad = []
        _iphone = [
            'bigo.tv',
        ]

        if self.session.http.headers['User-Agent'].startswith(
                'python-requests'):
            if o.netloc.endswith(tuple(_android)):
                self.session.http.headers.update(
                    {'User-Agent': useragents.ANDROID})
            elif o.netloc.endswith(tuple(_chrome)):
                self.session.http.headers.update(
                    {'User-Agent': useragents.CHROME})
            elif o.netloc.endswith(tuple(_ipad)):
                self.session.http.headers.update(
                    {'User-Agent': useragents.IPAD})
            elif o.netloc.endswith(tuple(_iphone)):
                self.session.http.headers.update(
                    {'User-Agent': useragents.IPHONE_6})
            else:
                # default User-Agent
                self.session.http.headers.update(
                    {'User-Agent': useragents.FIREFOX})

        # SSL Verification - http.verify
        http_verify = [
            '.cdn.bg',
            'sportal.bg',
        ]
        if (o.netloc.endswith(tuple(http_verify))
                and self.session.http.verify):
            self.session.http.verify = False
            log.warning('SSL Verification disabled.')

    def get_title(self):
        if self.title is None:
            if not self.html_text:
                self.html_text = self._res_text(self.url)
            _title_re = re.compile(r'<title>(?P<title>[^<>]+)</title>')
            m = _title_re.search(self.html_text)
            if m:
                self.title = m.group('title')
            if self.title is None:
                # fallback if there is no <title>
                self.title = self.url
        return self.title

    def _get_streams(self):
        self.settings_url()

        if self._run <= 1:
            log.debug('Version 2018-08-19')
            log.info('This is a custom plugin.')
            log.debug('User-Agent: {0}'.format(
                self.session.http.headers['User-Agent']))

        new_session_url = False

        log.info('  {0}. URL={1}'.format(self._run, self.url))

        # GET website content
        self.html_text = self._res_text(self.url)

        # Playlist URL
        playlist_all = self._playlist_re.findall(self.html_text)

        _p_u = self._unescape_type(self._unescape_hls_re, self._playlist_re)
        if _p_u:
            playlist_all += _p_u

        if playlist_all:
            log.debug('Found Playlists: {0}'.format(len(playlist_all)))
            playlist_list = self._make_url_list(
                playlist_all,
                self.url,
                url_type='playlist',
            )
            if playlist_list:
                log.info('Found Playlists: {0} (valid)'.format(
                    len(playlist_list)))
                return self._resolve_playlist(playlist_list)
        else:
            log.trace('No Playlists')

        # iFrame URL
        iframe_list = []
        for _iframe_list in (self._iframe_re.findall(self.html_text),
                             self._unescape_type(self._unescape_iframe_re,
                                                 self._iframe_re)):
            if not _iframe_list:
                continue
            iframe_list += _iframe_list

        if iframe_list:
            log.debug('Found Iframes: {0}'.format(len(iframe_list)))
            # repair and filter iframe url list
            new_iframe_list = self._make_url_list(iframe_list,
                                                  self.url,
                                                  url_type='iframe')
            if new_iframe_list:
                number_iframes = len(new_iframe_list)
                if number_iframes == 1:
                    new_session_url = new_iframe_list[0]
                else:
                    log.info('--- IFRAMES ---')
                    for i, item in enumerate(new_iframe_list, start=1):
                        log.info('{0} - {1}'.format(i, item))
                    log.info('--- IFRAMES ---')

                    try:
                        number = int(
                            self.input_ask('Choose an iframe number from above'
                                           ).split(' ')[0])
                        new_session_url = new_iframe_list[number - 1]
                    except FatalPluginError:
                        new_session_url = new_iframe_list[0]
                    except ValueError:
                        log.error('invalid input answer')
                    except (IndexError, TypeError):
                        log.error('invalid input number')

                    if not new_session_url:
                        new_session_url = new_iframe_list[0]
        else:
            log.trace('No Iframes')

        if not new_session_url:
            # search for window.location.href
            new_session_url = self._window_location()

        if new_session_url:
            # the Dailymotion Plugin does not work with this Referer
            if 'dailymotion.com' in new_session_url:
                del self.session.http.headers['Referer']

            return self.session.streams(new_session_url)

        raise NoPluginError
Exemplo n.º 11
0
def build_parser():
    parser = ArgumentParser(
        prog="streamlink",
        fromfile_prefix_chars="@",
        formatter_class=HelpFormatter,
        add_help=False,
        usage="%(prog)s [OPTIONS] <URL> [STREAM]",
        description=dedent("""
        Streamlink is command-line utility that extracts streams from various
        services and pipes them into a video player of choice.
        """)        
    )

    positional = parser.add_argument_group("Positional arguments") # add option groups to organize
    positional.add_argument(
        "url",
        metavars="URL",
        nargs="?", # one argument to be consumed (if not present, the value formd efault)
        help="""
        A URL to attempt to extract streams from 
        """
    )
    positional.add_argument(
        "stream",
        metavar="STREAM",
        nargs="?",
        type=comma_list, # type= can take any callable that takes a single string argument and returns the converted value
        help="""
        stream to play
        """
    )

    general = aparser.add_argument_group("General options")
    general.add_argument(
        "-h", "--help",
        action="store_true",
        help="""
        Show help message and exit
        """
    )
    general.add_argument(
        "-V", "--version",
        action="version",
        version="%(prog)s {0}".format(LIVESTREAMER_VERSION),
        help="""
        Show version number and exit.
        """
    )
    general.add_argument(
        "--plugins",
        action="store_true",
        help="""
        Print a list of plugins
        """
    )

    output = parser.add_argument_group("File output options")
    output.add_argument(
        "-o", "--output",
        metavar="FILENAME",
        help="""
        Write stream data to FILENAME instead of playing it.

        You will be prompted if the file already exists.
        """
    )

    stream = parser.add_argument_group("Stream options")
    stream.add_argument(
        "--url",
        dest="url_param",
        metavar="URL",
        help="""
        A URL to attempt to extract streams from.
        """
    )
    stream.add_argument(
        "--default-stream",
        type=comma_list,
        metavar="STREAM",
        help="""
        Stream to play.
        """
    )
    stream.add_argument(
        "--retry-streams",
        metavar="DELAY",
        type=num(float, min=0),
        help="""
        Retry
        """
    )

    return parser 
Exemplo n.º 12
0
def build_parser():
    parser = ArgumentParser(
        prog="streamlink",
        fromfile_prefix_chars="@",
        formatter_class=HelpFormatter,
        add_help=False,
        usage="%(prog)s [OPTIONS] <URL> [STREAM]",
        description=dedent("""
        Streamlink is command-line utility that extracts streams from various
        services and pipes them into a video player of choice.
        """),
        epilog=dedent("""
        For more in-depth documentation see:
          https://streamlink.github.io

        Please report broken plugins or bugs to the issue tracker on Github:
          https://github.com/streamlink/streamlink/issues
        """)
    )

    positional = parser.add_argument_group("Positional arguments")
    positional.add_argument(
        "url",
        metavar="URL",
        nargs="?",
        help="""
        A URL to attempt to extract streams from.

        Usually, the protocol of http(s) URLs can be omitted ("https://"),
        depending on the implementation of the plugin being used.

        Alternatively, the URL can also be specified by using the --url option.
        """
    )
    positional.add_argument(
        "stream",
        metavar="STREAM",
        nargs="?",
        type=comma_list,
        help="""
        Stream to play.

        Use ``best`` or ``worst`` for selecting the highest or lowest available
        quality.

        Fallback streams can be specified by using a comma-separated list:

          "720p,480p,best"

        If no stream is specified and --default-stream is not used, then a list
        of available streams will be printed.
        """
    )

    general = parser.add_argument_group("General options")
    general.add_argument(
        "-h", "--help",
        action="store_true",
        help="""
        Show this help message and exit.
        """
    )
    general.add_argument(
        "-V", "--version",
        action="version",
        version="%(prog)s {0}".format(LIVESTREAMER_VERSION),
        help="""
        Show version number and exit.
        """
    )
    general.add_argument(
        "--plugins",
        action="store_true",
        help="""
        Print a list of all currently installed plugins.
        """
    )
    general.add_argument(
        "--plugin-dirs",
        metavar="DIRECTORY",
        type=comma_list,
        help="""
        Attempts to load plugins from these directories.

        Multiple directories can be used by separating them with a comma.
        """
    )
    general.add_argument(
        "--can-handle-url",
        metavar="URL",
        help="""
        Check if Streamlink has a plugin that can handle the specified URL.

        Returns status code 1 for false and 0 for true.

        Useful for external scripting.
        """
    )
    general.add_argument(
        "--can-handle-url-no-redirect",
        metavar="URL",
        help="""
        Same as --can-handle-url but without following redirects when looking up
        the URL.
        """
    )
    general.add_argument(
        "--config",
        action="append",
        metavar="FILENAME",
        help="""
        Load options from this config file.

        Can be repeated to load multiple files, in which case the options are
        merged on top of each other where the last config has highest priority.
        """
    )
    general.add_argument(
        "-l", "--loglevel",
        metavar="LEVEL",
        choices=logger.levels,
        default="info",
        help="""
        Set the log message threshold.

        Valid levels are: none, error, warning, info, debug, trace
        """
    )
    general.add_argument(
        "-Q", "--quiet",
        action="store_true",
        help="""
        Hide all log output.

        Alias for "--loglevel none".
        """
    )
    general.add_argument(
        "-j", "--json",
        action="store_true",
        help="""
        Output JSON representations instead of the normal text output.

        Useful for external scripting.
        """
    )
    general.add_argument(
        "--auto-version-check",
        type=boolean,
        metavar="{yes,true,1,on,no,false,0,off}",
        default=False,
        help="""
        Enable or disable the automatic check for a new version of Streamlink.

        Default is "no".
        """
    )
    general.add_argument(
        "--version-check",
        action="store_true",
        help="""
        Runs a version check and exits.
        """
    )
    general.add_argument(
        "--locale",
        type=str,
        metavar="LOCALE",
        help="""
        The preferred locale setting, for selecting the preferred subtitle and
        audio language.

        The locale is formatted as [language_code]_[country_code], eg. en_US or
        es_ES.

        Default is system locale.
        """
    )
    general.add_argument(
        "--twitch-oauth-authenticate",
        action="store_true",
        help="""
        Open a web browser where you can grant Streamlink access to your Twitch
        account which creates a token for use with --twitch-oauth-token.
        """
    )

    player = parser.add_argument_group("Player options")
    player.add_argument(
        "-p", "--player",
        metavar="COMMAND",
        default=find_default_player(),
        help="""
        Player to feed stream data to. By default, VLC will be used if it can be
        found in its default location.

        This is a shell-like syntax to support using a specific player:

          %(prog)s --player=vlc <url> [stream]

        Absolute or relative paths can also be passed via this option in the
        event the player's executable can not be resolved:

          %(prog)s --player=/path/to/vlc <url> [stream]
          %(prog)s --player=./vlc-player/vlc <url> [stream]

        To use a player that is located in a path with spaces you must quote the
        parameter or its value:

          %(prog)s "--player=/path/with spaces/vlc" <url> [stream]
          %(prog)s --player "C:\\path\\with spaces\\mpc-hc64.exe" <url> [stream]

        Options may also be passed to the player. For example:

          %(prog)s --player "vlc --file-caching=5000" <url> [stream]

        As an alternative to this, see the --player-args parameter, which does
        not log any custom player arguments.
        """
    )
    player.add_argument(
        "-a", "--player-args",
        metavar="ARGUMENTS",
        default=DEFAULT_PLAYER_ARGUMENTS,
        help="""
        This option allows you to customize the default arguments which are put
        together with the value of --player to create a command to execute.
        Unlike the --player parameter, custom player arguments will not be logged.

        This value can contain formatting variables surrounded by curly braces,
        {{ and }}. If you need to include a brace character, it can be escaped
        by doubling, e.g. {{{{ and }}}}.

        Formatting variables available:

        {{filename}}
            This is the filename that the player will use. It's usually "-"
            (stdin), but can also be a URL or a file depending on the options
            used.

        It's usually enough to use --player instead of this unless you need to
        add arguments after the filename.

        Default is "{0}".

        Example:

          %(prog)s -p vlc -a "--play-and-exit {{filename}}" <url> [stream]

        """.format(DEFAULT_PLAYER_ARGUMENTS)
    )
    player.add_argument(
        "-v", "--verbose-player",
        action="store_true",
        help="""
        Allow the player to display its console output.
        """
    )
    player.add_argument(
        "-n", "--player-fifo", "--fifo",
        action="store_true",
        help="""
        Make the player read the stream through a named pipe instead of the
        stdin pipe.
        """
    )
    player.add_argument(
        "--player-http",
        action="store_true",
        help="""
        Make the player read the stream through HTTP instead of the stdin pipe.
        """
    )
    player.add_argument(
        "--player-continuous-http",
        action="store_true",
        help="""
        Make the player read the stream through HTTP, but unlike --player-http
        it will continuously try to open the stream if the player requests it.

        This makes it possible to handle stream disconnects if your player is
        capable of reconnecting to a HTTP stream. This is usually done by
        setting your player to a "repeat mode".
        """
    )
    player.add_argument(
        "--player-external-http",
        action="store_true",
        help="""
        Serve stream data through HTTP without running any player. This is
        useful to allow external devices like smartphones or streaming boxes to
        watch streams they wouldn't be able to otherwise.

        Behavior will be similar to the continuous HTTP option, but no player
        program will be started, and the server will listen on all available
        connections instead of just in the local (loopback) interface.

        The URLs that can be used to access the stream will be printed to the
        console, and the server can be interrupted using CTRL-C.
        """
    )
    player.add_argument(
        "--player-external-http-port",
        metavar="PORT",
        type=num(int, min=0, max=65535),
        default=0,
        help="""
        A fixed port to use for the external HTTP server if that mode is
        enabled. Omit or set to 0 to use a random high ( >1024) port.
        """
    )
    player.add_argument(
        "--player-passthrough",
        metavar="TYPES",
        type=comma_list_filter(STREAM_PASSTHROUGH),
        default=[],
        help="""
        A comma-delimited list of stream types to pass to the player as a URL to
        let it handle the transport of the stream instead.

        Stream types that can be converted into a playable URL are:

        - {0}

        Make sure your player can handle the stream type when using this.
        """.format("\n        - ".join(STREAM_PASSTHROUGH))
    )
    player.add_argument(
        "--player-no-close",
        action="store_true",
        help="""
        By default Streamlink will close the player when the stream
        ends. This is to avoid "dead" GUI players lingering after a
        stream ends.

        It does however have the side-effect of sometimes closing a
        player before it has played back all of its cached data.

        This option will instead let the player decide when to exit.
        """
    )
    player.add_argument(
        "-t", "--title",
        metavar="TITLE",
        help="""
        This option allows you to supply a title to be displayed in the
        title bar of the window that the video player is launched in.

        This value can contain formatting variables surrounded by curly braces,
        {{ and }}. If you need to include a brace character, it can be escaped
        by doubling, e.g. {{{{ and }}}}.

        This option is only supported for the following players: {0}.

        VLC specific information:
            VLC has certain codes you can use inside your title.
            These are accessible inside --title by using a backslash
            before the dollar sign VLC uses to denote a format character.

            e.g. to put the current date in your VLC window title,
            the string "\\$A" could be inserted inside your --title string.

            A full list of the format codes VLC uses is available here:
            https://wiki.videolan.org/Documentation:Format_String/

        mpv specific information:
            mpv has certain codes you can use inside your title.
            These are accessible inside --title by using a backslash
            before the dollar sign mpv uses to denote a format character.

            e.g. to put the current version of mpv running inside your
            mpv window title, the string "\\${{{{mpv-version}}}}" could be
            inserted inside your --title string.

            A full list of the format codes mpv uses is available here:
            https://mpv.io/manual/stable/#property-expansion

        Formatting variables available to use in --title:

        {{title}}
            If available, this is the title of the stream.
            Otherwise, it is the string "{1}"

        {{author}}
            If available, this is the author of the stream.
            Otherwise, it is the string "{2}"

        {{category}}
            If available, this is the category the stream has been placed into.

            - For Twitch, this is the game being played
            - For YouTube, it's the category e.g. Gaming, Sports, Music...

            Otherwise, it is the string "{3}"

        {{game}}
            This is just a synonym for {{category}} which may make more sense for
            gaming oriented platforms. "Game being played" is a way to categorize
            the stream, so it doesn't need its own separate handling.

        Examples:

            %(prog)s -p vlc --title "{{title}} -!- {{author}} -!- {{category}} \\$A" <url> [stream]
            %(prog)s -p mpv --title "{{title}} -- {{author}} -- {{category}} -- (\\${{{{mpv-version}}}})" <url> [stream]

        """.format(', '.join(sorted(SUPPORTED_PLAYERS.keys())),
                   DEFAULT_STREAM_METADATA['title'],
                   DEFAULT_STREAM_METADATA['author'],
                   DEFAULT_STREAM_METADATA['category'])
    )

    output = parser.add_argument_group("File output options")
    output.add_argument(
        "-o", "--output",
        metavar="FILENAME",
        help="""
        Write stream data to FILENAME instead of playing it.

        You will be prompted if the file already exists.
        """
    )
    output.add_argument(
        "-f", "--force",
        action="store_true",
        help="""
        When using -o or -r, always write to file even if it already exists.
        """
    )
    output.add_argument(
        "-O", "--stdout",
        action="store_true",
        help="""
        Write stream data to stdout instead of playing it.
        """
    )
    output.add_argument(
        "-r", "--record",
        metavar="FILENAME",
        help="""
        Open the stream in the player, while at the same time writing it to FILENAME.

        You will be prompted if the file already exists.
        """
    )
    output.add_argument(
        "-R", "--record-and-pipe",
        metavar="FILENAME",
        help="""
        Write stream data to stdout, while at the same time writing it to FILENAME.

        You will be prompted if the file already exists.
        """
    )

    stream = parser.add_argument_group("Stream options")
    stream.add_argument(
        "--url",
        dest="url_param",
        metavar="URL",
        help="""
        A URL to attempt to extract streams from.

        Usually, the protocol of http(s) URLs can be omitted (https://),
        depending on the implementation of the plugin being used.

        This is an alternative to setting the URL using a positional argument
        and can be useful if set in a config file.
        """
    )
    stream.add_argument(
        "--default-stream",
        type=comma_list,
        metavar="STREAM",
        help="""
        Stream to play.

        Use ``best`` or ``worst`` for selecting the highest or lowest available
        quality.

        Fallback streams can be specified by using a comma-separated list:

          "720p,480p,best"

        This is an alternative to setting the stream using a positional argument
        and can be useful if set in a config file.
        """
    )
    stream.add_argument(
        "--retry-streams",
        metavar="DELAY",
        type=num(float, min=0),
        help="""
        Retry fetching the list of available streams until streams are found
        while waiting DELAY second(s) between each attempt. If unset, only one
        attempt will be made to fetch the list of streams available.

        The number of fetch retry attempts can be capped with --retry-max.
        """
    )
    stream.add_argument(
        "--retry-max",
        metavar="COUNT",
        type=num(int, min=-1),
        help="""
        When using --retry-streams, stop retrying the fetch after COUNT retry
        attempt(s). Fetch will retry infinitely if COUNT is zero or unset.

        If --retry-max is set without setting --retry-streams, the delay between
        retries will default to 1 second.
        """
    )
    stream.add_argument(
        "--retry-open",
        metavar="ATTEMPTS",
        type=num(int, min=0),
        default=1,
        help="""
        After a successful fetch, try ATTEMPTS time(s) to open the stream until
        giving up.

        Default is 1.
        """
    )
    stream.add_argument(
        "--stream-types", "--stream-priority",
        metavar="TYPES",
        type=comma_list,
        help="""
        A comma-delimited list of stream types to allow.

        The order will be used to separate streams when there are multiple
        streams with the same name but different stream types. Any stream type
        not listed will be omitted from the available streams list.  A ``*`` can
        be used as a wildcard to match any other type of stream, eg. muxed-stream.

        Default is "rtmp,hls,hds,http,akamaihd,*".
        """
    )
    stream.add_argument(
        "--stream-sorting-excludes",
        metavar="STREAMS",
        type=comma_list,
        help="""
        Fine tune the ``best`` and ``worst`` stream name synonyms by excluding unwanted streams.

        If all of the available streams get excluded, ``best`` and ``worst`` will become
        inaccessible and new special stream synonyms ``best-unfiltered`` and ``worst-unfiltered``
        can be used as a fallback selection method.

        Uses a filter expression in the format:

          [operator]<value>

        Valid operators are ``>``, ``>=``, ``<`` and ``<=``. If no operator is specified then
        equality is tested.

        For example this will exclude streams ranked higher than "480p":

          ">480p"

        Multiple filters can be used by separating each expression with a comma.

        For example this will exclude streams from two quality types:

          ">480p,>medium"

        """
    )

    transport = parser.add_argument_group("Stream transport options")
    transport.add_argument(
        "--hds-live-edge",
        type=num(float, min=0),
        metavar="SECONDS",
        help="""
        The time live HDS streams will start from the edge of stream.

        Default is 10.0.
        """
    )
    transport.add_argument(
        "--hds-segment-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
        help="""
        How many attempts should be done to download each HDS segment before
        giving up.

        Default is 3.
        """
    )
    transport.add_argument(
        "--hds-segment-threads",
        type=num(int, max=10),
        metavar="THREADS",
        help="""
        The size of the thread pool used to download HDS segments. Minimum value
        is 1 and maximum is 10.

        Default is 1.
        """
    )
    transport.add_argument(
        "--hds-segment-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        HDS segment connect and read timeout.

        Default is 10.0.
        """
    )
    transport.add_argument(
        "--hds-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from HDS streams.

        Default is 60.0.
        """
    )
    transport.add_argument(
        "--hls-live-edge",
        type=num(int, min=0),
        metavar="SEGMENTS",
        help="""
        How many segments from the end to start live HLS streams on.

        The lower the value the lower latency from the source you will be,
        but also increases the chance of buffering.

        Default is 3.
        """
    )
    transport.add_argument(
        "--hls-segment-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
        help="""
        How many attempts should be done to download each HLS segment before
        giving up.

        Default is 3.
        """
    )
    transport.add_argument(
        "--hls-playlist-reload-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
        help="""
        How many attempts should be done to reload the HLS playlist before
        giving up.

        Default is 3.
        """
    )
    transport.add_argument(
        "--hls-segment-threads",
        type=num(int, max=10),
        metavar="THREADS",
        help="""
        The size of the thread pool used to download HLS segments. Minimum value
        is 1 and maximum is 10.

        Default is 1.
        """
    )
    transport.add_argument(
        "--hls-segment-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        HLS segment connect and read timeout.

        Default is 10.0.
        """)
    transport.add_argument(
        "--hls-segment-ignore-names",
        metavar="NAMES",
        type=comma_list,
        help="""
        A comma-delimited list of segment names that will not be fetched.

        Example: --hls-segment-ignore-names 000,001,002

        This will ignore every segment that ends with 000.ts, 001.ts and 002.ts

        Default is None.

        Note: The --hls-timeout must be increased, to a time that is longer than
        the ignored break.
        """
    )
    transport.add_argument(
        "--hls-segment-key-uri",
        metavar="URI",
        type=str,
        help="""
        URI to segment encryption key. If no URI is specified, the URI contained
        in the segments will be used.

        Example: --hls-segment-key-uri "https://example.com/hls/encryption_key"

        Default is None.
        """
    )
    transport.add_argument(
        "--hls-audio-select",
        type=comma_list,
        metavar="CODE",
        help="""
        Selects a specific audio source or sources, by language code or name,
        when multiple audio sources are available. Can be * to download all
        audio sources.

        Examples:

          --hls-audio-select "English,German"
          --hls-audio-select "en,de"
          --hls-audio-select "*"

        Note: This is only useful in special circumstances where the regular
        locale option fails, such as when multiple sources of the same language
        exists.
        """)
    transport.add_argument(
        "--hls-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from HLS streams.

        Default is 60.0.
        """)
    transport.add_argument(
        "--hls-start-offset",
        type=hours_minutes_seconds,
        metavar="[HH:]MM:SS",
        default=None,
        help="""
        Amount of time to skip from the beginning of the stream. For live
        streams, this is a negative offset from the end of the stream (rewind).

        Default is 00:00:00.
        """)
    transport.add_argument(
        "--hls-duration",
        type=hours_minutes_seconds,
        metavar="[HH:]MM:SS",
        default=None,
        help="""
        Limit the playback duration, useful for watching segments of a stream.
        The actual duration may be slightly longer, as it is rounded to the
        nearest HLS segment.

        Default is unlimited.
        """)
    transport.add_argument(
        "--hls-live-restart",
        action="store_true",
        help="""
        Skip to the beginning of a live stream, or as far back as possible.
        """)
    transport.add_argument(
        "--http-stream-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from HTTP streams.

        Default is 60.0.
        """)
    transport.add_argument(
        "--ringbuffer-size",
        metavar="SIZE",
        type=filesize,
        help="""
        The maximum size of ringbuffer. Add a M or K suffix to specify mega or
        kilo bytes instead of bytes.

        The ringbuffer is used as a temporary storage between the stream and the
        player. This is to allows us to download the stream faster than the
        player wants to read it.

        The smaller the size, the higher chance of the player buffering if there
        are download speed dips and the higher size the more data we can use as
        a storage to catch up from speed dips.

        It also allows you to temporary pause as long as the ringbuffer doesn't
        get full since we continue to download the stream in the background.

        Default is "16M".

        Note: A smaller size is recommended on lower end systems (such as
        Raspberry Pi) when playing stream types that require some extra
        processing (such as HDS) to avoid unnecessary background processing.
        """)
    transport.add_argument(
        "--rtmp-proxy", "--rtmpdump-proxy",
        metavar="PROXY",
        help="""
        A SOCKS proxy that RTMP streams will use.

        Example: 127.0.0.1:9050
        """
    )
    transport.add_argument(
        "--rtmp-rtmpdump", "--rtmpdump",
        metavar="FILENAME",
        help="""
        RTMPDump is used to access RTMP streams. You can specify the
        location of the rtmpdump executable if it is not in your PATH.

        Example: "/usr/local/bin/rtmpdump"
        """
    )
    transport.add_argument(
        "--rtmp-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from RTMP streams.

        Default is 60.0.
        """
    )
    transport.add_argument(
        "--stream-segment-attempts",
        type=num(int, min=0),
        metavar="ATTEMPTS",
        help="""
        How many attempts should be done to download each segment before giving
        up.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 3.
        """
    )
    transport.add_argument(
        "--stream-segment-threads",
        type=num(int, max=10),
        metavar="THREADS",
        help="""
        The size of the thread pool used to download segments. Minimum value is
        1 and maximum is 10.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 1.
        """
    )
    transport.add_argument(
        "--stream-segment-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Segment connect and read timeout.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 10.0.
        """)
    transport.add_argument(
        "--stream-timeout",
        type=num(float, min=0),
        metavar="TIMEOUT",
        help="""
        Timeout for reading data from streams.

        This is generic option used by streams not covered by other options,
        such as stream protocols specific to plugins, e.g. UStream.

        Default is 60.0.
        """)
    transport.add_argument(
        "--stream-url",
        action="store_true",
        help="""
        If possible, translate the stream to a URL and print it.
        """
    )
    transport.add_argument(
        "--subprocess-cmdline", "--cmdline", "-c",
        action="store_true",
        help="""
        Print the command-line used internally to play the stream.

        This is only available on RTMP streams.
        """
    )
    transport.add_argument(
        "--subprocess-errorlog", "--errorlog", "-e",
        action="store_true",
        help="""
        Log possible errors from internal subprocesses to a temporary file. The
        file will be saved in your systems temporary directory.

        Useful when debugging rtmpdump related issues.
        """
    )
    transport.add_argument(
        "--subprocess-errorlog-path", "--errorlog-path",
        type=str,
        metavar="PATH",
        help="""
        Log the subprocess errorlog to a specific file rather than a temporary
        file. Takes precedence over subprocess-errorlog.

        Useful when debugging rtmpdump related issues.
        """
    )
    transport.add_argument(
        "--ffmpeg-ffmpeg",
        metavar="FILENAME",
        help="""
        FFMPEG is used to access or mux separate video and audio streams. You
        can specify the location of the ffmpeg executable if it is not in your
        PATH.

        Example: "/usr/local/bin/ffmpeg"
        """
    )
    transport.add_argument(
        "--ffmpeg-verbose",
        action="store_true",
        help="""
        Write the console output from ffmpeg to the console.
        """
    )
    transport.add_argument(
        "--ffmpeg-verbose-path",
        type=str,
        metavar="PATH",
        help="""
        Path to write the output from the ffmpeg console.
        """
    )
    transport.add_argument(
        "--ffmpeg-video-transcode",
        metavar="CODEC",
        help="""
        When muxing streams transcode the video to this CODEC.

        Default is "copy".

        Example: "h264"
        """
    )
    transport.add_argument(
        "--ffmpeg-audio-transcode",
        metavar="CODEC",
        help="""
        When muxing streams transcode the audio to this CODEC.

        Default is "copy".

        Example: "aac"
        """
    )

    http = parser.add_argument_group("HTTP options")
    http.add_argument(
        "--http-proxy",
        metavar="HTTP_PROXY",
        help="""
        A HTTP proxy to use for all HTTP requests.

        Example: "http://hostname:port/"
        """
    )
    http.add_argument(
        "--https-proxy",
        metavar="HTTPS_PROXY",
        help="""
        A HTTPS capable proxy to use for all HTTPS requests.

        Example: "https://hostname:port/"
        """
    )
    http.add_argument(
        "--http-cookie",
        metavar="KEY=VALUE",
        type=keyvalue,
        action="append",
        help="""
        A cookie to add to each HTTP request.

        Can be repeated to add multiple cookies.
        """
    )
    http.add_argument(
        "--http-header",
        metavar="KEY=VALUE",
        type=keyvalue,
        action="append",
        help="""
        A header to add to each HTTP request.

        Can be repeated to add multiple headers.
        """
    )
    http.add_argument(
        "--http-query-param",
        metavar="KEY=VALUE",
        type=keyvalue,
        action="append",
        help="""
        A query parameter to add to each HTTP request.

        Can be repeated to add multiple query parameters.
        """
    )
    http.add_argument(
        "--http-ignore-env",
        action="store_true",
        help="""
        Ignore HTTP settings set in the environment such as environment
        variables (HTTP_PROXY, etc) or ~/.netrc authentication.
        """
    )
    http.add_argument(
        "--http-no-ssl-verify",
        action="store_true",
        help="""
        Don't attempt to verify SSL certificates.

        Usually a bad idea, only use this if you know what you're doing.
        """
    )
    http.add_argument(
        "--http-disable-dh",
        action="store_true",
        help="""
        Disable Diffie Hellman key exchange

        Usually a bad idea, only use this if you know what you're doing.
        """
    )
    http.add_argument(
        "--http-ssl-cert",
        metavar="FILENAME",
        help="""
        SSL certificate to use.

        Expects a .pem file.
        """
    )
    http.add_argument(
        "--http-ssl-cert-crt-key",
        metavar=("CRT_FILENAME", "KEY_FILENAME"),
        nargs=2,
        help="""
        SSL certificate to use.

        Expects a .crt and a .key file.
        """
    )
    http.add_argument(
        "--http-timeout",
        metavar="TIMEOUT",
        type=num(float, min=0),
        help="""
        General timeout used by all HTTP requests except the ones covered by
        other options.

        Default is 20.0.
        """
    )

    # Deprecated options
    http.add_argument(
        "--http-cookies",
        metavar="COOKIES",
        help=argparse.SUPPRESS
    )
    http.add_argument(
        "--http-headers",
        metavar="HEADERS",
        help=argparse.SUPPRESS
    )
    http.add_argument(
        "--http-query-params",
        metavar="PARAMS",
        help=argparse.SUPPRESS
    )
    general.add_argument(
        "--no-version-check",
        action="store_true",
        help=argparse.SUPPRESS
    )
    return parser