File size: 2,296 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Classify a ``.vplay`` query so the user sees an accurate status message.

The distinction matters because the three cases take visibly different time:

- a bare 11-char id skips search entirely — only metadata is fetched
- a URL is a direct reference — only metadata is fetched
- free text needs a search round-trip *and* then a metadata fetch

Reporting "Searching: never gonna give you up" for a bare video id is
technically harmless but reads as though something slow is happening, and
reporting "Fetching" for free text hides the search step that actually
dominates the wait.
"""

from __future__ import annotations

import re
from enum import Enum

__all__ = ["QueryKind", "classify", "status_head", "status_rows"]


class QueryKind(str, Enum):
    VIDEO_ID = "id"
    URL = "url"
    SEARCH = "search"


#: youtube.com/watch?v=ID, youtu.be/ID, /shorts/ID, /embed/ID, /live/ID
_URL_RE = re.compile(
    r"(?:youtube\.com/(?:watch\?(?:.*&)?v=|shorts/|embed/|live/|v/)|youtu\.be/)"
    r"([A-Za-z0-9_-]{11})",
    re.IGNORECASE,
)
#: a bare id with no surrounding characters
_BARE_ID_RE = re.compile(r"^[A-Za-z0-9_-]{11}$")
#: any URL at all (including non-YouTube, which the resolver may still try)
_ANY_URL_RE = re.compile(r"^https?://", re.IGNORECASE)


def classify(query: str) -> QueryKind:
    """Return the kind of a raw ``.vplay`` argument."""
    q = (query or "").strip()
    if not q:
        return QueryKind.SEARCH
    if _BARE_ID_RE.match(q):
        return QueryKind.VIDEO_ID
    if _URL_RE.search(q) or _ANY_URL_RE.match(q):
        return QueryKind.URL
    return QueryKind.SEARCH


def status_head(kind: QueryKind, query: str) -> str:
    """The one-line status shown while the query is being resolved."""
    q = (query or "").strip()
    if kind is QueryKind.VIDEO_ID:
        return f"Fetching track `{q}`"
    if kind is QueryKind.URL:
        return f"Fetching link `{q}`"
    return f"Searching `{q}`"


def status_rows(kind: QueryKind) -> dict:
    """Extra detail rows for the status message."""
    if kind is QueryKind.VIDEO_ID:
        return {"Stage": "direct id", "Next": "reading metadata"}
    if kind is QueryKind.URL:
        return {"Stage": "direct link", "Next": "reading metadata"}
    return {"Stage": "search", "Next": "picking the best match"}