-
Notifications
You must be signed in to change notification settings - Fork 0
/
guid.py
108 lines (79 loc) · 2.7 KB
/
guid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from plex_metadata.agents import Agents
from plex_metadata.core.helpers import urlparse
import logging
log = logging.getLogger(__name__)
unsupported_agents = {}
class Guid(object):
def __init__(self, value, extra=None):
self.value = value
self.extra = extra
# Identifier
self.service = None
self.id = None
# Show
self.season = None
self.episode = None
# Optional
self.language = None
@property
def agent(self):
return self.service
@property
def sid(self):
# `sid` property always returns strings
return str(self.id)
@classmethod
def construct(cls, service, id, extra=None):
result = cls(id, extra)
result.service = service
result.id = id
return result
@classmethod
def parse(cls, guid, match=True, media=None, strict=False):
if not guid:
return None
# Parse Guid URI
agent_name, uri = urlparse(guid)
if not agent_name or not uri or not uri.netloc:
return None
# Construct `Guid` object
result = Guid(uri.netloc, uri.query)
# Match guid with agent, fill with details
if match and cls.match(agent_name, result, uri, media):
return result
if strict:
return None
# No agent matching enabled, basic fill
result.service = agent_name[agent_name.rfind('.') + 1:]
result.id = uri.netloc
return result
@classmethod
def match(cls, agent_name, guid, uri, media=None):
# Retrieve `Agent` for provided `guid`
agent = Agents.get(agent_name)
if agent is None:
if agent_name not in unsupported_agents:
# First occurrence of unsupported agent
log.warn('Unsupported metadata agent: %s' % agent_name)
# Mark unsupported agent as "seen"
unsupported_agents[agent_name] = True
return False
# Duplicate occurrence of unsupported agent
log.warn('Unsupported metadata agent: %s' % agent_name, extra={
'duplicate': True
})
return False
# Fill `guid` with details from agent
return agent.fill(guid, uri, media)
def __repr__(self):
parameters = [
'service: %r' % self.service,
'id: %r' % self.id
]
if self.season is not None:
parameters.append('season: %r' % self.season)
if self.episode is not None:
parameters.append('episode: %r' % self.episode)
return '<Guid - %s>' % ', '.join(parameters)
def __str__(self):
return self.__repr__()