Passed
Push — develop ( 7270f6...02f680 )
by Dean
02:23
created

Base.to_view_offset()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.048
Metric Value
cc 2
dl 0
loc 10
ccs 1
cts 5
cp 0.2
crap 4.048
rs 9.4285
1 1
from plugin.sync.core.enums import SyncData, SyncMedia, SyncMode, SyncActionMode
2 1
from plugin.sync.handlers.core import DataHandler, PullHandler, bind
3 1
from plugin.sync.handlers.playback.base import PlaybackHandler
4
5 1
from plex import Plex
6 1
import logging
7
8 1
log = logging.getLogger(__name__)
9
10
11 1
class Base(PullHandler, PlaybackHandler):
0 ignored issues
show
Bug introduced by
The method full which was declared abstract in the super-class MediaHandler
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
Bug introduced by
The method on_removed which was declared abstract in the super-class MediaHandler
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
Bug introduced by
The method push which was declared abstract in the super-class MediaHandler
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
12 1
    @classmethod
13
    def build_action(cls, action, p_item, p_value, t_value, **kwargs):
0 ignored issues
show
Unused Code introduced by
The argument p_value seems to be unused.
Loading history...
14
        data = {}
15
16
        # Retrieve plex parameters
17
        p_duration = p_item.get('part', {}).get('duration')
18
19
        if p_duration is None:
20
            # Missing data required for playback syncing
21
            return None
22
23
        p_settings = p_item.get('settings', {})
24
        p_view_count = p_settings.get('view_count', 0)
25
26
        if p_view_count > 0:
27
            # Ignore items that have been watched in plex
28
            return None
29
30
        data['p_duration'] = p_duration
31
        data['p_value'] = p_item.get('settings', {}).get('view_offset')
32
33
        # Set arguments for action
34
        if action in ['added', 'changed']:
35
            # Expand tuple, convert values to view offsets
36
            if type(t_value) is tuple:
37
                t_previous, t_current = t_value
38
39
                data['t_previous'] = cls.to_view_offset(p_duration, t_previous)
40
                data['t_value'] = cls.to_view_offset(p_duration, t_current)
41
            else:
42
                data['t_value'] = cls.to_view_offset(p_duration, t_value)
43
44
            # Ensure current value is available
45
            if data['t_value'] is None:
46
                # TODO Progress should be cleared on items during fast pulls
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
47
                return None
48
49
            # Ensure progress is above one minute
50
            if data['t_value'] <= 60 * 1000:
51
                return None
52
53
        # Set additional action attributes
54
        data.update(kwargs)
55
56
        return data
57
58 1
    @staticmethod
59
    def to_view_offset(duration, progress):
60
        if not progress:
61
            return None
62
63
        # Convert `progress` to a view offset
64
        view_offset = duration * (float(progress) / 100)
65
66
        # Round `view_offset` to an integer
67
        return int(round(view_offset, 0))
68
69 1
    def update_progress(self, key, time, duration):
70
        action_mode = self.configuration['sync.action.mode']
71
72
        if action_mode == SyncActionMode.Update:
73
            return Plex[':/timeline'].update(key, 'stopped', time, duration)
74
75
        if action_mode == SyncActionMode.Log:
76
            log.info('[%s] update_progress(%r)', key, time)
77
            return True
78
79
        raise NotImplementedError('Unable to update plex, action mode %r not supported', action_mode)
80
81
    #
82
    # Handlers
83
    #
84
85 1
    @bind('added')
86
    def on_added(self, key, p_duration, p_value, t_value, **kwargs):
87
        log.debug('%s.on_added(%s, %r, %r, %r)', self.media, key, p_duration, p_value, t_value)
88
89
        if p_value is not None and p_value > t_value:
90
            # Already updated progress
91
            return
92
93
        return self.update_progress(key, t_value, p_duration)
94
95 1
    @bind('changed')
96
    def on_changed(self, key, p_duration, p_value, t_value, **kwargs):
97
        log.debug('%s.on_changed(%s, %r, %r, %r)', self.media, key, p_duration, p_value, t_value)
98
99
        if p_value > t_value:
100
            # Ignore change, plex progress has advanced
101
            return
102
103
        return self.update_progress(key, t_value, p_duration)
104
105
106 1
class Movies(Base):
0 ignored issues
show
Bug introduced by
The method full which was declared abstract in the super-class MediaHandler
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
Bug introduced by
The method on_removed which was declared abstract in the super-class MediaHandler
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
Bug introduced by
The method push which was declared abstract in the super-class MediaHandler
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
107 1
    media = SyncMedia.Movies
108
109
110 1
class Episodes(Base):
0 ignored issues
show
Bug introduced by
The method full which was declared abstract in the super-class MediaHandler
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
Bug introduced by
The method on_removed which was declared abstract in the super-class MediaHandler
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
Bug introduced by
The method push which was declared abstract in the super-class MediaHandler
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
111 1
    media = SyncMedia.Episodes
112
113
114 1
class Pull(DataHandler):
115 1
    data = SyncData.Playback
116 1
    mode = [SyncMode.FastPull, SyncMode.Pull]
117
118 1
    children = [
119
        Movies,
120
        Episodes
121
    ]
122