Test Failed
Push — develop ( db2443...9e1b8b )
by Dean
05:56 queued 03:05
created

UpdateSession.get_part()   A

Complexity

Conditions 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
crap 6
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
import math
7
8
log = logging.getLogger(__name__)
9
10
11
class Base(object):
12
    @classmethod
13
    def build_session_key(cls, session_key):
14
        if type(session_key) is str:
15
            return session_key
16
17
        # Prepend session prefix
18
        session_prefix = SessionPrefix.get()
19
20
        return '%s:%s' % (
21
            session_prefix,
22
            session_key
23
        )
24
25
26
class GetSession(Get, Base):
27
    pass
28
29
30
class UpdateSession(Update, Base):
31
    @staticmethod
32
    def get_account(result):
33
        # Try retrieve account from client
34
        client = result.get('client')
35
36
        try:
37
            client_account_id = client.account_id if client else None
38
        except KeyError:
39
            client_account_id = None
40
41
        if client_account_id:
42
            # Valid account found
43
            return client_account_id
44
45
        # Try retrieve account from user
46
        user = result.get('user')
47
48
        try:
49
            user_account_id = user.account_id if user else None
50
        except KeyError:
51
            user_account_id = None
52
53
        if user_account_id:
54
            # Valid account found
55
            return user_account_id
56
57
        return None
58
59
    @staticmethod
60
    def get_metadata(rating_key):
61
        # Retrieve metadata for `rating_key`
62
        try:
63
            metadata = Metadata.get(rating_key)
64
        except NotImplementedError, e:
65
            log.debug('%r, ignoring session', e.message)
66
            return None, None
67
68
        # Ensure metadata was returned
69
        if not metadata:
70
            return None, None
71
72
        # Queue flush for metadata cache
73
        Metadata.cache.flush_queue()
74
75
        # Validate metadata
76
        if metadata.type not in ['movie', 'episode']:
77
            log.info('Ignoring metadata with type %r for rating_key %r', metadata.type, rating_key)
78
            return metadata, None
79
80
        # Parse guid
81
        guid = Guid.parse(metadata.guid)
82
83
        return metadata, guid
84
85
    @staticmethod
86
    def get_part(duration, view_offset, part_count):
87
        if duration is None:
88
            return 1
89
90
        part_duration = int(math.floor(
91
            float(duration) / part_count
92
        ))
93
94
        # Calculate current part number
95
        part = int(math.floor(
96
            float(view_offset) / part_duration
97
        )) + 1
98
99
        # Clamp `part` to: 0 - `total_parts`
100
        return part_duration, max(0, min(part, part_count))
101
102
    @staticmethod
103
    def get_progress(duration, view_offset, part=1, part_count=1, part_duration=None):
104
        if duration is None:
105
            return None
106
107
        if part_count > 1 and part_duration is not None:
108
            # Update attributes for part progress calculations
109
            duration = part_duration
110
            view_offset -= (part_duration * (part - 1))
111
112
        # Calculate progress (0 - 100)
113
        return round((float(view_offset) / duration) * 100, 2)
114