|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (c) 2025 relakkes@gmail.com |
| 3 | +# |
| 4 | +# This file is part of MediaCrawler project. |
| 5 | +# Repository: https://github.com/NanmiCoder/MediaCrawler/blob/main/tests/test_douyin_url_parse.py |
| 6 | +# GitHub: https://github.com/NanmiCoder |
| 7 | +# Licensed under NON-COMMERCIAL LEARNING LICENSE 1.1 |
| 8 | +# |
| 9 | + |
| 10 | +"""抖音作品输入解析的回归测试。 |
| 11 | +
|
| 12 | +图文作品的链接是 ``/note/<aweme_id>``,短链跳转后落到 iesdouyin.com 的 |
| 13 | +``/share/note/<aweme_id>/``。它们和视频一样都带作品 ID,应直接解析出来, |
| 14 | +而不是被当成短链再去请求一次。 |
| 15 | +
|
| 16 | +这里只测纯函数,不发网络请求。 |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from media_platform.douyin.help import parse_video_info_from_url |
| 24 | + |
| 25 | + |
| 26 | +def test_pure_aweme_id_is_normal(): |
| 27 | + info = parse_video_info_from_url("7525082444551310602") |
| 28 | + |
| 29 | + assert info.aweme_id == "7525082444551310602" |
| 30 | + assert info.url_type == "normal" |
| 31 | + |
| 32 | + |
| 33 | +def test_video_url_is_normal(): |
| 34 | + info = parse_video_info_from_url("https://www.douyin.com/video/7525082444551310602") |
| 35 | + |
| 36 | + assert info.aweme_id == "7525082444551310602" |
| 37 | + assert info.url_type == "normal" |
| 38 | + |
| 39 | + |
| 40 | +def test_modal_id_url(): |
| 41 | + info = parse_video_info_from_url( |
| 42 | + "https://www.douyin.com/root/search/python?modal_id=7471165520058862848" |
| 43 | + ) |
| 44 | + |
| 45 | + assert info.aweme_id == "7471165520058862848" |
| 46 | + assert info.url_type == "modal" |
| 47 | + |
| 48 | + |
| 49 | +def test_note_url_is_normal(): |
| 50 | + """图文作品页 /note/<id> 不能被当成短链""" |
| 51 | + info = parse_video_info_from_url("https://www.douyin.com/note/7525082444551310602") |
| 52 | + |
| 53 | + assert info.aweme_id == "7525082444551310602" |
| 54 | + assert info.url_type == "normal" |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize( |
| 58 | + "url", |
| 59 | + [ |
| 60 | + "https://www.iesdouyin.com/share/note/7525082444551310602/?region=CN&mid=7525082500000000000", |
| 61 | + "https://www.iesdouyin.com/share/video/7525082444551310602/?region=CN&mid=7525082500000000000", |
| 62 | + ], |
| 63 | +) |
| 64 | +def test_resolved_share_url_is_normal(url): |
| 65 | + """短链跳转后的 share 页(视频或图文)都要能取到作品 ID""" |
| 66 | + info = parse_video_info_from_url(url) |
| 67 | + |
| 68 | + assert info.aweme_id == "7525082444551310602" |
| 69 | + assert info.url_type == "normal" |
| 70 | + |
| 71 | + |
| 72 | +def test_share_short_link_is_marked_short(): |
| 73 | + info = parse_video_info_from_url("https://v.douyin.com/iF12345ABC/") |
| 74 | + |
| 75 | + assert info.aweme_id == "" |
| 76 | + assert info.url_type == "short" |
| 77 | + |
| 78 | + |
| 79 | +def test_unknown_long_url_raises(): |
| 80 | + with pytest.raises(ValueError): |
| 81 | + parse_video_info_from_url( |
| 82 | + "https://www.douyin.com/user/MS4wLjABAAAATJPY7LAlaa5X-c8uNdWkvz0jUGgpw4eeXIwu_8BhvqE" |
| 83 | + ) |
0 commit comments