|
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') |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
# Determine number of movies that will be processed |
|
42
|
|
|
self.p_count = self.plex.library.movies.count( |
|
|
|
|
|
|
43
|
|
|
self.p_sections, |
|
44
|
|
|
account=self.current.account.plex.key |
|
|
|
|
|
|
45
|
|
|
) |
|
46
|
|
|
|
|
47
|
|
|
# Increment progress steps total |
|
48
|
|
|
self.current.progress.group(Movies, 'matched:movies').add(self.p_count) |
|
|
|
|
|
|
49
|
|
|
self.current.progress.group(Movies, 'missing:movies') |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
1 |
|
@elapsed.clock |
|
52
|
|
|
def start(self): |
|
53
|
|
|
# Fetch movies with account settings |
|
54
|
|
|
self.p_movies = self.plex.library.movies.mapped( |
|
|
|
|
|
|
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, |
|
|
|
|
|
|
66
|
|
|
parse_guid=True |
|
67
|
|
|
) |
|
68
|
|
|
|
|
69
|
|
|
# Reset state |
|
70
|
|
|
self.p_pending = self.trakt.table.movie_keys.copy() |
|
|
|
|
|
|
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): |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
# Process `p_guid` (map + validate) |
|
88
|
|
|
supported, matched, p_guid = self.process_guid(p_guid) |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
if not supported: |
|
91
|
|
|
mark_unsupported(self.p_unsupported, mo_id, p_guid) |
|
92
|
|
|
continue |
|
93
|
|
|
|
|
94
|
|
|
if not matched: |
|
95
|
|
|
log.info('Unable to find identifier for: %s/%s (rating_key: %r)', p_guid.service, p_guid.id, mo_id) |
|
96
|
|
|
continue |
|
97
|
|
|
|
|
98
|
|
|
key = (p_guid.service, p_guid.id) |
|
99
|
|
|
|
|
100
|
|
|
# Try retrieve `pk` for `key` |
|
101
|
|
|
pk = self.trakt.table('movies').get(key) |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
for data in self.get_data(SyncMedia.Movies): |
|
|
|
|
|
|
104
|
|
|
t_movie = self.trakt[(SyncMedia.Movies, data)].get(pk) |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
self.execute_handlers( |
|
|
|
|
|
|
107
|
|
|
SyncMedia.Movies, data, |
|
108
|
|
|
|
|
109
|
|
|
key=mo_id, |
|
110
|
|
|
|
|
111
|
|
|
guid=p_guid, |
|
112
|
|
|
p_item=p_item, |
|
113
|
|
|
|
|
114
|
|
|
t_item=t_movie |
|
115
|
|
|
) |
|
116
|
|
|
|
|
117
|
|
|
# Remove movie from `pending` set |
|
118
|
|
|
if pk and pk in self.p_pending: |
|
119
|
|
|
self.p_pending.remove(pk) |
|
120
|
|
|
|
|
121
|
|
|
# Task checkpoint |
|
122
|
|
|
self.checkpoint() |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
# Stop progress group |
|
125
|
|
|
self.current.progress.group(Movies, 'matched:movies').stop() |
|
|
|
|
|
|
126
|
1 |
|
|
|
127
|
|
|
# Report unsupported movies (unsupported guid) |
|
128
|
|
|
log_unsupported(log, 'Found %d unsupported movie(s)', self.p_unsupported) |
|
129
|
|
|
|
|
130
|
|
|
def process_missing_movies(self): |
|
131
|
|
|
"""Trigger actions for movies that are in trakt, but was unable to be found in plex""" |
|
132
|
|
|
|
|
133
|
|
|
if self.current.kwargs.get('section'): |
|
|
|
|
|
|
134
|
|
|
# Collection cleaning disabled for individual syncs |
|
135
|
|
|
return |
|
136
|
|
|
|
|
137
|
|
|
# Increment progress steps |
|
138
|
|
|
self.current.progress.group(Movies, 'missing:movies').add(len(self.p_pending)) |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
# Iterate over movies |
|
141
|
|
|
for pk in list(self.p_pending): |
|
142
|
|
|
# Increment one step |
|
143
|
|
|
self.current.progress.group(Movies, 'missing:movies').step() |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
# Iterate over data handlers |
|
146
|
|
|
triggered = False |
|
147
|
|
|
|
|
148
|
|
|
for data in self.get_data(SyncMedia.Movies): |
|
|
|
|
|
|
149
|
|
|
if data not in [SyncData.Collection]: |
|
150
|
|
|
continue |
|
151
|
|
|
|
|
152
|
|
|
# Retrieve movie |
|
153
|
|
|
t_movie = self.trakt[(SyncMedia.Movies, data)].get(pk) |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
if not t_movie: |
|
156
|
|
|
continue |
|
157
|
|
|
|
|
158
|
|
|
log.debug('Found movie missing from plex: %r [data: %r]', pk, SyncData.title(data)) |
|
159
|
|
|
|
|
160
|
|
|
# Trigger handler |
|
161
|
|
|
self.execute_handlers( |
|
|
|
|
|
|
162
|
|
|
SyncMedia.Movies, data, |
|
163
|
|
|
|
|
164
|
|
|
key=None, |
|
165
|
|
|
|
|
166
|
|
|
guid=Guid.construct(*pk), |
|
167
|
|
|
p_item=None, |
|
168
|
|
|
|
|
169
|
|
|
t_item=t_movie |
|
170
|
|
|
) |
|
171
|
|
|
|
|
172
|
|
|
# Mark triggered |
|
173
|
|
|
triggered = True |
|
174
|
|
|
|
|
175
|
|
|
# Check if action was triggered |
|
176
|
|
|
if not triggered: |
|
177
|
|
|
continue |
|
178
|
|
|
|
|
179
|
|
|
# Remove movie from `pending` set |
|
180
|
|
|
self.p_pending.remove(pk) |
|
181
|
|
|
|
|
182
|
|
|
# Stop progress group |
|
183
|
|
|
self.current.progress.group(Movies, 'missing:movies').stop() |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
# Report pending movies (no actions triggered) |
|
186
|
|
|
self.log_pending( |
|
|
|
|
|
|
187
|
|
|
log, 'Unable to find %d movie(s) in Plex, list has been saved to: %s', |
|
188
|
|
|
self.current.account, 'movies', self.p_pending |
|
|
|
|
|
|
189
|
|
|
) |
|
190
|
|
|
|