1
|
|
|
from core.header import Header |
|
|
|
|
2
|
|
|
from core.helpers import get_class_name, spawn |
|
|
|
|
3
|
|
|
from core.logger import Logger |
|
|
|
|
4
|
|
|
from core.update_checker import UpdateChecker |
|
|
|
|
5
|
|
|
|
6
|
|
|
from plugin.core.constants import ACTIVITY_MODE, PLUGIN_VERSION |
|
|
|
|
7
|
|
|
from plugin.core.cache import CacheManager |
|
|
|
|
8
|
|
|
from plugin.core.helpers.thread import module_start |
|
|
|
|
9
|
|
|
from plugin.core.logger import LOG_HANDLER, LoggerManager |
|
|
|
|
10
|
|
|
from plugin.managers.account import TraktAccountManager |
|
|
|
|
11
|
|
|
from plugin.models import TraktAccount |
|
|
|
|
12
|
|
|
from plugin.modules.core.manager import ModuleManager |
|
|
|
|
13
|
|
|
from plugin.preferences import Preferences |
|
|
|
|
14
|
|
|
from plugin.scrobbler.core.session_prefix import SessionPrefix |
|
|
|
|
15
|
|
|
|
16
|
|
|
from plex import Plex |
|
|
|
|
17
|
|
|
from plex_activity import Activity |
|
|
|
|
18
|
|
|
from plex_metadata import Metadata |
|
|
|
|
19
|
|
|
from requests.packages.urllib3.util import Retry |
|
|
|
|
20
|
|
|
from trakt import Trakt |
|
|
|
|
21
|
|
|
import os |
22
|
|
|
import uuid |
23
|
|
|
|
24
|
|
|
log = Logger() |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class Main(object): |
28
|
|
|
modules = [ |
29
|
|
|
# core |
30
|
|
|
UpdateChecker() |
31
|
|
|
] |
32
|
|
|
|
33
|
|
|
def __init__(self): |
34
|
|
|
Header.show(self) |
35
|
|
|
|
36
|
|
|
LoggerManager.refresh() |
37
|
|
|
|
38
|
|
|
self.init_trakt() |
39
|
|
|
self.init_plex() |
40
|
|
|
self.init() |
41
|
|
|
|
42
|
|
|
ModuleManager.initialize() |
43
|
|
|
|
44
|
|
|
def init(self): |
45
|
|
|
names = [] |
46
|
|
|
|
47
|
|
|
# Initialize modules |
48
|
|
|
for module in self.modules: |
49
|
|
|
names.append(get_class_name(module)) |
50
|
|
|
|
51
|
|
|
if hasattr(module, 'initialize'): |
52
|
|
|
module.initialize() |
53
|
|
|
|
54
|
|
|
log.info('Initialized %s modules: %s', len(names), ', '.join(names)) |
55
|
|
|
|
56
|
|
|
@staticmethod |
57
|
|
|
def init_plex(): |
58
|
|
|
# Ensure client identifier has been generated |
59
|
|
|
if not Dict['plex.client.identifier']: |
|
|
|
|
60
|
|
|
# Generate identifier |
61
|
|
|
Dict['plex.client.identifier'] = uuid.uuid4() |
|
|
|
|
62
|
|
|
|
63
|
|
|
# plex.py |
64
|
|
|
Plex.configuration.defaults.authentication( |
65
|
|
|
os.environ.get('PLEXTOKEN') |
66
|
|
|
) |
67
|
|
|
|
68
|
|
|
Plex.configuration.defaults.client( |
69
|
|
|
identifier=Dict['plex.client.identifier'], |
|
|
|
|
70
|
|
|
|
71
|
|
|
product='trakt (for Plex)', |
72
|
|
|
version=PLUGIN_VERSION |
73
|
|
|
) |
74
|
|
|
|
75
|
|
|
# plex.activity.py |
76
|
|
|
path = os.path.join(LOG_HANDLER.baseFilename, '..', '..', 'Plex Media Server.log') |
77
|
|
|
path = os.path.abspath(path) |
78
|
|
|
|
79
|
|
|
Activity['logging'].add_hint(path) |
80
|
|
|
|
81
|
|
|
# plex.metadata.py |
82
|
|
|
Metadata.configure( |
83
|
|
|
cache=CacheManager.get( |
84
|
|
|
'plex.metadata', |
85
|
|
|
serializer='pickle:///?protocol=2' |
86
|
|
|
), |
87
|
|
|
client=Plex.client |
88
|
|
|
) |
89
|
|
|
|
90
|
|
|
@classmethod |
91
|
|
|
def init_trakt(cls): |
92
|
|
|
# Client |
93
|
|
|
Trakt.configuration.defaults.client( |
94
|
|
|
id='c9ccd3684988a7862a8542ae0000535e0fbd2d1c0ca35583af7ea4e784650a61', |
95
|
|
|
secret='bf00575b1ad252b514f14b2c6171fe650d474091daad5eb6fa890ef24d581f65' |
96
|
|
|
) |
97
|
|
|
|
98
|
|
|
# Application |
99
|
|
|
Trakt.configuration.defaults.app( |
100
|
|
|
name='trakt (for Plex)', |
101
|
|
|
version=PLUGIN_VERSION |
102
|
|
|
) |
103
|
|
|
|
104
|
|
|
# Setup request retrying |
105
|
|
|
Trakt.http.adapter_kwargs = {'max_retries': Retry(total=3, read=0)} |
106
|
|
|
Trakt.http.rebuild() |
107
|
|
|
|
108
|
|
|
Trakt.on('oauth.token_refreshed', cls.on_token_refreshed) |
109
|
|
|
|
110
|
|
|
@classmethod |
111
|
|
|
def on_token_refreshed(cls, authorization): |
112
|
|
|
log.debug('Authentication - PIN authorization refreshed') |
113
|
|
|
|
114
|
|
|
# Retrieve trakt account matching this `authorization` |
115
|
|
|
with Trakt.configuration.http(retry=True).oauth(token=authorization.get('access_token')): |
116
|
|
|
settings = Trakt['users/settings'].get() |
117
|
|
|
|
118
|
|
|
if not settings: |
119
|
|
|
log.warn('Authentication - Unable to retrieve account details for authorization') |
120
|
|
|
return |
121
|
|
|
|
122
|
|
|
# Retrieve trakt account username from `settings` |
123
|
|
|
username = settings.get('user', {}).get('username') |
124
|
|
|
|
125
|
|
|
if not username: |
126
|
|
|
log.warn('Authentication - Unable to retrieve username for authorization') |
127
|
|
|
return None |
128
|
|
|
|
129
|
|
|
# Find matching trakt account |
130
|
|
|
trakt_account = (TraktAccount |
131
|
|
|
.select() |
132
|
|
|
.where( |
133
|
|
|
TraktAccount.username == username |
134
|
|
|
) |
135
|
|
|
).first() |
136
|
|
|
|
137
|
|
|
if not trakt_account: |
138
|
|
|
log.warn('Authentication - Unable to find TraktAccount with the username %r', username) |
139
|
|
|
|
140
|
|
|
# Update oauth credential |
141
|
|
|
TraktAccountManager.update.from_dict(trakt_account, { |
142
|
|
|
'authorization': { |
143
|
|
|
'oauth': authorization |
144
|
|
|
} |
145
|
|
|
}) |
146
|
|
|
|
147
|
|
|
log.info('Authentication - Updated OAuth credential for %r', trakt_account) |
148
|
|
|
|
149
|
|
|
def start(self): |
150
|
|
|
# Construct main thread |
151
|
|
|
spawn(self.run, thread_name='main') |
152
|
|
|
|
153
|
|
|
def run(self): |
154
|
|
|
# Check for authentication token |
155
|
|
|
log.info('X-Plex-Token: %s', 'available' if os.environ.get('PLEXTOKEN') else 'unavailable') |
156
|
|
|
|
157
|
|
|
# Process server startup state |
158
|
|
|
self.process_server_state() |
159
|
|
|
|
160
|
|
|
# Start new-style modules |
161
|
|
|
module_start() |
162
|
|
|
|
163
|
|
|
# Start modules |
164
|
|
|
names = [] |
165
|
|
|
|
166
|
|
|
for module in self.modules: |
167
|
|
|
if not hasattr(module, 'start'): |
168
|
|
|
continue |
169
|
|
|
|
170
|
|
|
names.append(get_class_name(module)) |
171
|
|
|
|
172
|
|
|
module.start() |
173
|
|
|
|
174
|
|
|
log.info('Started %s modules: %s', len(names), ', '.join(names)) |
175
|
|
|
|
176
|
|
|
ModuleManager.start() |
177
|
|
|
|
178
|
|
|
# Start plex.activity.py |
179
|
|
|
Activity.start(ACTIVITY_MODE.get(Preferences.get('activity.mode'))) |
180
|
|
|
|
181
|
|
|
@classmethod |
182
|
|
|
def process_server_state(cls): |
183
|
|
|
# Check startup state |
184
|
|
|
server = Plex.detail() |
185
|
|
|
|
186
|
|
|
if server is None: |
187
|
|
|
log.info('Unable to check startup state, detail request failed') |
188
|
|
|
return |
189
|
|
|
|
190
|
|
|
# Check server startup state |
191
|
|
|
if server.start_state is None: |
192
|
|
|
return |
193
|
|
|
|
194
|
|
|
if server.start_state == 'startingPlugins': |
195
|
|
|
return cls.on_starting_plugins() |
196
|
|
|
|
197
|
|
|
log.error('Unhandled server start state %r', server.start_state) |
198
|
|
|
|
199
|
|
|
@staticmethod |
200
|
|
|
def on_starting_plugins(): |
201
|
|
|
log.debug('on_starting_plugins') |
202
|
|
|
|
203
|
|
|
SessionPrefix.increment() |
204
|
|
|
|
205
|
|
|
@staticmethod |
206
|
|
|
def on_configuration_changed(): |
207
|
|
|
LoggerManager.refresh() |
208
|
|
|
|