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 |
|
@elapsed.clock |
17
|
|
|
def run(self): |
18
|
|
|
# Retrieve movie sections |
19
|
|
|
p_sections, p_sections_map = self.sections('movie') |
|
|
|
|
20
|
|
|
|
21
|
|
|
# Fetch movies with account settings |
22
|
|
|
p_items = self.plex.library.movies.mapped( |
|
|
|
|
23
|
|
|
p_sections, [ |
24
|
|
|
MetadataItem.library_section |
25
|
|
|
], |
26
|
|
|
account=self.current.account.plex.key, |
|
|
|
|
27
|
|
|
parse_guid=True |
28
|
|
|
) |
29
|
|
|
|
30
|
|
|
# Calculate total number of steps |
31
|
|
|
total = 0 |
32
|
|
|
|
33
|
|
|
for key, result in self.trakt.changes: |
|
|
|
|
34
|
|
|
media, data = key[0:2] |
35
|
|
|
|
36
|
|
|
if media != SyncMedia.Movies: |
37
|
|
|
# Ignore changes that aren't for episodes |
38
|
|
|
continue |
39
|
|
|
|
40
|
|
|
data_name = Cache.Data.get(data) |
41
|
|
|
|
42
|
|
|
for count in result.metrics.movies.get(data_name, {}).itervalues(): |
43
|
|
|
total += count |
44
|
|
|
|
45
|
|
|
# Task started |
46
|
|
|
unsupported_movies = {} |
47
|
|
|
|
48
|
|
|
self.current.progress.start(total) |
|
|
|
|
49
|
|
|
|
50
|
|
|
# Process movies |
51
|
|
|
for rating_key, p_guid, p_item in p_items: |
52
|
|
|
if not p_guid or p_guid.agent not in GUID_AGENTS: |
53
|
|
|
mark_unsupported(unsupported_movies, rating_key, p_guid, p_item) |
54
|
|
|
continue |
55
|
|
|
|
56
|
|
|
key = (p_guid.agent, p_guid.sid) |
57
|
|
|
|
58
|
|
|
log.debug('Processing movie: %s', key) |
59
|
|
|
|
60
|
|
|
# Try retrieve `pk` for `key` |
61
|
|
|
pk = self.trakt.table.get(key) |
|
|
|
|
62
|
|
|
|
63
|
|
|
# Store in item map |
64
|
|
|
self.current.map.add(p_item.get('library_section'), rating_key, [key, pk]) |
|
|
|
|
65
|
|
|
|
66
|
|
|
if pk is None: |
67
|
|
|
# No `pk` found |
68
|
|
|
continue |
69
|
|
|
|
70
|
|
|
# Iterate over changed data |
71
|
|
|
for key, result in self.trakt.changes: |
|
|
|
|
72
|
|
|
media, data = key[0:2] |
73
|
|
|
|
74
|
|
|
if media != SyncMedia.Movies: |
75
|
|
|
# Ignore changes that aren't for movies |
76
|
|
|
continue |
77
|
|
|
|
78
|
|
|
if data == SyncData.Watchlist: |
79
|
|
|
# Ignore watchlist data |
80
|
|
|
continue |
81
|
|
|
|
82
|
|
|
if not self.is_data_enabled(data): |
|
|
|
|
83
|
|
|
# Data type has been disabled |
84
|
|
|
continue |
85
|
|
|
|
86
|
|
|
data_name = Cache.Data.get(data) |
87
|
|
|
|
88
|
|
|
if data_name not in result.changes: |
89
|
|
|
# No changes for collection |
90
|
|
|
continue |
91
|
|
|
|
92
|
|
|
for action, items in result.changes[data_name].items(): |
93
|
|
|
t_item = items.get(pk) |
94
|
|
|
|
95
|
|
|
if t_item is None: |
96
|
|
|
# No item found in changes |
97
|
|
|
continue |
98
|
|
|
|
99
|
|
|
self.execute_handlers( |
|
|
|
|
100
|
|
|
SyncMedia.Movies, data, |
101
|
|
|
action=action, |
102
|
|
|
key=rating_key, |
103
|
|
|
|
104
|
|
|
p_item=p_item, |
105
|
|
|
t_item=t_item |
106
|
|
|
) |
107
|
|
|
|
108
|
|
|
# Increment one step |
109
|
|
|
self.current.progress.step() |
|
|
|
|
110
|
|
|
|
111
|
|
|
# Task checkpoint |
112
|
|
|
self.checkpoint() |
|
|
|
|
113
|
|
|
|
114
|
|
|
# Log details |
115
|
|
|
log_unsupported(log, 'Found %d unsupported movie(s)\n%s', unsupported_movies) |
116
|
|
|
|
117
|
|
|
# Task stopped |
118
|
|
|
self.current.progress.stop() |
|
|
|
|
119
|
|
|
|