|
1
|
|
|
from plugin.managers.core.base import Get, Update |
|
2
|
|
|
from plugin.scrobbler.core.session_prefix import SessionPrefix |
|
3
|
|
|
|
|
4
|
|
|
from plex_metadata import Metadata, Guid |
|
5
|
|
|
import logging |
|
6
|
|
|
|
|
7
|
|
|
log = logging.getLogger(__name__) |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class Base(object): |
|
11
|
|
|
@classmethod |
|
12
|
|
|
def build_session_key(cls, session_key): |
|
13
|
|
|
if type(session_key) is str: |
|
14
|
|
|
return session_key |
|
15
|
|
|
|
|
16
|
|
|
# Prepend session prefix |
|
17
|
|
|
session_prefix = SessionPrefix.get() |
|
18
|
|
|
|
|
19
|
|
|
return '%s:%s' % ( |
|
20
|
|
|
session_prefix, |
|
21
|
|
|
session_key |
|
22
|
|
|
) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class GetSession(Get, Base): |
|
26
|
|
|
pass |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class UpdateSession(Update, Base): |
|
30
|
|
|
@staticmethod |
|
31
|
|
|
def get_account(result): |
|
32
|
|
|
# Try retrieve account from client |
|
33
|
|
|
client = result.get('client') |
|
34
|
|
|
|
|
35
|
|
|
try: |
|
36
|
|
|
client_account_id = client.account_id if client else None |
|
37
|
|
|
except KeyError: |
|
38
|
|
|
client_account_id = None |
|
39
|
|
|
|
|
40
|
|
|
if client_account_id: |
|
41
|
|
|
# Valid account found |
|
42
|
|
|
return client_account_id |
|
43
|
|
|
|
|
44
|
|
|
# Try retrieve account from user |
|
45
|
|
|
user = result.get('user') |
|
46
|
|
|
|
|
47
|
|
|
try: |
|
48
|
|
|
user_account_id = user.account_id if user else None |
|
49
|
|
|
except KeyError: |
|
50
|
|
|
user_account_id = None |
|
51
|
|
|
|
|
52
|
|
|
if user_account_id: |
|
53
|
|
|
# Valid account found |
|
54
|
|
|
return user_account_id |
|
55
|
|
|
|
|
56
|
|
|
return None |
|
57
|
|
|
|
|
58
|
|
|
@staticmethod |
|
59
|
|
|
def get_metadata(rating_key): |
|
60
|
|
|
# Retrieve metadata for `rating_key` |
|
61
|
|
|
try: |
|
62
|
|
|
metadata = Metadata.get(rating_key) |
|
63
|
|
|
except NotImplementedError, e: |
|
64
|
|
|
log.debug('%r, ignoring session', e.message) |
|
65
|
|
|
return None, None |
|
66
|
|
|
|
|
67
|
|
|
# Ensure metadata was returned |
|
68
|
|
|
if not metadata: |
|
69
|
|
|
return None, None |
|
70
|
|
|
|
|
71
|
|
|
# Queue flush for metadata cache |
|
72
|
|
|
Metadata.cache.flush_queue() |
|
73
|
|
|
|
|
74
|
|
|
# Validate metadata |
|
75
|
|
|
if metadata.type not in ['movie', 'episode']: |
|
76
|
|
|
log.info('Ignoring metadata with type %r for rating_key %r', metadata.type, rating_key) |
|
77
|
|
|
return metadata, None |
|
78
|
|
|
|
|
79
|
|
|
# Parse guid |
|
80
|
|
|
guid = Guid.parse(metadata.guid) |
|
81
|
|
|
|
|
82
|
|
|
return metadata, guid |
|
83
|
|
|
|
|
84
|
|
|
@staticmethod |
|
85
|
|
|
def get_progress(duration, view_offset): |
|
86
|
|
|
if duration is None: |
|
87
|
|
|
return None |
|
88
|
|
|
|
|
89
|
|
|
return round((float(view_offset) / duration) * 100, 2) |
|
90
|
|
|
|