Passed
Push — beta ( 8befa3...d680d6 )
by Dean
06:01 queued 03:09
created

Movies.process_matched_movies()   B

Complexity

Conditions 6

Size

Total Lines 46

Duplication

Lines 46
Ratio 100 %

Code Coverage

Tests 1
CRAP Score 36.0156

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 46
loc 46
ccs 1
cts 17
cp 0.0588
rs 7.5748
cc 6
crap 36.0156
1 1
from plugin.core.constants import GUID_SERVICES
0 ignored issues
show
Unused Code introduced by
Unused GUID_SERVICES imported from plugin.core.constants
Loading history...
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
0 ignored issues
show
Bug introduced by
The name base does not seem to exist in module plugin.sync.modes.push.
Loading history...
Configuration introduced by
Unable to import 'plugin.sync.modes.push.base' (invalid syntax (<string>, line 37))

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
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
    data = [
16
        SyncData.Collection,
17
        SyncData.Playback,
18
        SyncData.Ratings,
19
        SyncData.Watched
20
    ]
21
22 1
    def __init__(self, task):
23
        super(Movies, self).__init__(task)
24
25
        # Sections
26
        self.p_sections = None
27
        self.p_sections_map = None
28
29
        # Items
30
        self.p_count = None
31
        self.p_movies = None
32
33
        self.p_pending = None
34
        self.p_unsupported = None
35
36 1
    @elapsed.clock
37
    def construct(self):
38
        # Retrieve movie sections
39
        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...
40
41
        # Determine number of movies that will be processed
42
        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...
43
            self.p_sections,
44
            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...
45
        )
46
47
        # Increment progress steps total
48
        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...
49
        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...
50
51 1
    @elapsed.clock
52
    def start(self):
53
        # Fetch movies with account settings
54
        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...
55
            self.p_sections, [
56
                MetadataItem.added_at,
57
                MetadataItem.title,
58
                MetadataItem.year,
59
60
                MediaItem.audio_channels,
61
                MediaItem.audio_codec,
62
                MediaItem.height,
63
                MediaItem.interlaced
64
            ],
65
            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...
66
            parse_guid=True
67
        )
68
69
        # Reset state
70
        self.p_pending = self.trakt.table.movie_keys.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...
71
        self.p_unsupported = {}
72
73 1
    @elapsed.clock
74
    def run(self):
75
        # Process movies
76
        self.process_matched_movies()
77
        self.process_missing_movies()
78
79 1 View Code Duplication
    def process_matched_movies(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
80
        """Trigger actions for movies that have been matched in plex"""
81
82
        # Iterate over movies
83
        for mo_id, p_guid, p_item in self.p_movies:
84
            # Increment one step
85
            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...
86
87
            # Process `p_guid` (map + validate)
88
            supported, p_guid = self.process_guid(p_guid)
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named process_guid.

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
            if not supported:
91
                mark_unsupported(self.p_unsupported, mo_id, p_guid)
92
                continue
93
94
            key = (p_guid.service, p_guid.id)
95
96
            # Try retrieve `pk` for `key`
97
            pk = self.trakt.table('movies').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...
98
99
            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...
100
                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...
101
102
                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...
103
                    SyncMedia.Movies, data,
104
105
                    key=mo_id,
106
107
                    guid=p_guid,
108
                    p_item=p_item,
109
110
                    t_item=t_movie
111
                )
112
113
            # Remove movie from `pending` set
114
            if pk and pk in self.p_pending:
115
                self.p_pending.remove(pk)
116
117
            # Task checkpoint
118
            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...
119
120
        # Stop progress group
121
        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...
122
123
        # Report unsupported movies (unsupported guid)
124
        log_unsupported(log, 'Found %d unsupported movie(s)', self.p_unsupported)
125
126 1
    def process_missing_movies(self):
127
        """Trigger actions for movies that are in trakt, but was unable to be found in plex"""
128
129
        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...
130
            # Collection cleaning disabled for individual syncs
131
            return
132
133
        # Increment progress steps
134
        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...
135
136
        # Iterate over movies
137
        for pk in list(self.p_pending):
138
            # Increment one step
139
            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...
140
141
            # Iterate over data handlers
142
            triggered = False
143
144
            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...
145
                if data not in [SyncData.Collection]:
146
                    continue
147
148
                # Retrieve movie
149
                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...
150
151
                if not t_movie:
152
                    continue
153
154
                log.debug('Found movie missing from plex: %r [data: %r]', pk, SyncData.title(data))
155
156
                # Trigger handler
157
                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...
158
                    SyncMedia.Movies, data,
159
160
                    key=None,
161
162
                    guid=Guid.construct(*pk),
163
                    p_item=None,
164
165
                    t_item=t_movie
166
                )
167
168
                # Mark triggered
169
                triggered = True
170
171
            # Check if action was triggered
172
            if not triggered:
173
                continue
174
175
            # Remove movie from `pending` set
176
            self.p_pending.remove(pk)
177
178
        # Stop progress group
179
        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...
180
181
        # Report pending movies (no actions triggered)
182
        self.log_pending(
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named log_pending.

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...
183
            log, 'Unable to find %d movie(s) in Plex, list has been saved to: %s',
184
            self.current.account, 'movies', 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...
185
        )
186