|
3 | 3 | import os |
4 | 4 | import shutil |
5 | 5 | import sys |
| 6 | +import unicodedata |
6 | 7 | from urllib.parse import parse_qsl, quote_plus |
7 | 8 |
|
8 | 9 | import requests |
@@ -142,7 +143,7 @@ def nyaa_menu(): |
142 | 143 | True, |
143 | 144 | ), |
144 | 145 | ], |
145 | | - ), |
| 146 | + ) |
146 | 147 | xbmcplugin.endOfDirectory(HANDLE) |
147 | 148 |
|
148 | 149 |
|
@@ -185,7 +186,7 @@ def sukebei_menu(): |
185 | 186 | True, |
186 | 187 | ), |
187 | 188 | ], |
188 | | - ), |
| 189 | + ) |
189 | 190 | xbmcplugin.endOfDirectory(HANDLE) |
190 | 191 |
|
191 | 192 |
|
@@ -307,15 +308,99 @@ def _play_nyaa(selected_file=None, url=None, magnet=None): |
307 | 308 | resolved_url = resolveurl.HostedMediaFile(url=magnet).resolve() |
308 | 309 | else: |
309 | 310 | all_urls = resolveurl.resolve(magnet, return_all=True) |
| 311 | + log( |
| 312 | + "resolveurl return_all for selected_file={} -> type={} value={}".format( |
| 313 | + selected_file, type(all_urls).__name__, all_urls |
| 314 | + ) |
| 315 | + ) |
310 | 316 | if isinstance(all_urls, str): |
311 | 317 | # sometimes we get it already resolved as a single string? |
312 | 318 | # this should fix: string indices must be integers, not 'str' |
313 | 319 | resolved_url = all_urls |
| 320 | + elif not all_urls: |
| 321 | + log("resolveurl return_all empty; falling back to magnet resolve") |
| 322 | + resolved_url = resolveurl.resolve(magnet) |
314 | 323 | else: |
315 | | - selected_url = next( |
316 | | - filter(lambda x: selected_file in x["name"], all_urls) |
317 | | - )["link"] |
318 | | - resolved_url = resolveurl.resolve(selected_url) |
| 324 | + |
| 325 | + def normalize_match_text(value, ascii_only=False): |
| 326 | + if not value: |
| 327 | + return "" |
| 328 | + normalized = unicodedata.normalize("NFKC", value) |
| 329 | + if ascii_only: |
| 330 | + normalized = normalized.encode("ascii", "ignore").decode( |
| 331 | + "ascii" |
| 332 | + ) |
| 333 | + return " ".join(normalized.split()).casefold() |
| 334 | + |
| 335 | + selected_normalized = normalize_match_text(selected_file) |
| 336 | + selected_ascii = normalize_match_text(selected_file, ascii_only=True) |
| 337 | + |
| 338 | + entries = [] |
| 339 | + for item in all_urls: |
| 340 | + name = item.get("name", "") if isinstance(item, dict) else str(item) |
| 341 | + entries.append( |
| 342 | + { |
| 343 | + "item": item, |
| 344 | + "normalized": normalize_match_text(name), |
| 345 | + "ascii": normalize_match_text(name, ascii_only=True), |
| 346 | + } |
| 347 | + ) |
| 348 | + |
| 349 | + def is_exact_match(entry): |
| 350 | + return ( |
| 351 | + entry["normalized"] |
| 352 | + and selected_normalized |
| 353 | + and entry["normalized"] == selected_normalized |
| 354 | + ) or ( |
| 355 | + entry["ascii"] |
| 356 | + and selected_ascii |
| 357 | + and entry["ascii"] == selected_ascii |
| 358 | + ) |
| 359 | + |
| 360 | + def is_partial_match(entry): |
| 361 | + return ( |
| 362 | + entry["normalized"] |
| 363 | + and selected_normalized |
| 364 | + and ( |
| 365 | + selected_normalized in entry["normalized"] |
| 366 | + or entry["normalized"] in selected_normalized |
| 367 | + ) |
| 368 | + ) or ( |
| 369 | + entry["ascii"] |
| 370 | + and selected_ascii |
| 371 | + and ( |
| 372 | + selected_ascii in entry["ascii"] |
| 373 | + or entry["ascii"] in selected_ascii |
| 374 | + ) |
| 375 | + ) |
| 376 | + |
| 377 | + matched_entry = next( |
| 378 | + (entry["item"] for entry in entries if is_exact_match(entry)), |
| 379 | + None, |
| 380 | + ) |
| 381 | + if not matched_entry: |
| 382 | + matched_entry = next( |
| 383 | + (entry["item"] for entry in entries if is_partial_match(entry)), |
| 384 | + None, |
| 385 | + ) |
| 386 | + if not matched_entry: |
| 387 | + log( |
| 388 | + "resolveurl could not match selected_file={}; normalized={} ascii={}; using first entry".format( |
| 389 | + selected_file, selected_normalized, selected_ascii |
| 390 | + ) |
| 391 | + ) |
| 392 | + matched_entry = all_urls[0] |
| 393 | + if isinstance(matched_entry, dict): |
| 394 | + selected_url = matched_entry.get("link") |
| 395 | + else: |
| 396 | + selected_url = matched_entry |
| 397 | + if not selected_url: |
| 398 | + log( |
| 399 | + "resolveurl selected_url missing; falling back to magnet resolve" |
| 400 | + ) |
| 401 | + resolved_url = resolveurl.resolve(magnet) |
| 402 | + else: |
| 403 | + resolved_url = resolveurl.resolve(selected_url) |
319 | 404 | play_item = xbmcgui.ListItem(path=resolved_url) |
320 | 405 | xbmcplugin.setResolvedUrl(HANDLE, True, listitem=play_item) |
321 | 406 |
|
|
0 commit comments