From 0f3765749001279fe232914bfa852650c4fd1162 Mon Sep 17 00:00:00 2001 From: Christopher Rosell Date: Wed, 21 Mar 2012 16:24:45 +0100 Subject: Refactor the stream/command line handling. --- src/livestreamer/cli.py | 10 +++++----- src/livestreamer/plugins/__init__.py | 3 --- src/livestreamer/plugins/justintv.py | 37 ++++++++++++++++------------------- src/livestreamer/plugins/ownedtv.py | 21 +++++--------------- src/livestreamer/plugins/ustreamtv.py | 26 +++++++----------------- src/livestreamer/stream.py | 23 ++++++++++++++++++++++ src/livestreamer/utils.py | 7 ++++--- 7 files changed, 61 insertions(+), 66 deletions(-) create mode 100644 src/livestreamer/stream.py (limited to 'src') diff --git a/src/livestreamer/cli.py b/src/livestreamer/cli.py index 1965e7e..58c935c 100644 --- a/src/livestreamer/cli.py +++ b/src/livestreamer/cli.py @@ -36,19 +36,19 @@ def handle_url(args): if args.stream: if args.stream in streams: stream = streams[args.stream] - cmdline = plugin.stream_cmdline(stream, args.output or "-") + cmdline = stream.cmdline(args.output or "-") if args.cmdline: - print(cmdline) + print(cmdline.format()) sys.exit() else: if not args.output: - cmdline = ("{0} | {1} -").format(cmdline, args.player) - os.system(cmdline) + cmdline.pipe = ("{0} -").format(args.player) + + os.system(cmdline.format()) 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)) diff --git a/src/livestreamer/plugins/__init__.py b/src/livestreamer/plugins/__init__.py index 1eaf829..a989799 100644 --- a/src/livestreamer/plugins/__init__.py +++ b/src/livestreamer/plugins/__init__.py @@ -24,9 +24,6 @@ class Plugin(object): def get_streams(self): raise NotImplementedError - def stream_cmdline(self, stream, filename): - raise NotImplementedError - def load_plugins(plugins): for loader, name, ispkg in pkgutil.iter_modules(plugins.__path__): file, pathname, desc = imp.find_module(name, plugins.__path__) diff --git a/src/livestreamer/plugins/justintv.py b/src/livestreamer/plugins/justintv.py index 233a17b..8db4db4 100644 --- a/src/livestreamer/plugins/justintv.py +++ b/src/livestreamer/plugins/justintv.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 from livestreamer.plugins import Plugin, register_plugin -from livestreamer.utils import CommandLine, swfverify +from livestreamer.stream import RTMPStream +from livestreamer.utils import swfverify from livestreamer.compat import urllib, str import xml.dom.minidom, re, sys, random @@ -93,31 +94,27 @@ class JustinTV(Plugin): dom = xml.dom.minidom.parseString(data) nodes = dom.getElementsByTagName("nodes")[0] + swfhash, swfsize = swfverify(self.SWFURL) + for node in nodes.childNodes: - stream = {} + info = {} for child in node.childNodes: - stream[child.tagName] = self._get_node_text(child) + info[child.tagName] = self._get_node_text(child) + + stream = RTMPStream({ + "rtmp": ("{0}/{1}").format(info["connect"], info["play"]), + "swfUrl": self.SWFURL, + "swfhash": swfhash, + "swfsize": swfsize, + "live": 1 + }) + + if "token" in info: + stream.params["jtv"] = info["token"] sname = clean_tag(node.tagName) streams[sname] = stream return streams - def stream_cmdline(self, stream, filename): - swfhash, swfsize = swfverify(self.SWFURL) - - cmd = CommandLine("rtmpdump") - cmd.arg("rtmp", ("{0}/{1}").format(stream["connect"], stream["play"])) - cmd.arg("swfUrl", self.SWFURL) - cmd.arg("swfhash", swfhash) - cmd.arg("swfsize", swfsize) - cmd.arg("live", True) - cmd.arg("flv", filename) - - if "token" in stream: - cmd.arg("jtv", stream["token"]) - - return cmd.format() - - register_plugin("justintv", JustinTV) diff --git a/src/livestreamer/plugins/ownedtv.py b/src/livestreamer/plugins/ownedtv.py index 779dd2c..ab9c479 100644 --- a/src/livestreamer/plugins/ownedtv.py +++ b/src/livestreamer/plugins/ownedtv.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from livestreamer.plugins import Plugin, register_plugin -from livestreamer.utils import CommandLine +from livestreamer.stream import RTMPStream from livestreamer.compat import urllib import xml.dom.minidom, re @@ -76,22 +76,11 @@ class OwnedTV(Plugin): playpath = streamel.getAttribute("name") if not name in streams: - streams[name] = { - "base": base, - "name": name, - "playpath": playpath - } + streams[name] = RTMPStream({ + "rtmp": ("{0}/{1}").format(base, playpath), + "live": 1 + }) return streams - - def stream_cmdline(self, stream, filename): - cmd = CommandLine("rtmpdump") - cmd.arg("rtmp", ("{0}/{1}").format(stream["base"], stream["playpath"])) - cmd.arg("live", True) - cmd.arg("flv", filename) - - return cmd.format() - - register_plugin("own3dtv", OwnedTV) diff --git a/src/livestreamer/plugins/ustreamtv.py b/src/livestreamer/plugins/ustreamtv.py index f191589..5f120f5 100644 --- a/src/livestreamer/plugins/ustreamtv.py +++ b/src/livestreamer/plugins/ustreamtv.py @@ -1,12 +1,11 @@ #!/usr/bin/env python3 from livestreamer.plugins import Plugin, register_plugin -from livestreamer.utils import CommandLine +from livestreamer.stream import RTMPStream from livestreamer.compat import urllib, str, bytes import xml.dom.minidom, re - class UStreamTV(Plugin): AMFURL = "http://cgw.ustream.tv/Viewer/getStream/1/{0}.amf" SWFURL = "http://cdn1.ustream.tv/swf/4/viewer.rsl.210.swf" @@ -40,8 +39,6 @@ class UStreamTV(Plugin): data = fd.read() fd.close() - stream = {} - playpath = get_amf_value(data, "streamName") cdnurl = get_amf_value(data, "cdnUrl") fmsurl = get_amf_value(data, "fmsUrl") @@ -49,22 +46,13 @@ class UStreamTV(Plugin): if not playpath: return False - stream["playpath"] = playpath - stream["rtmp"] = cdnurl or fmsurl - stream["url"] = self.url + stream = RTMPStream({ + "rtmp": ("{0}/{1}").format(cdnurl or fmsurl, playpath), + "pageUrl": self.url, + "swfUrl": self.SWFURL, + "live": 1 + }) return {"live": stream} - - def stream_cmdline(self, stream, filename): - cmd = CommandLine("rtmpdump") - cmd.arg("rtmp", ("{0}/{1}").format(stream["rtmp"], stream["playpath"])) - cmd.arg("swfUrl", self.SWFURL) - cmd.arg("pageUrl", stream["url"]) - cmd.arg("live", True) - cmd.arg("flv", filename) - - return cmd.format() - - register_plugin("ustreamtv", UStreamTV) diff --git a/src/livestreamer/stream.py b/src/livestreamer/stream.py new file mode 100644 index 0000000..4893172 --- /dev/null +++ b/src/livestreamer/stream.py @@ -0,0 +1,23 @@ +from livestreamer.utils import CommandLine + +class Stream(object): + def __init__(self, params={}): + self.params = params + + def cmdline(self, out): + raise NotImplementedError + +class RTMPStream(Stream): + def cmdline(self, out): + cmd = CommandLine("rtmpdump") + + for key, value in self.params.items(): + if key == "live": + if value == 1: + cmd.args[key] = True + + cmd.args[key] = value + + cmd.args["flv"] = out + + return cmd diff --git a/src/livestreamer/utils.py b/src/livestreamer/utils.py index 2b4214d..60a4d6a 100644 --- a/src/livestreamer/utils.py +++ b/src/livestreamer/utils.py @@ -9,9 +9,7 @@ class CommandLine(object): def __init__(self, command): self.command = command self.args = {} - - def arg(self, key, value): - self.args[key] = value + self.pipe = None def format(self): args = [] @@ -26,6 +24,9 @@ class CommandLine(object): args = (" ").join(args) cmdline = ("{0} {1}").format(self.command, args) + if self.pipe: + cmdline += (" | {0}").format(self.pipe) + return cmdline class ArgumentParser(argparse.ArgumentParser): -- cgit v1.2.3