aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Christopher Rosell <chrippa@tanuki.se>2012-03-21 16:24:45 +0100
committerGravatar Christopher Rosell <chrippa@tanuki.se>2012-03-21 16:24:45 +0100
commit0f3765749001279fe232914bfa852650c4fd1162 (patch)
tree4236330f90d37783ddb76418a106b70d3912d6c1 /src
parentf175241692dfc461d773589653fd55c47508a293 (diff)
Refactor the stream/command line handling.
Diffstat (limited to 'src')
-rw-r--r--src/livestreamer/cli.py10
-rw-r--r--src/livestreamer/plugins/__init__.py3
-rw-r--r--src/livestreamer/plugins/justintv.py37
-rw-r--r--src/livestreamer/plugins/ownedtv.py21
-rw-r--r--src/livestreamer/plugins/ustreamtv.py26
-rw-r--r--src/livestreamer/stream.py23
-rw-r--r--src/livestreamer/utils.py7
7 files changed, 61 insertions, 66 deletions
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):