Utilities¶
utils
¶
Utility functions for Griddy SDK.
Classes¶
Cookie
¶
Cookie(
domain: str,
path: str,
secure: bool,
expires: int | None,
name: str,
value: str,
http_only: bool = True,
include_subdomains: bool = False,
)
Represents a single HTTP cookie.
Source code in griddy/core/utils.py
Attributes¶
Functions¶
matches_domain
¶
Check if this cookie matches the given domain.
Source code in griddy/core/utils.py
matches_path
¶
to_dict
¶
Functions¶
retry_on_rate_limit
¶
Decorator to retry function calls on rate limit errors.
| PARAMETER | DESCRIPTION |
|---|---|
max_retries
|
Maximum number of retry attempts
TYPE:
|
backoff_factor
|
Factor for exponential backoff
TYPE:
|
Source code in griddy/core/utils.py
parse_date
¶
Parse date string into datetime object.
| PARAMETER | DESCRIPTION |
|---|---|
date_str
|
Date string in various formats
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
datetime | None
|
Parsed datetime object or None |
Source code in griddy/core/utils.py
clean_text
¶
Clean and normalize text data.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
Text to clean
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str | None
|
Cleaned text or None |
Source code in griddy/core/utils.py
safe_int
¶
Safely convert value to integer.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Value to convert
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int | None
|
Integer value or None |
Source code in griddy/core/utils.py
safe_float
¶
Safely convert value to float.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Value to convert
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float | None
|
Float value or None |
Source code in griddy/core/utils.py
build_url
¶
Build URL from base URL, path, and parameters.
| PARAMETER | DESCRIPTION |
|---|---|
base_url
|
Base URL
TYPE:
|
path
|
URL path
TYPE:
|
params
|
Query parameters
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Complete URL |
Source code in griddy/core/utils.py
parse_cookies_txt
¶
parse_cookies_txt(file_path: str | Path) -> list[Cookie]
Parse a cookies.txt file and return a list of Cookie objects.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Path to the cookies.txt file
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Cookie]
|
List of Cookie objects |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the cookies file doesn't exist |
ValueError
|
If the file format is invalid |
Source code in griddy/core/utils.py
extract_cookies_for_url
¶
extract_cookies_for_url(
cookies_file: str | Path,
target_url: str,
include_expired: bool = False,
) -> list[Cookie]
Extract cookies that match a specific URL from a cookies.txt file.
| PARAMETER | DESCRIPTION |
|---|---|
cookies_file
|
Path to the cookies.txt file
TYPE:
|
target_url
|
URL to match cookies against
TYPE:
|
include_expired
|
Whether to include expired cookies
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Cookie]
|
List of matching Cookie objects |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the cookies file doesn't exist |
ValueError
|
If the URL or file format is invalid |
Source code in griddy/core/utils.py
cookies_to_dict
¶
cookies_to_dict(cookies: list[Cookie]) -> dict[str, str]
Convert a list of cookies to a dictionary format.
| PARAMETER | DESCRIPTION |
|---|---|
cookies
|
List of Cookie objects
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, str]
|
Dictionary with cookie names as keys and values as values |
Source code in griddy/core/utils.py
cookies_to_header
¶
cookies_to_header(cookies: list[Cookie]) -> str
Convert a list of cookies to a Cookie header string.
| PARAMETER | DESCRIPTION |
|---|---|
cookies
|
List of Cookie objects
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Cookie header string (e.g., "name1=value1; name2=value2") |
Source code in griddy/core/utils.py
extract_cookies_as_dict
¶
extract_cookies_as_dict(
cookies_file: str | Path,
target_url: str,
include_expired: bool = False,
) -> dict[str, str]
Extract cookies for a URL and return as a dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
cookies_file
|
Path to the cookies.txt file
TYPE:
|
target_url
|
URL to match cookies against
TYPE:
|
include_expired
|
Whether to include expired cookies
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, str]
|
Dictionary with cookie names as keys and values as values |
Source code in griddy/core/utils.py
extract_cookies_as_header
¶
extract_cookies_as_header(
cookies_file: str | Path,
target_url: str,
include_expired: bool = False,
) -> str
Extract cookies for a URL and return as a Cookie header string.
| PARAMETER | DESCRIPTION |
|---|---|
cookies_file
|
Path to the cookies.txt file
TYPE:
|
target_url
|
URL to match cookies against
TYPE:
|
include_expired
|
Whether to include expired cookies
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Cookie header string suitable for HTTP requests |