Passed
Push — master ( aeb165...d9ae97 )
by Dean
03:04
created

Movies.run()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963
Metric Value
cc 1
dl 0
loc 5
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 9.4285
1 1
from plugin.core.constants import GUID_SERVICES
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
    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 rating_key, 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
            # Ensure `guid` is available
88
            if not guid or guid.service not in GUID_SERVICES:
89
                mark_unsupported(self.p_unsupported, rating_key, guid)
90
                continue
91
92
            key = (guid.service, guid.id)
93
94
            # Try retrieve `pk` for `key`
95
            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...
96
97
            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...
98
                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...
99
100
                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...
101
                    SyncMedia.Movies, data,
102
103
                    key=rating_key,
104
105
                    guid=guid,
106
                    p_item=p_item,
107
108
                    t_item=t_movie
109
                )
110
111
            # Remove movie from `pending` set
112
            if pk and pk in self.p_pending:
113
                self.p_pending.remove(pk)
114
115
            # Task checkpoint
116
            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...
117
118
        # Stop progress group
119
        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...
120
121
        # Report unsupported movies (unsupported guid)
122
        log_unsupported(log, 'Found %d unsupported movie(s)\n%s', self.p_unsupported)
123
124 1 View Code Duplication
    def process_missing_movies(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
125
        """Trigger actions for movies that are in trakt, but was unable to be found in plex"""
126
127
        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...
128
            # Collection cleaning disabled for individual syncs
129
            return
130
131
        # Increment progress steps
132
        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...
133
134
        # Iterate over movies
135
        for pk in list(self.p_pending):
136
            # Increment one step
137
            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...
138
139
            # Iterate over data handlers
140
            triggered = False
141
142
            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...
143
                if data not in [SyncData.Collection]:
144
                    continue
145
146
                # Retrieve movie
147
                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...
148
149
                if not t_movie:
150
                    continue
151
152
                log.debug('Found movie missing from plex: %r [data: %r]', pk, SyncData.title(data))
153
154
                # Trigger handler
155
                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...
156
                    SyncMedia.Movies, data,
157
158
                    key=None,
159
160
                    guid=Guid.construct(*pk),
161
                    p_item=None,
162
163
                    t_item=t_movie
164
                )
165
166
                # Mark triggered
167
                triggered = True
168
169
            # Check if action was triggered
170
            if not triggered:
171
                continue
172
173
            # Remove movie from `pending` set
174
            self.p_pending.remove(pk)
175
176
        # Stop progress group
177
        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...
178
179
        # Report pending movies (no actions triggered)
180
        self.log_pending('Unable to find %d movie(s) in Plex\n%s', self.p_pending)
181