|
1
|
|
|
from plugin.core.cache import CacheManager |
|
2
|
|
|
from plugin.core.enums import MatcherMode |
|
3
|
|
|
from plugin.modules.core.base import Module |
|
4
|
|
|
from plugin.preferences import Preferences |
|
5
|
|
|
|
|
6
|
|
|
from plex import Plex |
|
7
|
|
|
from plex.objects.library.metadata.episode import Episode |
|
8
|
|
|
import plex_database.matcher |
|
9
|
|
|
import plex_metadata.matcher |
|
10
|
|
|
import logging |
|
11
|
|
|
|
|
12
|
|
|
log = logging.getLogger(__name__) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class Matcher(Module): |
|
16
|
|
|
__key__ = 'matcher' |
|
17
|
|
|
|
|
18
|
|
|
def __init__(self): |
|
19
|
|
|
self._cache = None |
|
20
|
|
|
|
|
21
|
|
|
self._database_matcher = None |
|
22
|
|
|
self._metadata_matcher = None |
|
23
|
|
|
|
|
24
|
|
|
@property |
|
25
|
|
|
def database(self): |
|
26
|
|
|
return self._database_matcher |
|
27
|
|
|
|
|
28
|
|
|
@property |
|
29
|
|
|
def metadata(self): |
|
30
|
|
|
return self._metadata_matcher |
|
31
|
|
|
|
|
32
|
|
|
def configure(self): |
|
33
|
|
|
extended = Preferences.get('matcher.mode') == MatcherMode.PlexExtended |
|
34
|
|
|
|
|
35
|
|
|
# Configure matchers |
|
36
|
|
|
matchers = [ |
|
37
|
|
|
self._database_matcher, |
|
38
|
|
|
self._metadata_matcher, |
|
39
|
|
|
|
|
40
|
|
|
# Default matchers |
|
41
|
|
|
plex_database.matcher.Default, |
|
42
|
|
|
plex_metadata.matcher.Default |
|
43
|
|
|
] |
|
44
|
|
|
|
|
45
|
|
|
for matcher in matchers: |
|
46
|
|
|
# Update cache |
|
47
|
|
|
matcher.cache = self._cache |
|
48
|
|
|
|
|
49
|
|
|
# Configure features |
|
50
|
|
|
if matcher.caper_enabled != extended or matcher.extend_enabled != extended: |
|
51
|
|
|
matcher.caper_enabled = extended |
|
52
|
|
|
matcher.extend_enabled = extended |
|
53
|
|
|
|
|
54
|
|
|
log.info('Extended matcher has been %s on %r', 'enabled' if extended else 'disabled', matcher) |
|
55
|
|
|
|
|
56
|
|
|
return True |
|
57
|
|
|
|
|
58
|
|
|
def flush(self, force=False): |
|
59
|
|
|
if not self._cache: |
|
60
|
|
|
return False |
|
61
|
|
|
|
|
62
|
|
|
self._cache.flush(force=force) |
|
63
|
|
|
return True |
|
64
|
|
|
|
|
65
|
|
|
def prime(self, keys=None, force=False): |
|
66
|
|
|
if not self._cache: |
|
67
|
|
|
return DummyContext() |
|
68
|
|
|
|
|
69
|
|
|
return self._cache.prime( |
|
70
|
|
|
keys=keys, |
|
71
|
|
|
force=force |
|
72
|
|
|
) |
|
73
|
|
|
|
|
74
|
|
|
def process(self, episode): |
|
75
|
|
|
if not episode or not isinstance(episode, Episode): |
|
76
|
|
|
raise ValueError('Invalid value specified for the "episode" parameter: %r' % (episode,)) |
|
77
|
|
|
|
|
78
|
|
|
if not self._metadata_matcher: |
|
79
|
|
|
return episode.season.index, [episode.index] |
|
80
|
|
|
|
|
81
|
|
|
# Process episode with matcher |
|
82
|
|
|
season, episodes = self._metadata_matcher.process(episode) |
|
83
|
|
|
|
|
84
|
|
|
log.debug('Matcher returned season: %r, episodes: %r', season, episodes) |
|
85
|
|
|
return season, episodes |
|
86
|
|
|
|
|
87
|
|
|
def start(self): |
|
88
|
|
|
if self._database_matcher and self._metadata_matcher: |
|
89
|
|
|
return |
|
90
|
|
|
|
|
91
|
|
|
if self._database_matcher is False or self._metadata_matcher is False: |
|
92
|
|
|
return |
|
93
|
|
|
|
|
94
|
|
|
try: |
|
95
|
|
|
self._cache = CacheManager.get('plex.matcher') |
|
96
|
|
|
|
|
97
|
|
|
self._database_matcher = plex_database.matcher.Matcher(self._cache, Plex.client) |
|
98
|
|
|
self._metadata_matcher = plex_metadata.matcher.Matcher(self._cache, Plex.client) |
|
99
|
|
|
except Exception, ex: |
|
100
|
|
|
log.warn('Unable to initialize matchers: %s', ex, exc_info=True) |
|
101
|
|
|
|
|
102
|
|
|
# Mark attributes as disabled |
|
103
|
|
|
self._cache = False |
|
104
|
|
|
self._database_matcher = False |
|
105
|
|
|
self._metadata_matcher = False |
|
106
|
|
|
return |
|
107
|
|
|
|
|
108
|
|
|
# Configure matchers |
|
109
|
|
|
self.configure() |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
class DummyContext(object): |
|
113
|
|
|
def __enter__(self): |
|
114
|
|
|
pass |
|
115
|
|
|
|
|
116
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb): |
|
117
|
|
|
pass |
|
118
|
|
|
|