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