Skip to content

Commit 0cf0883

Browse files
authored
* fix sigma67#582 * fix result type heuristics
1 parent ae966bb commit 0cf0883

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

tests/mixins/test_search.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def test_search_localized(self):
4040
yt_local = YTMusic(language="it")
4141
results = yt_local.search("ABBA")
4242
assert all(result["resultType"] in ALL_RESULT_TYPES for result in results)
43+
assert len([x for x in results if x["resultType"] == "album"]) <= 10 # album is default fallback
44+
45+
results = yt_local.search("ABBA", filter="songs")
46+
assert all(item["resultType"] == "song" for item in results)
4347

4448
def test_search_filters(self, yt_auth):
4549
query = "hip hop playlist"

ytmusicapi/mixins/search.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,38 +205,44 @@ def search(
205205
filter = scopes[1]
206206

207207
for res in section_list:
208+
result_type = category = None
209+
search_result_types = self.parser.get_search_result_types()
210+
208211
if "musicCardShelfRenderer" in res:
209212
top_result = parse_top_result(
210213
res["musicCardShelfRenderer"], self.parser.get_search_result_types()
211214
)
212215
search_results.append(top_result)
213216
if not (shelf_contents := nav(res, ["musicCardShelfRenderer", "contents"], True)):
214217
continue
215-
type = category = None
216218
# if "more from youtube" is present, remove it - it's not parseable
217219
if "messageRenderer" in shelf_contents[0]:
218220
category = nav(shelf_contents.pop(0), ["messageRenderer", *TEXT_RUN_TEXT])
219221

220222
elif "musicShelfRenderer" in res:
221223
shelf_contents = res["musicShelfRenderer"]["contents"]
222224
category = nav(res, MUSIC_SHELF + TITLE_TEXT, True)
223-
type_filter = filter or category
224225

225-
type = type_filter[:-1].lower() if type_filter else None
226+
# if we know the filter it's easy to set the result type
227+
# unfortunately uploads is modeled as a filter (historical reasons),
228+
# so we take care to not set the result type for that scope
229+
if filter and not scope == scopes[1]:
230+
result_type = filter[:-1].lower()
226231

227232
else:
228233
continue
229234

230-
search_result_types = self.parser.get_search_result_types()
231-
search_results.extend(parse_search_results(shelf_contents, search_result_types, type, category))
235+
search_results.extend(
236+
parse_search_results(shelf_contents, search_result_types, result_type, category)
237+
)
232238

233239
if filter: # if filter is set, there are continuations
234240

235241
def request_func(additionalParams):
236242
return self._send_request(endpoint, body, additionalParams)
237243

238244
def parse_func(contents):
239-
return parse_search_results(contents, search_result_types, type, category)
245+
return parse_search_results(contents, search_result_types, result_type, category)
240246

241247
search_results.extend(
242248
get_continuations(

ytmusicapi/parsers/search.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,19 @@ def parse_search_result(data, search_result_types, result_type, category):
6161
search_result = {"category": category}
6262
video_type = nav(data, [*PLAY_BUTTON, "playNavigationEndpoint", *NAVIGATION_VIDEO_TYPE], True)
6363

64-
# try to determine the result type based on the first run
65-
if result_type not in ALL_RESULT_TYPES: # i.e. localized result_type
66-
result_type = get_search_result_type(get_item_text(data, 1), search_result_types)
67-
6864
# determine result type based on browseId
6965
# if there was no category title (i.e. for extra results in Top Result)
7066
if not result_type:
7167
if browse_id := nav(data, NAVIGATION_BROWSE_ID, True):
7268
mapping = {
73-
"VMPL": "playlist",
69+
"VM": "playlist",
7470
"RD": "playlist",
71+
"VL": "playlist",
7572
"MPLA": "artist",
7673
"MPRE": "album",
7774
"MPSP": "podcast",
7875
"MPED": "episode",
76+
"UC": "artist",
7977
}
8078
result_type = next(
8179
iter(type for prefix, type in mapping.items() if browse_id.startswith(prefix)), None

0 commit comments

Comments
 (0)