Test Setup Failed
Push — develop ( e459d6...12ef70 )
by Dean
02:19
created

Movies.process_missing_movies()   C

Complexity

Conditions 7

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 48.6673
Metric Value
cc 7
dl 0
loc 57
ccs 1
cts 19
cp 0.0526
crap 48.6673
rs 6.6397

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1 1
from plugin.sync.core.constants import GUID_AGENTS
2 1
from plugin.sync.core.enums import SyncMedia, SyncData
3 1
from plugin.sync.modes.core.base import log_unsupported, mark_unsupported
4 1
from plugin.sync.modes.push.base import Base
5
6 1
from plex_database.models import MetadataItem, MediaItem
7 1
from plex_metadata import Guid
8 1
import elapsed
9 1
import logging
10
11 1
log = logging.getLogger(__name__)
12
13
14 1
class Movies(Base):
15 1
    def __init__(self, task):
16
        super(Movies, self).__init__(task)
17
18
        # Sections
19
        self.p_sections = None
20
        self.p_sections_map = None
21
22
        # Items
23
        self.p_count = None
24
        self.p_movies = None
25
26
        self.p_pending = None
27
        self.p_unsupported = None
28
29 1
    @elapsed.clock
30
    def construct(self):
31
        # Retrieve movie sections
32
        self.p_sections, self.p_sections_map = self.sections('movie')
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named sections.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
33
34
        # Determine number of movies that will be processed
35
        self.p_count = self.plex.library.movies.count(
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named plex.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
36
            self.p_sections,
37
            account=self.current.account.plex.key
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
38
        )
39
40
        # Increment progress steps total
41
        self.current.progress.group(Movies, 'matched:movies').add(self.p_count)
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
42
        self.current.progress.group(Movies, 'missing:movies')
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
43
44 1
    @elapsed.clock
45
    def start(self):
46
        # Fetch movies with account settings
47
        self.p_movies = self.plex.library.movies.mapped(
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named plex.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
48
            self.p_sections, [
49
                MetadataItem.added_at,
50
                MetadataItem.title,
51
                MetadataItem.year,
52
53
                MediaItem.audio_channels,
54
                MediaItem.audio_codec,
55
                MediaItem.height,
56
                MediaItem.interlaced
57
            ],
58
            account=self.current.account.plex.key,
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
59
            parse_guid=True
60
        )
61
62
        # Reset state
63
        self.p_pending = self.trakt.table.movies.copy()
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named trakt.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
64
        self.p_unsupported = {}
65
66 1
    @elapsed.clock
67
    def run(self):
68
        # Process movies
69
        self.process_matched_movies()
70
        self.process_missing_movies()
71
72 1
    def process_matched_movies(self):
73
        """Trigger actions for movies that have been matched in plex"""
74
75
        # Iterate over movies
76
        for rating_key, guid, p_item in self.p_movies:
77
            # Increment one step
78
            self.current.progress.group(Movies, 'matched:movies').step()
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
79
80
            # Ensure `guid` is available
81
            if not guid or guid.agent not in GUID_AGENTS:
82
                mark_unsupported(self.p_unsupported, rating_key, guid, p_item)
83
                continue
84
85
            key = (guid.agent, guid.sid)
86
87
            # Try retrieve `pk` for `key`
88
            pk = self.trakt.table.get(key)
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named trakt.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
89
90
            for data in self.get_data(SyncMedia.Movies):
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named get_data.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
91
                t_movie = self.trakt[(SyncMedia.Movies, data)].get(pk)
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named trakt.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
92
93
                self.execute_handlers(
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named execute_handlers.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
94
                    SyncMedia.Movies, data,
95
96
                    key=rating_key,
97
98
                    guid=guid,
99
                    p_item=p_item,
100
101
                    t_item=t_movie
102
                )
103
104
            # Remove movie from `pending` set
105
            if pk and pk in self.p_pending:
106
                self.p_pending.remove(pk)
107
108
            # Task checkpoint
109
            self.checkpoint()
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named checkpoint.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
110
111
        # Stop progress group
112
        self.current.progress.group(Movies, 'matched:movies').stop()
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
113
114
        # Report unsupported movies (unsupported guid)
115
        log_unsupported(log, 'Found %d unsupported movie(s)\n%s', self.p_unsupported)
116
117 1
    def process_missing_movies(self):
118
        """Trigger actions for movies that are in trakt, but was unable to be found in plex"""
119
120
        if self.current.kwargs.get('section'):
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
121
            # Collection cleaning disabled for individual syncs
122
            return
123
124
        # Increment progress steps
125
        self.current.progress.group(Movies, 'missing:movies').add(len(self.p_pending))
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
126
127
        # Iterate over movies
128
        for pk in list(self.p_pending):
129
            # Increment one step
130
            self.current.progress.group(Movies, 'missing:movies').step()
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
131
132
            # Iterate over data handlers
133
            triggered = False
134
135
            for data in self.get_data(SyncMedia.Movies):
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named get_data.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
136
                if data not in [SyncData.Collection]:
137
                    continue
138
139
                # Retrieve movie
140
                t_movie = self.trakt[(SyncMedia.Movies, data)].get(pk)
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named trakt.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
141
142
                if not t_movie:
143
                    continue
144
145
                log.debug('Found movie missing from plex: %r [data: %r]', pk, SyncData.title(data))
146
147
                # Trigger handler
148
                self.execute_handlers(
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named execute_handlers.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
149
                    SyncMedia.Movies, data,
150
151
                    key=None,
152
153
                    guid=Guid(*pk),
154
                    p_item=None,
155
156
                    t_item=t_movie
157
                )
158
159
                # Mark triggered
160
                triggered = True
161
162
            # Check if action was triggered
163
            if not triggered:
164
                continue
165
166
            # Remove movie from `pending` set
167
            self.p_pending.remove(pk)
168
169
        # Stop progress group
170
        self.current.progress.group(Movies, 'missing:movies').stop()
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named current.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
171
172
        # Report pending movies (no actions triggered)
173
        self.log_pending('Unable to process %d movie(s)\n%s', self.p_pending)
174