|
1 | 1 | from typing import Union, List |
2 | 2 | from youtubesearchpython.core.constants import * |
| 3 | +from urllib.parse import urlparse, parse_qs |
| 4 | +import re |
3 | 5 |
|
4 | 6 |
|
5 | | -def getValue(source: dict, path: List[str]) -> Union[str, int, dict, None]: |
| 7 | +def getValue(source: dict, path: List[Union[str, int]]) -> Union[str, int, dict, None]: |
6 | 8 | value = source |
| 9 | + |
7 | 10 | for key in path: |
8 | | - if type(key) is str: |
9 | | - if key in value.keys(): |
10 | | - value = value[key] |
11 | | - else: |
12 | | - value = None |
13 | | - break |
14 | | - elif type(key) is int: |
15 | | - if len(value) != 0: |
16 | | - value = value[key] |
17 | | - else: |
18 | | - value = None |
19 | | - break |
| 11 | + if value is None: |
| 12 | + return None |
| 13 | + |
| 14 | + if isinstance(key, str): |
| 15 | + if not isinstance(value, dict): |
| 16 | + return None |
| 17 | + value = value.get(key) |
| 18 | + if value is None: |
| 19 | + return None |
| 20 | + |
| 21 | + elif isinstance(key, int): |
| 22 | + if not isinstance(value, (list, tuple)): |
| 23 | + return None |
| 24 | + if key < 0 or key >= len(value): |
| 25 | + return None |
| 26 | + value = value[key] |
| 27 | + |
| 28 | + else: |
| 29 | + return None |
| 30 | + |
20 | 31 | return value |
21 | 32 |
|
22 | 33 |
|
23 | 34 | def getVideoId(videoLink: str) -> str: |
24 | | - if 'youtu.be' in videoLink: |
25 | | - path_part = videoLink.split('?')[0].split('#')[0] |
26 | | - if path_part[-1] == '/': |
27 | | - return path_part.split('/')[-2] |
28 | | - return path_part.split('/')[-1] |
29 | | - elif 'youtube.com' in videoLink: |
30 | | - if '/shorts/' in videoLink: |
31 | | - path_part = videoLink.split('/shorts/')[1].split('?')[0].split('#')[0] |
32 | | - return path_part |
33 | | - elif '/live/' in videoLink: |
34 | | - path_part = videoLink.split('/live/')[1].split('?')[0].split('#')[0] |
35 | | - return path_part |
36 | | - elif 'v=' in videoLink: |
37 | | - v_index = videoLink.index('v=') + 2 |
38 | | - end_index = len(videoLink) |
39 | | - for char in ['&', '#']: |
40 | | - if char in videoLink[v_index:]: |
41 | | - end_index = min(end_index, videoLink.index(char, v_index)) |
42 | | - return videoLink[v_index:end_index] |
43 | | - return videoLink |
44 | | - else: |
| 35 | + try: |
| 36 | + parsed = urlparse(videoLink) |
| 37 | + host = (parsed.netloc or "").lower() |
| 38 | + |
| 39 | + if "youtu.be" in host: |
| 40 | + path = parsed.path.rstrip("/") |
| 41 | + if path: |
| 42 | + return path.split("/")[-1] |
| 43 | + |
| 44 | + if "youtube" in host or "youtube-nocookie" in host: |
| 45 | + qs = parse_qs(parsed.query) |
| 46 | + |
| 47 | + if "v" in qs and qs["v"]: |
| 48 | + return qs["v"][0] |
| 49 | + |
| 50 | + parts = [p for p in parsed.path.split("/") if p] |
| 51 | + |
| 52 | + for i, p in enumerate(parts): |
| 53 | + if p in ("embed", "v", "live") and i + 1 < len(parts): |
| 54 | + return parts[i + 1] |
| 55 | + |
| 56 | + if parts: |
| 57 | + return parts[-1] |
| 58 | + core = videoLink.split("?")[0].split("#")[0].rstrip("/") |
| 59 | + if "/" in core: |
| 60 | + return core.split("/")[-1] |
| 61 | + return core |
| 62 | + |
| 63 | + except Exception: |
45 | 64 | return videoLink |
46 | 65 |
|
47 | 66 |
|
|
0 commit comments