|
1
|
1 |
|
from plugin.core.constants import GUID_SERVICES |
|
2
|
1 |
|
from plugin.sync.core.enums import SyncData, SyncMedia |
|
3
|
1 |
|
from plugin.sync.modes.core.base import log_unsupported, mark_unsupported |
|
4
|
1 |
|
from plugin.sync.modes.pull.base import Base |
|
5
|
|
|
|
|
6
|
1 |
|
from plex_database.models import MetadataItem |
|
7
|
1 |
|
import elapsed |
|
8
|
1 |
|
import logging |
|
9
|
|
|
|
|
10
|
1 |
|
log = logging.getLogger(__name__) |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
1 |
|
class Shows(Base): |
|
|
|
|
|
|
14
|
1 |
|
data = [ |
|
15
|
|
|
SyncData.Collection, |
|
16
|
|
|
SyncData.Playback, |
|
17
|
|
|
SyncData.Ratings, |
|
18
|
|
|
SyncData.Watched |
|
19
|
|
|
] |
|
20
|
|
|
|
|
21
|
1 |
|
@elapsed.clock |
|
22
|
|
|
def run(self): |
|
23
|
|
|
# Retrieve show sections |
|
24
|
|
|
p_sections, p_sections_map = self.sections('show') |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
# Fetch episodes with account settings |
|
27
|
|
|
p_shows, p_seasons, p_episodes = self.plex.library.episodes.mapped( |
|
|
|
|
|
|
28
|
|
|
p_sections, ([ |
|
29
|
|
|
MetadataItem.library_section |
|
30
|
|
|
], [], []), |
|
31
|
|
|
account=self.current.account.plex.key, |
|
|
|
|
|
|
32
|
|
|
parse_guid=True |
|
33
|
|
|
) |
|
34
|
|
|
|
|
35
|
|
|
# TODO process seasons |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
# Calculate total number of episodes |
|
38
|
|
|
pending = {} |
|
39
|
|
|
|
|
40
|
|
|
for data in self.get_data(SyncMedia.Episodes): |
|
|
|
|
|
|
41
|
|
|
t_episodes = [ |
|
42
|
|
|
(key, se, ep) |
|
43
|
|
|
for key, t_show in self.trakt[(SyncMedia.Episodes, data)].items() |
|
|
|
|
|
|
44
|
|
|
for se, t_season in t_show.seasons.items() |
|
45
|
|
|
for ep in t_season.episodes.iterkeys() |
|
46
|
|
|
] |
|
47
|
|
|
|
|
48
|
|
|
if data not in pending: |
|
49
|
|
|
pending[data] = {} |
|
50
|
|
|
|
|
51
|
|
|
for key in t_episodes: |
|
52
|
|
|
pending[data][key] = False |
|
53
|
|
|
|
|
54
|
|
|
# Task started |
|
55
|
|
|
unsupported_shows = {} |
|
56
|
|
|
|
|
57
|
|
|
# Process shows |
|
58
|
|
|
for sh_id, guid, p_show in p_shows: |
|
59
|
|
|
if not guid or guid.service not in GUID_SERVICES: |
|
60
|
|
|
mark_unsupported(unsupported_shows, sh_id, guid) |
|
61
|
|
|
continue |
|
62
|
|
|
|
|
63
|
|
|
key = (guid.service, guid.id) |
|
64
|
|
|
|
|
65
|
|
|
# Try retrieve `pk` for `key` |
|
66
|
|
|
pk = self.trakt.table('shows').get(key) |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
# Store in item map |
|
69
|
|
|
self.current.map.add(p_show.get('library_section'), sh_id, [key, pk]) |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
if pk is None: |
|
72
|
|
|
# No `pk` found |
|
73
|
|
|
continue |
|
74
|
|
|
|
|
75
|
|
|
# Execute data handlers |
|
76
|
|
|
for data in self.get_data(SyncMedia.Shows): |
|
|
|
|
|
|
77
|
|
|
t_show = self.trakt[(SyncMedia.Shows, data)].get(pk) |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
# Execute show handlers |
|
80
|
|
|
self.execute_handlers( |
|
|
|
|
|
|
81
|
|
|
SyncMedia.Shows, data, |
|
82
|
|
|
key=sh_id, |
|
83
|
|
|
|
|
84
|
|
|
p_item=p_show, |
|
85
|
|
|
t_item=t_show |
|
86
|
|
|
) |
|
87
|
|
|
|
|
88
|
|
|
# Process episodes |
|
89
|
|
|
for ids, guid, (season_num, episode_num), p_show, p_season, p_episode in p_episodes: |
|
|
|
|
|
|
90
|
|
|
if not guid or guid.service not in GUID_SERVICES: |
|
91
|
|
|
mark_unsupported(unsupported_shows, ids['show'], guid) |
|
92
|
|
|
continue |
|
93
|
|
|
|
|
94
|
|
|
key = (guid.service, guid.id) |
|
95
|
|
|
|
|
96
|
|
|
# Try retrieve `pk` for `key` |
|
97
|
|
|
pk = self.trakt.table('shows').get(key) |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
if pk is None: |
|
100
|
|
|
# No `pk` found |
|
101
|
|
|
continue |
|
102
|
|
|
|
|
103
|
|
|
if not ids.get('episode'): |
|
104
|
|
|
# Missing `episode` rating key |
|
105
|
|
|
continue |
|
106
|
|
|
|
|
107
|
|
|
for data in self.get_data(SyncMedia.Episodes): |
|
|
|
|
|
|
108
|
|
|
t_show = self.trakt[(SyncMedia.Episodes, data)].get(pk) |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
if t_show is None: |
|
111
|
|
|
# Unable to find matching show in trakt data |
|
112
|
|
|
continue |
|
113
|
|
|
|
|
114
|
|
|
t_season = t_show.seasons.get(season_num) |
|
115
|
|
|
|
|
116
|
|
|
if t_season is None: |
|
117
|
|
|
# Unable to find matching season in `t_show` |
|
118
|
|
|
continue |
|
119
|
|
|
|
|
120
|
|
|
t_episode = t_season.episodes.get(episode_num) |
|
121
|
|
|
|
|
122
|
|
|
if t_episode is None: |
|
123
|
|
|
# Unable to find matching episode in `t_season` |
|
124
|
|
|
continue |
|
125
|
|
|
|
|
126
|
|
|
self.execute_handlers( |
|
|
|
|
|
|
127
|
|
|
SyncMedia.Episodes, data, |
|
128
|
|
|
key=ids['episode'], |
|
129
|
|
|
|
|
130
|
|
|
p_item=p_episode, |
|
131
|
|
|
t_item=t_episode |
|
132
|
|
|
) |
|
133
|
|
|
|
|
134
|
|
|
# Increment one step |
|
135
|
|
|
self.step(pending, data, (pk, season_num, episode_num)) |
|
136
|
|
|
|
|
137
|
|
|
# Task checkpoint |
|
138
|
|
|
self.checkpoint() |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
# Log details |
|
141
|
|
|
log_unsupported(log, 'Found %d unsupported show(s)\n%s', unsupported_shows) |
|
142
|
|
|
log.debug('Pending: %r', pending) |
|
143
|
|
|
|