forked from CastagnaIT/plugin.video.netflix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.py
101 lines (86 loc) · 3.33 KB
/
addon.py
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
# -*- coding: utf-8 -*-
# Author: asciidisco
# Module: default
# Created on: 13.01.2017
# License: MIT https://goo.gl/5bMj3H
# pylint: disable=wrong-import-position
"""Kodi plugin for Netflix (https://netflix.com)"""
from __future__ import unicode_literals
import sys
from functools import wraps
import xbmcplugin
# Import and intiliaze globals right away to avoid stale values from the last
# addon invocation. Otherwise Kodi's reuseLanguageInvoker will caus some
# really quirky behavior!
from resources.lib.globals import g
g.init_globals(sys.argv)
import resources.lib.common as common
import resources.lib.api.shakti as api
import resources.lib.kodi.ui as ui
import resources.lib.navigation as nav
import resources.lib.navigation.directory as directory
import resources.lib.navigation.hub as hub
import resources.lib.navigation.player as player
import resources.lib.navigation.actions as actions
import resources.lib.navigation.library as library
from resources.lib.api.exceptions import NotLoggedInError
NAV_HANDLERS = {
g.MODE_DIRECTORY: directory.DirectoryBuilder,
g.MODE_ACTION: actions.AddonActionExecutor,
g.MODE_LIBRARY: library.LibraryActionExecutor,
g.MODE_HUB: hub.HubBrowser
}
def lazy_login(func):
"""
Decorator to ensure that a valid login is present when calling a method
"""
# pylint: disable=protected-access, missing-docstring
@wraps(func)
def lazy_login_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except NotLoggedInError:
common.debug('Tried to perform an action without being logged in')
if api.login():
common.debug('Now that we\'re logged in, let\'s try again')
return func(*args, **kwargs)
else:
common.debug('Login failed or canceled, abort initial action')
xbmcplugin.endOfDirectory(handle=g.PLUGIN_HANDLE,
succeeded=False)
return lazy_login_wrapper
@lazy_login
def route(pathitems):
"""Route to the appropriate handler"""
common.debug('Routing navigation request')
root_handler = pathitems[0] if pathitems else g.MODE_DIRECTORY
if root_handler == g.MODE_PLAY:
player.play(pathitems=pathitems[1:])
elif root_handler == 'extrafanart':
common.debug('Ignoring extrafanart invocation')
xbmcplugin.endOfDirectory(handle=g.PLUGIN_HANDLE, succeeded=False)
elif root_handler not in NAV_HANDLERS:
raise nav.InvalidPathError(
'No root handler for path {}'.format('/'.join(pathitems)))
else:
nav.execute(NAV_HANDLERS[root_handler], pathitems[1:],
g.REQUEST_PARAMS)
if __name__ == '__main__':
# pylint: disable=broad-except
# Initialize variables in common module scope
# (necessary when reusing language invoker)
common.info('Started (Version {})'.format(g.VERSION))
common.info('URL is {}'.format(g.URL))
success = False
try:
route(filter(None, g.PATH.split('/')))
success = True
except common.BackendNotReady:
ui.show_backend_not_ready()
except Exception as exc:
import traceback
common.error(traceback.format_exc())
ui.show_addon_error_info(exc)
xbmcplugin.endOfDirectory(g.PLUGIN_HANDLE, succeeded=success)
g.CACHE.commit()
common.log_time_trace()