1
|
1 |
|
from plugin.sync.core.constants import GUID_AGENTS |
2
|
1 |
|
from plugin.sync.core.enums import SyncMode, SyncData, SyncMedia |
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 Movies(Mode): |
14
|
1 |
|
mode = SyncMode.FastPull |
15
|
|
|
|
16
|
1 |
|
def __init__(self, task): |
17
|
|
|
super(Movies, self).__init__(task) |
18
|
|
|
|
19
|
|
|
# Sections |
20
|
|
|
self.p_sections = None |
21
|
|
|
self.p_sections_map = None |
22
|
|
|
|
23
|
|
|
# Items |
24
|
|
|
self.p_count = None |
25
|
|
|
self.p_movies = None |
26
|
|
|
|
27
|
|
|
self.p_unsupported = None |
28
|
|
|
|
29
|
1 |
|
@elapsed.clock |
30
|
|
|
def construct(self): |
31
|
|
|
# Retrieve movie sections |
32
|
|
|
self.p_sections, self.p_sections_map = self.sections('movie') |
|
|
|
|
33
|
|
|
|
34
|
|
|
# Determine number of movies that will be processed |
35
|
|
|
self.p_count = self.plex.library.movies.count( |
|
|
|
|
36
|
|
|
self.p_sections, |
37
|
|
|
account=self.current.account.plex.key |
|
|
|
|
38
|
|
|
) |
39
|
|
|
|
40
|
|
|
# Increment progress steps total |
41
|
|
|
self.current.progress.group(Movies).add(self.p_count) |
|
|
|
|
42
|
|
|
|
43
|
1 |
|
@elapsed.clock |
44
|
|
|
def start(self): |
45
|
|
|
# Fetch movies with account settings |
46
|
|
|
self.p_movies = self.plex.library.movies.mapped( |
|
|
|
|
47
|
|
|
self.p_sections, [ |
48
|
|
|
MetadataItem.library_section |
49
|
|
|
], |
50
|
|
|
account=self.current.account.plex.key, |
|
|
|
|
51
|
|
|
parse_guid=True |
52
|
|
|
) |
53
|
|
|
|
54
|
|
|
# Reset state |
55
|
|
|
self.p_unsupported = {} |
56
|
|
|
|
57
|
1 |
|
@elapsed.clock |
58
|
|
|
def run(self): |
59
|
|
|
# Process movies |
60
|
|
|
for mo_id, guid, p_item in self.p_movies: |
61
|
|
|
# Increment one step |
62
|
|
|
self.current.progress.group(Movies).step() |
|
|
|
|
63
|
|
|
|
64
|
|
|
# Ensure `guid` is available |
65
|
|
|
if not guid or guid.agent not in GUID_AGENTS: |
66
|
|
|
mark_unsupported(self.p_unsupported, mo_id, guid, p_item) |
67
|
|
|
continue |
68
|
|
|
|
69
|
|
|
key = (guid.agent, guid.sid) |
70
|
|
|
|
71
|
|
|
log.debug('Processing movie: %s', key) |
72
|
|
|
|
73
|
|
|
# Try retrieve `pk` for `key` |
74
|
|
|
pk = self.trakt.table.get(key) |
|
|
|
|
75
|
|
|
|
76
|
|
|
# Store in item map |
77
|
|
|
self.current.map.add(p_item.get('library_section'), mo_id, [key, pk]) |
|
|
|
|
78
|
|
|
|
79
|
|
|
if pk is None: |
80
|
|
|
# No `pk` found |
81
|
|
|
continue |
82
|
|
|
|
83
|
|
|
# Iterate over changed data |
84
|
|
|
for key, result in self.trakt.changes: |
|
|
|
|
85
|
|
|
media, data = key[0:2] |
86
|
|
|
|
87
|
|
|
if media != SyncMedia.Movies: |
88
|
|
|
# Ignore changes that aren't for movies |
89
|
|
|
continue |
90
|
|
|
|
91
|
|
|
if data == SyncData.Watchlist: |
92
|
|
|
# Ignore watchlist data |
93
|
|
|
continue |
94
|
|
|
|
95
|
|
|
if not self.is_data_enabled(data): |
|
|
|
|
96
|
|
|
# Data type has been disabled |
97
|
|
|
continue |
98
|
|
|
|
99
|
|
|
data_name = Cache.Data.get(data) |
100
|
|
|
|
101
|
|
|
if data_name not in result.changes: |
102
|
|
|
# No changes for collection |
103
|
|
|
continue |
104
|
|
|
|
105
|
|
|
for action, items in result.changes[data_name].items(): |
106
|
|
|
t_item = items.get(pk) |
107
|
|
|
|
108
|
|
|
if t_item is None: |
109
|
|
|
# No item found in changes |
110
|
|
|
continue |
111
|
|
|
|
112
|
|
|
self.execute_handlers( |
|
|
|
|
113
|
|
|
SyncMedia.Movies, data, |
114
|
|
|
action=action, |
115
|
|
|
key=mo_id, |
116
|
|
|
|
117
|
|
|
p_item=p_item, |
118
|
|
|
t_item=t_item |
119
|
|
|
) |
120
|
|
|
|
121
|
|
|
# Task checkpoint |
122
|
|
|
self.checkpoint() |
|
|
|
|
123
|
|
|
|
124
|
|
|
# Stop progress group |
125
|
|
|
self.current.progress.group(Movies).stop() |
|
|
|
|
126
|
|
|
|
127
|
|
|
# Log details |
128
|
|
|
log_unsupported(log, 'Found %d unsupported movie(s)\n%s', self.p_unsupported) |
129
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.