Passed
Push — develop ( af752c...572338 )
by Dean
02:33
created

Base.build_action()   B

Complexity

Conditions 5

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 26.2588
Metric Value
cc 5
dl 0
loc 35
ccs 1
cts 19
cp 0.0526
crap 26.2588
rs 8.0894
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
    @staticmethod
13
    def build_action(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_value'] = p_item.get('settings', {}).get('view_offset')
31
32
        # Set arguments for action
33
        if action in ['added', 'changed']:
34
            # Calculate trakt view offset
35
            t_value = p_duration * (float(t_value) / 100)
36
            t_value = int(round(t_value, 0))
37
38
            data['p_duration'] = p_duration
39
            data['t_value'] = t_value
40
41
            if t_value <= 60 * 1000:
42
                # Ignore progress below one minute
43
                return None
44
45
        data.update(kwargs)
46
        return data
47
48 1
    def update_progress(self, key, time, duration):
49
        action_mode = self.configuration['sync.action.mode']
50
51
        if action_mode == SyncActionMode.Update:
52
            return Plex[':/timeline'].update(key, 'stopped', time, duration)
53
54
        if action_mode == SyncActionMode.Log:
55
            log.info('[%s] update_progress(%r)', key, time)
56
            return True
57
58
        raise NotImplementedError('Unable to update plex, action mode %r not supported', action_mode)
59
60
    #
61
    # Handlers
62
    #
63
64 1
    @bind('added')
65
    def on_added(self, key, p_duration, p_value, t_value, **kwargs):
66
        log.debug('%s.on_added(%s, %r, %r, %r)', self.media, key, p_duration, p_value, t_value)
67
68
        if p_value is not None and p_value > t_value:
69
            # Already updated progress
70
            return
71
72
        return self.update_progress(key, t_value, p_duration)
73
74 1
    @bind('changed')
75
    def on_changed(self, key, p_duration, p_value, t_value, **kwargs):
76
        log.debug('%s.on_changed(%s, %r, %r, %r)', self.media, key, p_duration, p_value, t_value)
77
78
        if p_value > t_value:
79
            # Ignore change, plex progress has advanced
80
            return
81
82
        return self.update_progress(key, t_value, p_duration)
83
84
85 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...
86 1
    media = SyncMedia.Movies
87
88
89 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...
90 1
    media = SyncMedia.Episodes
91
92
93 1
class Pull(DataHandler):
94 1
    data = SyncData.Playback
95 1
    mode = [SyncMode.FastPull, SyncMode.Pull]
96
97 1
    children = [
98
        Movies,
99
        Episodes
100
    ]
101