Passed
Push — develop ( af752c...572338 )
by Dean
02:33
created

Shows   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Test Coverage

Coverage 3.8%
Metric Value
dl 0
loc 179
ccs 3
cts 79
cp 0.038
rs 9.8
wmc 31

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 176 31
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):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
14 1
    mode = SyncMode.FastPull
15
16 1
    @elapsed.clock
17
    def run(self):
18
        # Retrieve show sections
19
        p_sections, 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...
Unused Code introduced by
The variable p_sections_map seems to be unused.
Loading history...
20
21
        with elapsed.clock(Shows, 'run:fetch'):
22
            # Fetch episodes with account settings
23
            p_shows, p_seasons, 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...
Unused Code introduced by
The variable p_seasons seems to be unused.
Loading history...
24
                p_sections, ([
25
                    MetadataItem.library_section
26
                ], [], []),
27
                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...
28
                parse_guid=True
29
            )
30
31
        # TODO process seasons
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
32
33
        # Calculate total number of episode changes
34
        total = 0
35
36
        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...
37
            media, data = key[0:2]
38
39
            if media != SyncMedia.Episodes:
40
                # Ignore changes that aren't for episodes
41
                continue
42
43
            data_name = Cache.Data.get(data)
44
45
            for count in result.metrics.episodes.get(data_name, {}).itervalues():
46
                total += count
47
48
        # Task started
49
        unsupported_shows = {}
50
51
        self.current.progress.start(total)
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
53
        with elapsed.clock(Shows, 'run:shows'):
54
            # Process shows
55
            for sh_id, p_guid, p_show in p_shows:
56
                if not p_guid or p_guid.agent not in GUID_AGENTS:
57
                    mark_unsupported(unsupported_shows, sh_id, p_guid, p_show)
58
                    continue
59
60
                key = (p_guid.agent, p_guid.sid)
61
62
                log.debug('Processing show: %s', key)
63
64
                # Try retrieve `pk` for `key`
65
                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...
66
67
                # Store in item map
68
                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...
69
70
                if pk is None:
71
                    # No `pk` found
72
                    continue
73
74
                # Iterate over changed data
75
                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...
76
                    media, data = key[0:2]
77
78
                    if media != SyncMedia.Shows:
79
                        # Ignore changes that aren't for episodes
80
                        continue
81
82
                    if data == SyncData.Watchlist:
83
                        # Ignore watchlist data
84
                        continue
85
86
                    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...
87
                        # Data type has been disabled
88
                        continue
89
90
                    data_name = Cache.Data.get(data)
91
92
                    if data_name not in result.changes:
93
                        # No changes for collection
94
                        continue
95
96
                    for action, shows in result.changes[data_name].items():
97
                        t_show = shows.get(pk)
98
99
                        if t_show is None:
100
                            # Unable to find matching show in trakt data
101
                            continue
102
103
                        # Execute show handlers
104
                        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...
105
                            SyncMedia.Shows, data,
106
                            action=action,
107
108
                            key=sh_id,
109
110
                            p_item=p_show,
111
                            t_item=t_show
112
                        )
113
114
        with elapsed.clock(Shows, 'run:episodes'):
115
            # Process episodes
116
            for ids, p_guid, (season_num, episode_num), p_show, p_season, p_episode in p_episodes:
0 ignored issues
show
Unused Code introduced by
The variable p_season seems to be unused.
Loading history...
117
                if not p_guid or p_guid.agent not in GUID_AGENTS:
118
                    mark_unsupported(unsupported_shows, ids['show'], p_guid, p_show)
119
                    continue
120
121
                key = (p_guid.agent, p_guid.sid)
122
123
                log.debug('Processing episode: %s - S%02dE%02d', key, season_num, episode_num)
124
125
                # Try retrieve `pk` for `key`
126
                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...
127
128
                if pk is None:
129
                    # No `pk` found
130
                    continue
131
132
                if not ids.get('episode'):
133
                    # Missing `episode` rating key
134
                    continue
135
136
                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...
137
                    media, data = key[0:2]
138
139
                    if media != SyncMedia.Episodes:
140
                        # Ignore changes that aren't for episodes
141
                        continue
142
143
                    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...
144
                        # Data type has been disabled
145
                        continue
146
147
                    data_name = Cache.Data.get(data)
148
149
                    if data_name not in result.changes:
150
                        # No changes for collection
151
                        continue
152
153
                    for action, shows in result.changes[data_name].items():
154
                        t_show = shows.get(pk)
155
156
                        if t_show is None:
157
                            # Unable to find matching show in trakt data
158
                            continue
159
160
                        t_season = t_show.get('seasons', {}).get(season_num)
161
162
                        if t_season is None:
163
                            # Unable to find matching season in `t_show`
164
                            continue
165
166
                        t_episode = t_season.get('episodes', {}).get(episode_num)
167
168
                        if t_episode is None:
169
                            # Unable to find matching episode in `t_season`
170
                            continue
171
172
                        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...
173
                            SyncMedia.Episodes, data,
174
                            action=action,
175
                            key=ids['episode'],
176
177
                            p_item=p_episode,
178
                            t_item=t_episode
179
                        )
180
181
                        # Increment one step
182
                        self.current.progress.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...
183
184
                # Task checkpoint
185
                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...
186
187
        # Log details
188
        log_unsupported(log, 'Found %d unsupported show(s)\n%s', unsupported_shows)
189
190
        # Task stopped
191
        self.current.progress.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...
192