Passed
Push — develop ( b3bfc7...6fb855 )
by Dean
02:36
created

GetSession

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
Duplicated Lines 0 %

Test Coverage

Coverage 0%
Metric Value
dl 0
loc 2
ccs 0
cts 2
cp 0
wmc 0
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
        # Queue flush for metadata cache
68
        Metadata.cache.flush_queue()
69
70
        # Validate metadata
71
        if metadata.type not in ['movie', 'episode']:
72
            log.info('Ignoring metadata with type %r for rating_key %r', metadata.type, rating_key)
73
            return metadata, None
74
75
        # Parse guid
76
        guid = Guid.parse(metadata.guid)
77
78
        return metadata, guid
79
80
    @staticmethod
81
    def get_progress(duration, view_offset):
82
        if duration is None:
83
            return None
84
85
        return round((float(view_offset) / duration) * 100, 2)
86