File size: 1,620 Bytes
b380004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path

from agno.utils.log import log_info
from httpx import URL, Client, HTTPStatusError, RequestError
from pydantic import HttpUrl, ValidationError

VALID_STATUS_CODE = 200


def is_url(value: str | None) -> bool:
    """
    Check if a string is a valid URL.

    Args:
        value: The string to check. Can be None.

    Returns:
        bool: True if the string is a valid URL, False otherwise.
    """
    if value is None:
        return False

    try:
        _ = HttpUrl(value)
    except ValidationError:
        return False
    return True


def is_alive(url: HttpUrl) -> bool:
    """
    Check if a given URL is accessible and returns its availability status.

    Args:
        url (HttpUrl): The URL to check for accessibility.

    Returns:
        bool: True if the URL is accessible with a valid status code, otherwise False.
    """
    try:
        with Client() as client:
            response = client.get(str(url))
            return response.status_code == VALID_STATUS_CODE
    except (RequestError, HTTPStatusError):
        return False


def download_file(url: URL, path: Path) -> None:
    """
    Download a file from a URL and save it to a local path.

    Args:
        url (URL): The URL to download the file from.
        path (Path): The local file path where the downloaded file will be saved.

    Returns:
        None
    """
    log_info(f"Downloading {url} to {path}")
    with Client() as client:
        response = client.get(url)
        response.raise_for_status()
        path.write_bytes(response.content)
    log_info(f"Downloaded {url} to {path}")