aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/livestreamer/cli.py
blob: 1965e7e4bc2e61140b089777fa209bead65cfb11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3

import sys, os

import livestreamer

parser = livestreamer.utils.ArgumentParser(description="Util to play various livestreaming services in a custom player",
                                           fromfile_prefix_chars="@")
parser.add_argument("url", help="URL to stream", nargs="?")
parser.add_argument("stream", help="stream to play", nargs="?")
parser.add_argument("-p", "--player", metavar="player", help="commandline for player", default="vlc")
parser.add_argument("-o", "--output", metavar="filename", help="write stream to file instead of playing it")
parser.add_argument("-c", "--cmdline", action="store_true", help="print commandline used internally to play stream")
parser.add_argument("-l", "--plugins", action="store_true", help="print installed plugins")


def exit(msg):
    sys.stderr.write("error: " + msg + "\n")
    sys.exit()

def handle_url(args):
    (pluginname, plugin) = livestreamer.resolve_url(args.url)

    if not plugin:
        exit(("No plugin can handle url: {0}").format(args.url))

    streams = plugin.get_streams()

    if not streams:
        exit(("No streams found on url: {0}").format(args.url))

    keys = list(streams.keys())
    keys.sort()
    validstreams = (", ").join(keys)

    if args.stream:
        if args.stream in streams:
            stream = streams[args.stream]
            cmdline = plugin.stream_cmdline(stream, args.output or "-")

            if args.cmdline:
                print(cmdline)
                sys.exit()
            else:
                if not args.output:
                    cmdline = ("{0} | {1} -").format(cmdline, args.player)
                os.system(cmdline)
        else:
            print(("This channel does not have stream: {0}").format(args.stream))
            print(("Valid streams: {0}").format(validstreams))
            sys.exit()
    else:
        print(("Found streams: {0}").format(validstreams))


def print_plugins():
    pluginlist = list(livestreamer.get_plugins().keys())
    print(("Installed plugins: {0}").format(", ".join(pluginlist)))


def main():
    for name, plugin in livestreamer.get_plugins().items():
        plugin.handle_parser(parser)

    arglist = sys.argv[1:]

    if os.path.exists(livestreamer.RCFILE):
        arglist.insert(0, "@" + livestreamer.RCFILE)

    args = parser.parse_args(arglist)

    for name, plugin in livestreamer.get_plugins().items():
        plugin.handle_args(args)

    if args.url:
        handle_url(args)
    elif args.plugins:
        print_plugins()
    else:
        parser.print_help()