1
|
1 |
|
from plugin.core.constants import GUID_SERVICES |
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 |
|
data = [ |
15
|
|
|
SyncData.Collection, |
16
|
|
|
SyncData.Playback, |
17
|
|
|
SyncData.Ratings, |
18
|
|
|
SyncData.Watched |
19
|
|
|
] |
20
|
1 |
|
mode = SyncMode.FastPull |
21
|
|
|
|
22
|
1 |
|
def __init__(self, task): |
23
|
|
|
super(Shows, self).__init__(task) |
24
|
|
|
|
25
|
|
|
# Sections |
26
|
|
|
self.p_sections = None |
27
|
|
|
self.p_sections_map = None |
28
|
|
|
|
29
|
|
|
# Shows |
30
|
|
|
self.p_shows = None |
31
|
|
|
self.p_shows_count = None |
32
|
|
|
self.p_shows_unsupported = None |
33
|
|
|
|
34
|
|
|
# Seasons |
35
|
|
|
self.p_seasons = None |
36
|
|
|
|
37
|
|
|
# Episodes |
38
|
|
|
self.p_episodes = None |
39
|
|
|
self.p_episodes_count = None |
40
|
|
|
|
41
|
1 |
|
@elapsed.clock |
42
|
|
|
def construct(self): |
43
|
|
|
# Retrieve show sections |
44
|
|
|
self.p_sections, self.p_sections_map = self.sections('show') |
|
|
|
|
45
|
|
|
|
46
|
|
|
# Determine number of shows that will be processed |
47
|
|
|
self.p_shows_count = self.plex.library.shows.count( |
|
|
|
|
48
|
|
|
self.p_sections |
49
|
|
|
) |
50
|
|
|
|
51
|
|
|
# Determine number of shows that will be processed |
52
|
|
|
self.p_episodes_count = self.plex.library.episodes.count_items( |
|
|
|
|
53
|
|
|
self.p_sections |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
# Increment progress steps total |
57
|
|
|
self.current.progress.group(Shows, 'shows').add(self.p_shows_count) |
|
|
|
|
58
|
|
|
self.current.progress.group(Shows, 'episodes').add(self.p_episodes_count) |
|
|
|
|
59
|
|
|
|
60
|
1 |
|
@elapsed.clock |
61
|
|
|
def start(self): |
62
|
|
|
# Fetch episodes with account settings |
63
|
|
|
self.p_shows, self.p_seasons, self.p_episodes = self.plex.library.episodes.mapped( |
|
|
|
|
64
|
|
|
self.p_sections, ([ |
65
|
|
|
MetadataItem.library_section |
66
|
|
|
], [], []), |
67
|
|
|
account=self.current.account.plex.key, |
|
|
|
|
68
|
|
|
parse_guid=True |
69
|
|
|
) |
70
|
|
|
|
71
|
|
|
# Reset state |
72
|
|
|
self.p_shows_unsupported = {} |
73
|
|
|
|
74
|
1 |
|
@elapsed.clock |
75
|
|
|
def run(self): |
76
|
|
|
# TODO process seasons |
|
|
|
|
77
|
|
|
|
78
|
|
|
with elapsed.clock(Shows, 'run:shows'): |
79
|
|
|
# Process shows |
80
|
|
|
for sh_id, guid, p_show in self.p_shows: |
81
|
|
|
# Increment one step |
82
|
|
|
self.current.progress.group(Shows, 'shows').step() |
|
|
|
|
83
|
|
|
|
84
|
|
|
# Ensure `guid` is available |
85
|
|
|
if not guid or guid.service not in GUID_SERVICES: |
86
|
|
|
mark_unsupported(self.p_shows_unsupported, sh_id, guid, p_show) |
87
|
|
|
continue |
88
|
|
|
|
89
|
|
|
key = (guid.service, guid.id) |
90
|
|
|
|
91
|
|
|
# Try retrieve `pk` for `key` |
92
|
|
|
pk = self.trakt.table('shows').get(key) |
|
|
|
|
93
|
|
|
|
94
|
|
|
# Store in item map |
95
|
|
|
self.current.map.add(p_show.get('library_section'), sh_id, [key, pk]) |
|
|
|
|
96
|
|
|
|
97
|
|
|
if pk is None: |
98
|
|
|
# No `pk` found |
99
|
|
|
continue |
100
|
|
|
|
101
|
|
|
# Iterate over changed data |
102
|
|
|
for key, result in self.trakt.changes: |
|
|
|
|
103
|
|
|
media, data = key[0:2] |
104
|
|
|
|
105
|
|
|
if media != SyncMedia.Shows: |
106
|
|
|
# Ignore changes that aren't for episodes |
107
|
|
|
continue |
108
|
|
|
|
109
|
|
|
if data == SyncData.Watchlist: |
110
|
|
|
# Ignore watchlist data |
111
|
|
|
continue |
112
|
|
|
|
113
|
|
|
if not self.is_data_enabled(data): |
|
|
|
|
114
|
|
|
# Data type has been disabled |
115
|
|
|
continue |
116
|
|
|
|
117
|
|
|
data_name = Cache.Data.get(data) |
118
|
|
|
|
119
|
|
|
if data_name not in result.changes: |
120
|
|
|
# No changes for collection |
121
|
|
|
continue |
122
|
|
|
|
123
|
|
|
for action, shows in result.changes[data_name].items(): |
124
|
|
|
t_show = shows.get(pk) |
125
|
|
|
|
126
|
|
|
if t_show is None: |
127
|
|
|
# Unable to find matching show in trakt data |
128
|
|
|
continue |
129
|
|
|
|
130
|
|
|
# Execute show handlers |
131
|
|
|
self.execute_handlers( |
|
|
|
|
132
|
|
|
SyncMedia.Shows, data, |
133
|
|
|
action=action, |
134
|
|
|
|
135
|
|
|
key=sh_id, |
136
|
|
|
|
137
|
|
|
p_item=p_show, |
138
|
|
|
t_item=t_show |
139
|
|
|
) |
140
|
|
|
|
141
|
|
|
# Stop progress group |
142
|
|
|
self.current.progress.group(Shows, 'shows').stop() |
|
|
|
|
143
|
|
|
|
144
|
|
|
with elapsed.clock(Shows, 'run:episodes'): |
145
|
|
|
# Process episodes |
146
|
|
|
for ids, guid, (season_num, episode_num), p_show, p_season, p_episode in self.p_episodes: |
|
|
|
|
147
|
|
|
# Increment one step |
148
|
|
|
self.current.progress.group(Shows, 'episodes').step() |
|
|
|
|
149
|
|
|
|
150
|
|
|
# Ensure `guid` is available |
151
|
|
|
if not guid or guid.service not in GUID_SERVICES: |
152
|
|
|
mark_unsupported(self.p_shows_unsupported, ids['show'], guid, p_show) |
153
|
|
|
continue |
154
|
|
|
|
155
|
|
|
key = (guid.service, guid.id) |
156
|
|
|
|
157
|
|
|
# Try retrieve `pk` for `key` |
158
|
|
|
pk = self.trakt.table('shows').get(key) |
|
|
|
|
159
|
|
|
|
160
|
|
|
if pk is None: |
161
|
|
|
# No `pk` found |
162
|
|
|
continue |
163
|
|
|
|
164
|
|
|
if not ids.get('episode'): |
165
|
|
|
# Missing `episode` rating key |
166
|
|
|
continue |
167
|
|
|
|
168
|
|
|
for key, result in self.trakt.changes: |
|
|
|
|
169
|
|
|
media, data = key[0:2] |
170
|
|
|
|
171
|
|
|
if media != SyncMedia.Episodes: |
172
|
|
|
# Ignore changes that aren't for episodes |
173
|
|
|
continue |
174
|
|
|
|
175
|
|
|
if not self.is_data_enabled(data): |
|
|
|
|
176
|
|
|
# Data type has been disabled |
177
|
|
|
continue |
178
|
|
|
|
179
|
|
|
data_name = Cache.Data.get(data) |
180
|
|
|
|
181
|
|
|
if data_name not in result.changes: |
182
|
|
|
# No changes for collection |
183
|
|
|
continue |
184
|
|
|
|
185
|
|
|
for action, shows in result.changes[data_name].items(): |
186
|
|
|
t_show = shows.get(pk) |
187
|
|
|
|
188
|
|
|
if t_show is None: |
189
|
|
|
# Unable to find matching show in trakt data |
190
|
|
|
continue |
191
|
|
|
|
192
|
|
|
t_season = t_show.get('seasons', {}).get(season_num) |
193
|
|
|
|
194
|
|
|
if t_season is None: |
195
|
|
|
# Unable to find matching season in `t_show` |
196
|
|
|
continue |
197
|
|
|
|
198
|
|
|
t_episode = t_season.get('episodes', {}).get(episode_num) |
199
|
|
|
|
200
|
|
|
if t_episode is None: |
201
|
|
|
# Unable to find matching episode in `t_season` |
202
|
|
|
continue |
203
|
|
|
|
204
|
|
|
self.execute_handlers( |
|
|
|
|
205
|
|
|
SyncMedia.Episodes, data, |
206
|
|
|
action=action, |
207
|
|
|
key=ids['episode'], |
208
|
|
|
|
209
|
|
|
p_item=p_episode, |
210
|
|
|
t_item=t_episode |
211
|
|
|
) |
212
|
|
|
|
213
|
|
|
# Task checkpoint |
214
|
|
|
self.checkpoint() |
|
|
|
|
215
|
|
|
|
216
|
|
|
# Stop progress group |
217
|
|
|
self.current.progress.group(Shows, 'episodes').stop() |
|
|
|
|
218
|
|
|
|
219
|
|
|
# Log details |
220
|
|
|
log_unsupported(log, 'Found %d unsupported show(s)\n%s', self.p_shows_unsupported) |
221
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.