|
1
|
1 |
|
from plugin.sync.core.enums import SyncData, SyncMedia, SyncMode |
|
2
|
1 |
|
from plugin.sync.handlers.core import DataHandler, MediaHandler, bind |
|
3
|
|
|
|
|
4
|
1 |
|
from plex import Plex |
|
5
|
1 |
|
import logging |
|
6
|
1 |
|
import urllib |
|
7
|
|
|
|
|
8
|
1 |
|
log = logging.getLogger(__name__) |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
1 |
|
class Base(MediaHandler): |
|
|
|
|
|
|
12
|
1 |
|
@staticmethod |
|
13
|
|
|
def build_action(action, **kwargs): |
|
|
|
|
|
|
14
|
|
|
return kwargs |
|
15
|
|
|
|
|
16
|
1 |
|
def get_action(self, p_item, t_item): |
|
17
|
|
|
if not p_item and t_item: |
|
18
|
|
|
return 'added' |
|
19
|
|
|
|
|
20
|
|
|
if p_item and not t_item: |
|
21
|
|
|
return 'removed' |
|
22
|
|
|
|
|
23
|
|
|
return None |
|
24
|
|
|
|
|
25
|
1 |
|
def fast_pull(self, action=None, **kwargs): |
|
26
|
|
|
if action is None: |
|
27
|
|
|
# Determine performed action |
|
28
|
|
|
action = self.get_action(kwargs['p_item'], kwargs['t_item']) |
|
29
|
|
|
|
|
30
|
|
|
if not action: |
|
31
|
|
|
# No action required |
|
32
|
|
|
return |
|
33
|
|
|
|
|
34
|
|
|
# Execute action |
|
35
|
|
|
self.execute_action(action, **kwargs) |
|
36
|
|
|
|
|
37
|
1 |
|
def pull(self, key, p_item, t_item, *args, **kwargs): |
|
38
|
|
|
# Determine performed action |
|
39
|
|
|
action = self.get_action(p_item, t_item) |
|
40
|
|
|
|
|
41
|
|
|
if not action: |
|
42
|
|
|
# No action required |
|
43
|
|
|
return |
|
44
|
|
|
|
|
45
|
|
|
# Execute action |
|
46
|
|
|
self.execute_action( |
|
47
|
|
|
action, |
|
48
|
|
|
|
|
49
|
|
|
key=key, |
|
50
|
|
|
|
|
51
|
|
|
p_item=p_item, |
|
52
|
|
|
t_item=t_item, |
|
53
|
|
|
**kwargs |
|
54
|
|
|
) |
|
55
|
|
|
|
|
56
|
|
|
# |
|
57
|
|
|
# Action handlers |
|
58
|
|
|
# |
|
59
|
|
|
|
|
60
|
1 |
|
@bind('added') |
|
61
|
|
|
def on_added(self, p_sections_map, p_playlist, key, p_item, t_item): |
|
|
|
|
|
|
62
|
|
|
# Find item in plex matching `t_item` |
|
63
|
|
|
p_key = self.match(type(t_item), *key) |
|
64
|
|
|
|
|
65
|
|
|
if p_key is None: |
|
66
|
|
|
return |
|
67
|
|
|
|
|
68
|
|
|
log.debug('%s.on_added(p_key: %r)', self.media, p_key) |
|
69
|
|
|
|
|
70
|
|
|
# Build uri for plex item |
|
71
|
|
|
uri = self.build_uri(p_sections_map, *p_key) |
|
72
|
|
|
|
|
73
|
|
|
# Add item to playlist |
|
74
|
|
|
p_playlist.add(uri) |
|
75
|
|
|
|
|
76
|
|
|
# |
|
77
|
|
|
# Helpers |
|
78
|
|
|
# |
|
79
|
|
|
|
|
80
|
1 |
|
@staticmethod |
|
81
|
|
|
def build_uri(p_sections_map, p_section_key, p_item_key): |
|
82
|
|
|
# Retrieve section UUID |
|
83
|
|
|
p_section_uuid = p_sections_map.get(p_section_key) |
|
84
|
|
|
|
|
85
|
|
|
# Build URI |
|
86
|
|
|
return 'library://%s/item/%s' % ( |
|
87
|
|
|
p_section_uuid, |
|
88
|
|
|
urllib.quote_plus('/library/metadata/%s' % p_item_key) |
|
|
|
|
|
|
89
|
|
|
) |
|
90
|
|
|
|
|
91
|
1 |
|
def match(self, t_type, key, *extra): |
|
92
|
|
|
# Try retrieve `pk` for `key` |
|
93
|
|
|
pk = self.current.state.trakt.table(t_type).get(key) |
|
94
|
|
|
|
|
95
|
|
|
if pk is None: |
|
96
|
|
|
pk = key |
|
97
|
|
|
|
|
98
|
|
|
# Retrieve plex items that match `pk` |
|
99
|
|
|
p_keys = self.current.map.by_guid(pk) |
|
100
|
|
|
|
|
101
|
|
|
if not p_keys: |
|
102
|
|
|
return None |
|
103
|
|
|
|
|
104
|
|
|
# Convert to list (for indexing) |
|
105
|
|
|
p_keys = list(p_keys) |
|
106
|
|
|
|
|
107
|
|
|
# Use first match found in plex |
|
108
|
|
|
p_section_key, p_item_key = p_keys[0] |
|
109
|
|
|
|
|
110
|
|
|
# Find season/episode |
|
111
|
|
|
if len(extra) > 0: |
|
112
|
|
|
return self.match_show(p_section_key, p_item_key, *extra) |
|
113
|
|
|
|
|
114
|
|
|
return p_section_key, p_item_key |
|
115
|
|
|
|
|
116
|
1 |
|
def match_show(self, p_section_key, p_show_key, season, episode=None): |
|
|
|
|
|
|
117
|
|
|
# Fetch seasons for show |
|
118
|
|
|
p_seasons = dict([ |
|
119
|
|
|
(p_season.index, p_season.rating_key) |
|
120
|
|
|
for p_season in Plex['library/metadata'].children(p_show_key) |
|
121
|
|
|
]) |
|
122
|
|
|
|
|
123
|
|
|
# Find matching season |
|
124
|
|
|
p_season_key = p_seasons.get(season) |
|
125
|
|
|
|
|
126
|
|
|
if p_season_key is None: |
|
127
|
|
|
# Unable to find season |
|
128
|
|
|
return None |
|
129
|
|
|
|
|
130
|
|
|
if episode is None: |
|
131
|
|
|
# Return matching season |
|
132
|
|
|
return p_section_key, p_season_key |
|
133
|
|
|
|
|
134
|
|
|
# Fetch episodes for season |
|
135
|
|
|
p_episodes = dict([ |
|
136
|
|
|
(p_episode.index, p_episode.rating_key) |
|
137
|
|
|
for p_episode in Plex['library/metadata'].children(p_season_key) |
|
138
|
|
|
]) |
|
139
|
|
|
|
|
140
|
|
|
# Find matching episode |
|
141
|
|
|
p_episode_key = p_episodes.get(episode) |
|
142
|
|
|
|
|
143
|
|
|
if p_episode_key is None: |
|
144
|
|
|
# Unable to find episode |
|
145
|
|
|
return None |
|
146
|
|
|
|
|
147
|
|
|
# Return matching episode |
|
148
|
|
|
return p_section_key, p_episode_key |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
1 |
|
class Lists(MediaHandler): |
|
|
|
|
|
|
152
|
1 |
|
media = SyncMedia.Lists |
|
153
|
|
|
|
|
154
|
1 |
|
@staticmethod |
|
155
|
|
|
def build_action(action, **kwargs): |
|
|
|
|
|
|
156
|
|
|
return kwargs |
|
157
|
|
|
|
|
158
|
1 |
|
def fast_pull(self, action, **kwargs): |
|
159
|
|
|
# Execute action |
|
160
|
|
|
self.execute_action(action, **kwargs) |
|
161
|
|
|
|
|
162
|
1 |
|
@bind('added') |
|
163
|
|
|
def on_added(self, key, **kwargs): |
|
164
|
|
|
log.debug('%s.on_added(key: %r, kwargs: %r)', self.media, key, kwargs) |
|
165
|
|
|
|
|
166
|
1 |
|
@bind('changed') |
|
167
|
|
|
def on_changed(self, key, **kwargs): |
|
168
|
|
|
log.debug('%s.on_changed(key: %r, kwargs: %r)', self.media, key, kwargs) |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
1 |
|
class Movies(Base): |
|
|
|
|
|
|
172
|
1 |
|
media = SyncMedia.Movies |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
1 |
|
class Shows(Base): |
|
|
|
|
|
|
176
|
1 |
|
media = SyncMedia.Shows |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
1 |
|
class Seasons(Base): |
|
|
|
|
|
|
180
|
1 |
|
media = SyncMedia.Seasons |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
1 |
|
class Episodes(Base): |
|
|
|
|
|
|
184
|
1 |
|
media = SyncMedia.Episodes |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
1 |
|
class Pull(DataHandler): |
|
188
|
1 |
|
data = [ |
|
189
|
|
|
SyncData.Liked, |
|
190
|
|
|
SyncData.Personal, |
|
191
|
|
|
SyncData.Watchlist |
|
192
|
|
|
] |
|
193
|
|
|
|
|
194
|
1 |
|
mode = [ |
|
195
|
|
|
SyncMode.FastPull, |
|
196
|
|
|
SyncMode.Pull |
|
197
|
|
|
] |
|
198
|
|
|
|
|
199
|
1 |
|
children = [ |
|
200
|
|
|
Lists, |
|
201
|
|
|
|
|
202
|
|
|
Movies, |
|
203
|
|
|
Shows, |
|
204
|
|
|
Seasons, |
|
205
|
|
|
Episodes |
|
206
|
|
|
] |
|
207
|
|
|
|
Methods which raise
NotImplementedErrorshould be overridden in concrete child classes.