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

Shows.start()   A

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963
Metric Value
cc 1
dl 0
loc 13
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 9.4285
1 1
from plugin.sync.core.constants import GUID_AGENTS
2 1
from plugin.sync.core.enums import SyncData, SyncMedia, SyncMode
3 1
from plugin.sync.modes.core.base import Mode, log_unsupported, mark_unsupported
4
5 1
from plex_database.models import MetadataItem
6 1
from trakt_sync.cache.main import Cache
7 1
import elapsed
8 1
import logging
9
10 1
log = logging.getLogger(__name__)
11
12
13 1
class Shows(Mode):
14 1
    mode = SyncMode.FastPull
15
16 1
    def __init__(self, task):
17
        super(Shows, self).__init__(task)
18
19
        # Sections
20
        self.p_sections = None
21
        self.p_sections_map = None
22
23
        # Shows
24
        self.p_shows = None
25
        self.p_shows_count = None
26
        self.p_shows_unsupported = None
27
28
        # Seasons
29
        self.p_seasons = None
30
31
        # Episodes
32
        self.p_episodes = None
33
        self.p_episodes_count = None
34
35 1
    @elapsed.clock
36
    def construct(self):
37
        # Retrieve show sections
38
        self.p_sections, self.p_sections_map = self.sections('show')
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
39
40
        # Determine number of shows that will be processed
41
        self.p_shows_count = self.plex.library.shows.count(
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
42
            self.p_sections
43
        )
44
45
        # Determine number of shows that will be processed
46
        self.p_episodes_count = self.plex.library.episodes.count_items(
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
47
            self.p_sections
48
        )
49
50
        # Increment progress steps total
51
        self.current.progress.group(Shows, 'shows').add(self.p_shows_count)
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
52
        self.current.progress.group(Shows, 'episodes').add(self.p_episodes_count)
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
53
54 1
    @elapsed.clock
55
    def start(self):
56
        # Fetch episodes with account settings
57
        self.p_shows, self.p_seasons, self.p_episodes = self.plex.library.episodes.mapped(
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
58
            self.p_sections, ([
59
                MetadataItem.library_section
60
            ], [], []),
61
            account=self.current.account.plex.key,
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
62
            parse_guid=True
63
        )
64
65
        # Reset state
66
        self.p_shows_unsupported = {}
67
68 1
    @elapsed.clock
69
    def run(self):
70
        # TODO process seasons
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
71
72
        with elapsed.clock(Shows, 'run:shows'):
73
            # Process shows
74
            for sh_id, guid, p_show in self.p_shows:
75
                # Increment one step
76
                self.current.progress.group(Shows, 'shows').step()
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
77
78
                # Ensure `guid` is available
79
                if not guid or guid.agent not in GUID_AGENTS:
80
                    mark_unsupported(self.p_shows_unsupported, sh_id, guid, p_show)
81
                    continue
82
83
                key = (guid.agent, guid.sid)
84
85
                log.debug('Processing show: %s', key)
86
87
                # Try retrieve `pk` for `key`
88
                pk = self.trakt.table.get(key)
0 ignored issues
show
Bug introduced by
The Instance of Shows 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
                # Store in item map
91
                self.current.map.add(p_show.get('library_section'), sh_id, [key, pk])
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
92
93
                if pk is None:
94
                    # No `pk` found
95
                    continue
96
97
                # Iterate over changed data
98
                for key, result in self.trakt.changes:
0 ignored issues
show
Bug introduced by
The Instance of Shows 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
                    media, data = key[0:2]
100
101
                    if media != SyncMedia.Shows:
102
                        # Ignore changes that aren't for episodes
103
                        continue
104
105
                    if data == SyncData.Watchlist:
106
                        # Ignore watchlist data
107
                        continue
108
109
                    if not self.is_data_enabled(data):
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named is_data_enabled.

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
                        # Data type has been disabled
111
                        continue
112
113
                    data_name = Cache.Data.get(data)
114
115
                    if data_name not in result.changes:
116
                        # No changes for collection
117
                        continue
118
119
                    for action, shows in result.changes[data_name].items():
120
                        t_show = shows.get(pk)
121
122
                        if t_show is None:
123
                            # Unable to find matching show in trakt data
124
                            continue
125
126
                        # Execute show handlers
127
                        self.execute_handlers(
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
128
                            SyncMedia.Shows, data,
129
                            action=action,
130
131
                            key=sh_id,
132
133
                            p_item=p_show,
134
                            t_item=t_show
135
                        )
136
137
        # Stop progress group
138
        self.current.progress.group(Shows, 'shows').stop()
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
139
140
        with elapsed.clock(Shows, 'run:episodes'):
141
            # Process episodes
142
            for ids, guid, (season_num, episode_num), p_show, p_season, p_episode in self.p_episodes:
0 ignored issues
show
Unused Code introduced by
The variable p_season seems to be unused.
Loading history...
143
                # Increment one step
144
                self.current.progress.group(Shows, 'episodes').step()
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
145
146
                # Ensure `guid` is available
147
                if not guid or guid.agent not in GUID_AGENTS:
148
                    mark_unsupported(self.p_shows_unsupported, ids['show'], guid, p_show)
149
                    continue
150
151
                key = (guid.agent, guid.sid)
152
153
                log.debug('Processing episode: %s - S%02dE%02d', key, season_num, episode_num)
154
155
                # Try retrieve `pk` for `key`
156
                pk = self.trakt.table.get(key)
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
157
158
                if pk is None:
159
                    # No `pk` found
160
                    continue
161
162
                if not ids.get('episode'):
163
                    # Missing `episode` rating key
164
                    continue
165
166
                for key, result in self.trakt.changes:
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
167
                    media, data = key[0:2]
168
169
                    if media != SyncMedia.Episodes:
170
                        # Ignore changes that aren't for episodes
171
                        continue
172
173
                    if not self.is_data_enabled(data):
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named is_data_enabled.

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...
174
                        # Data type has been disabled
175
                        continue
176
177
                    data_name = Cache.Data.get(data)
178
179
                    if data_name not in result.changes:
180
                        # No changes for collection
181
                        continue
182
183
                    for action, shows in result.changes[data_name].items():
184
                        t_show = shows.get(pk)
185
186
                        if t_show is None:
187
                            # Unable to find matching show in trakt data
188
                            continue
189
190
                        t_season = t_show.get('seasons', {}).get(season_num)
191
192
                        if t_season is None:
193
                            # Unable to find matching season in `t_show`
194
                            continue
195
196
                        t_episode = t_season.get('episodes', {}).get(episode_num)
197
198
                        if t_episode is None:
199
                            # Unable to find matching episode in `t_season`
200
                            continue
201
202
                        self.execute_handlers(
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
203
                            SyncMedia.Episodes, data,
204
                            action=action,
205
                            key=ids['episode'],
206
207
                            p_item=p_episode,
208
                            t_item=t_episode
209
                        )
210
211
                # Task checkpoint
212
                self.checkpoint()
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
213
214
        # Stop progress group
215
        self.current.progress.group(Shows, 'episodes').stop()
0 ignored issues
show
Bug introduced by
The Instance of Shows 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...
216
217
        # Log details
218
        log_unsupported(log, 'Found %d unsupported show(s)\n%s', self.p_shows_unsupported)
219