aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/livestreamer/__init__.py
blob: ca176d018fdc5f38fa0e3b64e9479c78b717416d (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from . import plugins
from .compat import urlparse
from .logger import Logger
from .options import Options
from .plugins import PluginError, NoStreamsError, NoPluginError
from .stream import StreamError

import pkgutil
import imp

class Livestreamer(object):
    """
        A Livestreamer session is used to keep track of plugins,
        options and log settings.

    """

    def __init__(self):
        self.options = Options({
            "rtmpdump": None,
            "errorlog": False
        })
        self.plugins = {}
        self.logger = Logger()
        self.load_builtin_plugins()

    def set_option(self, key, value):
        """Set option *key* to *value*."""
        self.options.set(key, value)

    def get_option(self, key):
        """Return option *key*"""
        return self.options.get(key)

    def set_plugin_option(self, plugin, key, value):
        """Set plugin option *key* to *value* for the plugin *plugin*."""
        if plugin in self.plugins:
            plugin = self.plugins[plugin]
            plugin.set_option(key, value)

    def get_plugin_option(self, plugin, key):
        """Return plugin option *key* for the plugin *plugin*."""
        if plugin in self.plugins:
            plugin = self.plugins[plugin]
            return plugin.get_option(key)

    def set_loglevel(self, level):
        """
            Set the log level to *level*.
            Valid levels are: none, error, warning, info, debug.
        """
        self.logger.set_level(level)

    def set_logoutput(self, output):
        """
            Set the log output to *output*. Expects a file like
            object with a write method.
        """
        self.logger.set_output(output)

    def resolve_url(self, url):
        """
            Attempt to find the correct plugin for *url* and return it.
            Raises :exc:`NoPluginError` on failure.
        """
        parsed = urlparse(url)

        if len(parsed.scheme) == 0:
            url = "http://" + url

        for name, plugin in self.plugins.items():
            if plugin.can_handle_url(url):
                obj = plugin(url)
                return obj

        raise NoPluginError

    def get_plugins(self):
        """
            Returns the loaded plugins for the session.
        """
        return self.plugins

    def load_builtin_plugins(self):
        for loader, name, ispkg in pkgutil.iter_modules(plugins.__path__):
            file, pathname, desc = imp.find_module(name, plugins.__path__)
            self.load_plugin(name, file, pathname, desc)

    def load_plugins(self, path):
        """
            Attempt to load plugins from the *path* directory.
        """
        for loader, name, ispkg in pkgutil.iter_modules(path):
            file, pathname, desc = imp.find_module(name, path)
            self.load_plugin(name, file, pathname, desc)

    def load_plugin(self, name, file, pathname, desc):
        module = imp.load_module(name, file, pathname, desc)

        plugin = module.__plugin__
        plugin.module = module.__name__
        plugin.session = self

        self.plugins[module.__name__] = plugin

        if file:
            file.close()

    @property
    def version(self):
        return __version__

__all__ = ["PluginError", "NoStreamsError", "NoPluginError", "StreamError",
           "Livestreamer"]
__version__ = "1.3.1"