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