test_platform_video_url.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from __future__ import annotations
  2. from content_agent.integrations.platform_video_url import (
  3. classify_url_candidate,
  4. find_url_candidates,
  5. select_video_url,
  6. )
  7. def _probe(status: str = "verified"):
  8. def probe(url: str, platform: str):
  9. return {
  10. "http_status": 206 if status == "verified" else None,
  11. "content_type": "video/mp4" if status == "verified" else "",
  12. "content_range": "bytes 0-99/100" if status == "verified" else "",
  13. "range_bytes": 100 if status == "verified" else 0,
  14. "looks_like_video": status == "verified",
  15. "probe_status": status,
  16. }
  17. return probe
  18. def test_classifies_urls_without_ad_substring_false_positive():
  19. payload = {
  20. "content_link": "https://www.example.com/item",
  21. "image_url_list": [{"image_url": "https://img.example/a.jpg"}],
  22. "avatar": "https://wx.qlogo.cn/avatar",
  23. "bgm_data": {"play_url": "https://audio.example/a.m4a"},
  24. "ads": {"video_url": "https://ad.example/ad.mp4"},
  25. "video_url_list": [
  26. {
  27. "video_url": (
  28. "https://findermp.video.qq.com/251/20302/stodownload?"
  29. "head=1&token=abc"
  30. )
  31. }
  32. ],
  33. }
  34. by_path = {item.path: item for item in find_url_candidates(payload)}
  35. assert by_path["$.content_link"].kind == "page"
  36. assert by_path["$.image_url_list[0].image_url"].kind == "image"
  37. assert by_path["$.avatar"].kind == "avatar"
  38. assert by_path["$.bgm_data.play_url"].kind == "audio"
  39. assert by_path["$.ads.video_url"].kind == "ad_or_material"
  40. assert by_path["$.video_url_list[0].video_url"].kind == "video"
  41. assert classify_url_candidate("$.video_url", "https://x/stodownload?head=1")[0] == "video"
  42. def test_kuaishou_prefers_verified_detail_video_over_search():
  43. result = select_video_url(
  44. "kuaishou",
  45. [
  46. ("search", {"video_url_list": [{"video_url": "https://v23-3.kwaicdn.com/search.mp4"}]}),
  47. ("detail", {"video_url_list": [{"video_url": "https://tymov2.a.kwimgs.com/detail.mp4"}]}),
  48. ],
  49. probe_fn=_probe(),
  50. )
  51. assert result["play_url"] == "https://tymov2.a.kwimgs.com/detail.mp4"
  52. assert result["platform_raw_payload"]["selected_video_url_path"] == "$.detail.video_url_list[0].video_url"
  53. assert result["platform_raw_payload"]["selected_video_url_probe_status"] == "verified"
  54. def test_shipinhao_prefers_findermp_video_and_filters_non_video():
  55. result = select_video_url(
  56. "shipinhao",
  57. [
  58. (
  59. "search",
  60. {
  61. "image_url_list": [{"image_url": "https://findermp.video.qq.com/cover.jpg"}],
  62. "video_url_list": [{"video_url": "https://findermp.video.qq.com/video.mp4"}],
  63. },
  64. )
  65. ],
  66. probe_fn=_probe(),
  67. )
  68. assert result["play_url"] == "https://findermp.video.qq.com/video.mp4"
  69. assert result["platform_raw_payload"]["video_url_candidate_counts"] == {
  70. "image": 1,
  71. "video": 1,
  72. }
  73. def test_probe_failure_falls_back_to_strong_video_candidate():
  74. result = select_video_url(
  75. "kuaishou",
  76. [("search", {"video_url_list": [{"video_url": "https://v23-3.kwaicdn.com/search.mp4"}]})],
  77. probe_fn=_probe("failed"),
  78. )
  79. assert result["play_url"] == "https://v23-3.kwaicdn.com/search.mp4"
  80. assert result["platform_raw_payload"]["selected_video_url_probe_status"] == "failed_fallback"
  81. assert result["media_failure_reason"] is None
  82. def test_no_video_candidate_returns_no_valid_play_url():
  83. result = select_video_url(
  84. "kuaishou",
  85. [("search", {"image_url_list": [{"image_url": "https://img.example/a.jpg"}]})],
  86. probe_fn=_probe(),
  87. )
  88. assert result["play_url"] is None
  89. assert result["media_failure_reason"] == "no_valid_play_url"
  90. assert result["platform_raw_payload"]["no_valid_play_url"] is True