Test Failed
Push — develop ( 572338...fb7300 )
by Dean
02:29
created

Movies.process_matched_movies()   C

Complexity

Conditions 7

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56
Metric Value
cc 7
dl 0
loc 44
ccs 0
cts 16
cp 0
crap 56
rs 5.5
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
    @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
    @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
    @elapsed.clock
67
    def run(self):
68
        # Process movies
69
        self.process_matched_movies()
70
        self.process_missing_movies()
71
72
    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, p_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 `p_guid` is available
81
            if not p_guid or p_guid.agent not in GUID_AGENTS:
82
                mark_unsupported(self.p_unsupported, rating_key, p_guid, p_item)
83
                continue
84
85
            key = (p_guid.agent, p_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
                    p_guid=p_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
    def process_missing_movies(self):
118
        """Trigger actions for movies that are in trakt, but was unable to be found in plex"""
119
120
        # Increment progress steps
121
        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...
122
123
        # Iterate over movies
124
        for pk in list(self.p_pending):
125
            # Increment one step
126
            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...
127
128
            # Iterate over data handlers
129
            triggered = False
130
131
            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...
132
                if data not in [SyncData.Collection]:
133
                    continue
134
135
                # Retrieve movie
136
                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...
137
138
                if not t_movie:
139
                    continue
140
141
                log.debug('Found movie missing from plex: %r [data: %r]', pk, SyncData.title(data))
142
143
                # Trigger handler
144
                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...
145
                    SyncMedia.Movies, data,
146
147
                    key=None,
148
149
                    p_guid=Guid(*pk),
150
                    p_item=None,
151
152
                    t_item=t_movie
153
                )
154
155
                # Mark triggered
156
                triggered = True
157
158
            # Check if action was triggered
159
            if not triggered:
160
                continue
161
162
            # Remove movie from `pending` set
163
            self.p_pending.remove(pk)
164
165
        # Stop progress group
166
        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...
167
168
        # Report pending movies (no actions triggered)
169
        self.log_pending('Unable to process %d movie(s)\n%s', self.p_pending)
170