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