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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
"""Utilities for extracting and formatting metadata from TIDAL objects."""
from contextlib import suppress
def _convert_list_to_str(value: list | tuple) -> str:
"""Convert list/tuple to comma-separated string."""
if not value:
return "—"
with suppress(Exception):
return ", ".join([str(x) for x in value])
return str(value)
def _convert_dict_to_str(value: dict) -> str:
"""Extract meaningful string from dict."""
if "name" in value and value["name"]:
return str(value["name"])
for k in ("label", "title", "genre", "name"):
if k in value and value[k]:
return str(value[k])
with suppress(Exception):
vals = [str(v) for v in value.values() if v is not None]
if vals:
return ", ".join(vals)
return str(value)
def safe_str(value: object) -> str:
"""Convert a potentially non-str value into a safe string for display.
Args:
value: The value to convert.
Returns:
str: Safe string representation, '—' for None/empty.
"""
with suppress(Exception):
if value is None:
return "—"
if isinstance(value, str):
return value if value != "" else "—"
if isinstance(value, list | tuple):
return _convert_list_to_str(value)
if isinstance(value, dict):
return _convert_dict_to_str(value)
return str(value)
return "—"
def _find_in_dict_container(container: dict, names: tuple[str, ...]) -> object | None:
"""Search for names in a dict container."""
for n in names:
if n in container and container[n] is not None:
return container[n]
# Fuzzy key match
keys = list(container.keys())
for n in names:
for k in keys:
if n.lower() in str(k).lower():
return container[k]
return None
def _fuzzy_scan_attrs(obj: object, names: tuple[str, ...]) -> object | None:
"""Fuzzy scan object attributes for matching names."""
with suppress(Exception):
for k in dir(obj):
kl = k.lower()
for n in names:
if n.lower() in kl:
with suppress(Exception):
val = getattr(obj, k)
if val is not None:
return val
return None
def find_attr(obj: object, *names: str) -> object | None:
"""Attempt to find an attribute or data key from an object or its internals.
Args:
obj: The object to inspect.
*names: Attribute/key names to search for.
Returns:
The found value or None.
"""
# Direct attributes
for n in names:
with suppress(Exception):
if hasattr(obj, n):
val = getattr(obj, n)
if val is not None:
return val
# Inspect common dict-like internals
for container_name in ("_data", "data", "__dict__"):
with suppress(Exception):
container = getattr(obj, container_name, None)
if isinstance(container, dict):
result = _find_in_dict_container(container, names)
if result is not None:
return result
# Fuzzy scan of attributes
return _fuzzy_scan_attrs(obj, names)
def _scan_dict_recursive(container: dict, key_substrings: list[str]) -> object | None:
"""Recursively scan a dict for keys matching any substring."""
for k, v in container.items():
kl = str(k).lower()
for s in key_substrings:
if s.lower() in kl and v is not None:
return v
# recurse into nested structures
if isinstance(v, dict):
found = _scan_dict_recursive(v, key_substrings)
if found is not None:
return found
if isinstance(v, list | tuple):
for item in v:
if isinstance(item, dict):
found = _scan_dict_recursive(item, key_substrings)
if found is not None:
return found
return None
def search_in_data(obj: object, key_substrings: list[str]) -> object | None:
"""Recursively search dict-like internals for keys containing any substring.
Args:
obj: The object to search.
key_substrings: List of key substrings to search for.
Returns:
The first matching value found, or None.
"""
# check common containers
for container_name in ("_data", "data", "__dict__"):
with suppress(Exception):
container = getattr(obj, container_name, None)
if isinstance(container, dict):
found = _scan_dict_recursive(container, key_substrings)
if found is not None:
return found
# as last resort, try obj.__dict__ if available
with suppress(Exception):
d = getattr(obj, "__dict__", None)
if isinstance(d, dict):
return _scan_dict_recursive(d, key_substrings)
return None
def _extract_name_from_dict(item: dict, match_types: tuple[str, ...] | None) -> str | None:
"""Extract name from a dict if it matches type filters."""
if "name" not in item or not item["name"]:
return None
if match_types:
t = item.get("type") or item.get("role") or item.get("credit_type")
if t and any(mt in str(t).lower() for mt in match_types):
return str(item["name"])
return None
return str(item["name"])
def _extract_name_from_item(item: object) -> str | None:
"""Extract name from various item formats."""
if item is None:
return None
if isinstance(item, str):
return item
if isinstance(item, dict):
# Try common name keys
for k in ("name", "artist", "person"):
if k in item and item[k]:
return str(item[k])
return None
# Try object attributes
nm = getattr(item, "name", None) or getattr(item, "title", None)
if nm:
return str(nm)
with suppress(Exception):
return str(item)
return None
def extract_names_from_mixed(value: object, match_types: tuple[str, ...] | None = None) -> list[str]:
"""Normalize various credit-like structures into a list of names.
Accepts lists of dicts, dicts, strings, or objects.
If match_types provided, only include entries where the type/role matches one of them.
Args:
value: The value to extract names from.
match_types: Optional tuple of role types to filter by.
Returns:
List of extracted names.
"""
if value is None:
return []
if isinstance(value, str):
return [value]
if isinstance(value, dict):
name = _extract_name_from_dict(value, match_types)
if name:
return [name]
vals = [str(v) for v in value.values() if v is not None]
return vals
if isinstance(value, list | tuple):
names: list[str] = []
for item in value:
if isinstance(item, dict):
name = _extract_name_from_dict(item, match_types)
if name:
names.append(name)
else:
name = _extract_name_from_item(item)
if name:
names.append(name)
return names
return []
|