yt-dlp-utils 0.0.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ """Utilities for programmatic use of yt-dlp."""
2
+ from __future__ import annotations
3
+
4
+ from .lib import YoutubeDLLogger, get_configured_yt_dlp, setup_session
5
+
6
+ __all__ = ('YoutubeDLLogger', 'get_configured_yt_dlp', 'setup_session')
7
+ __version__ = '0.0.6'
@@ -0,0 +1,23 @@
1
+ """Constants."""
2
+ from __future__ import annotations
3
+
4
+ __all__ = ('DEFAULT_RETRY_BACKOFF_FACTOR', 'DEFAULT_RETRY_STATUS_FORCELIST', 'SHARED_HEADERS',
5
+ 'USER_AGENT')
6
+
7
+ DEFAULT_RETRY_STATUS_FORCELIST = (429, 500, 502, 503, 504)
8
+ """Default status codes to retry on."""
9
+ DEFAULT_RETRY_BACKOFF_FACTOR = 2.5
10
+ """Default backoff factor for retrying requests."""
11
+
12
+ USER_AGENT = ('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
13
+ 'Chrome/136.0.0.0 Safari/537.36')
14
+ """User agent."""
15
+ SHARED_HEADERS = {
16
+ 'accept': '*/*',
17
+ 'cache-control': 'no-cache',
18
+ 'content-type': 'application/vnd.api+json',
19
+ 'dnt': '1',
20
+ 'pragma': 'no-cache',
21
+ 'user-agent': USER_AGENT,
22
+ }
23
+ """Default headers for requests."""
yt_dlp_utils/lib.py ADDED
@@ -0,0 +1,142 @@
1
+ """Utilities."""
2
+ from __future__ import annotations
3
+
4
+ from typing import TYPE_CHECKING
5
+ import logging
6
+ import re
7
+ import sys
8
+
9
+ from requests.adapters import HTTPAdapter
10
+ from typing_extensions import Unpack
11
+ from urllib3 import Retry
12
+ from yt_dlp.cookies import extract_cookies_from_browser
13
+ import requests
14
+ import yt_dlp
15
+
16
+ from .constants import DEFAULT_RETRY_BACKOFF_FACTOR, DEFAULT_RETRY_STATUS_FORCELIST, SHARED_HEADERS
17
+
18
+ if TYPE_CHECKING:
19
+ from collections.abc import Collection, Iterable, Mapping
20
+
21
+ __all__ = ('YoutubeDLLogger', 'get_configured_yt_dlp', 'setup_session')
22
+
23
+ log = logging.getLogger(__name__)
24
+
25
+
26
+ class YoutubeDLLogger(yt_dlp.cookies.YDLLogger):
27
+ """Logger for yt-dlp."""
28
+ def debug(self, message: str) -> None: # noqa: PLR6301
29
+ """Log a debug message."""
30
+ if re.match(r'^\[download\]\s+[0-9\.]+%', message):
31
+ return
32
+ log.info('%s', re.sub(r'^\[(?:info|debug)\]\s+', '', message))
33
+
34
+ def info(self, message: str) -> None: # noqa: PLR6301
35
+ """Log an info message."""
36
+ log.info('%s', re.sub(r'^\[info\]\s+', '', message))
37
+
38
+ def warning( # type: ignore[override] # noqa: PLR6301
39
+ self, message: str, *, only_once: bool = False) -> None: # noqa: ARG002
40
+ """Log a warning message."""
41
+ log.warning('%s', re.sub(r'^\[warn(?:ing)?\]\s+', '', message))
42
+
43
+ def error(self, message: str) -> None: # noqa: PLR6301
44
+ """Log an error message."""
45
+ log.error('%s', re.sub(r'^\[err(?:or)?\]\s+', '', message))
46
+
47
+
48
+ def get_configured_yt_dlp(sleep_time: int = 3,
49
+ *,
50
+ debug: bool = False,
51
+ **kwargs: Unpack[yt_dlp._Params]) -> yt_dlp.YoutubeDL:
52
+ """
53
+ Get a configured ``YoutubeDL`` instance.
54
+
55
+ This function sets up a ``yt_dlp.YoutubeDL`` instance with the user's configuration (e.g.
56
+ located at ``~/.config/yt-dlp/config``). It overrides the default logger (``logger`` option),
57
+ disables colours (``color`` option), and sets the sleep time between requests
58
+ (``sleep_interval_requests`` option). It also sets the ``verbose`` flag based on the ``debug``
59
+ parameter.
60
+
61
+ All other keyword arguments are passed directly to the ``yt_dlp.YoutubeDL`` constructor.
62
+
63
+ Parameters
64
+ ----------
65
+ sleep_time : int
66
+ The time to sleep between requests, in seconds. Default is 3 seconds.
67
+ debug : bool
68
+ Whether to enable debug mode. Default is False.
69
+
70
+ Returns
71
+ -------
72
+ yt_dlp.YoutubeDL
73
+ A configured instance of `yt_dlp.YoutubeDL`_.
74
+ """
75
+ old_sys_argv = sys.argv
76
+ sys.argv = [sys.argv[0]]
77
+ ydl_opts = yt_dlp.parse_options()[-1]
78
+ ydl_opts['color'] = {'stdout': 'never', 'stderr': 'never'}
79
+ ydl_opts['logger'] = kwargs.pop('logger', YoutubeDLLogger())
80
+ ydl_opts['sleep_interval_requests'] = sleep_time
81
+ ydl_opts['verbose'] = debug
82
+ sys.argv = old_sys_argv
83
+ return yt_dlp.YoutubeDL(ydl_opts | kwargs)
84
+
85
+
86
+ def setup_session(browser: str,
87
+ profile: str,
88
+ add_headers: Mapping[str, str] | None = None,
89
+ backoff_factor: float = DEFAULT_RETRY_BACKOFF_FACTOR,
90
+ domains: Iterable[str] | None = None,
91
+ headers: Mapping[str, str] | None = None,
92
+ session: requests.Session | None = None,
93
+ status_forcelist: Collection[int] = DEFAULT_RETRY_STATUS_FORCELIST,
94
+ *,
95
+ setup_retry: bool = False) -> requests.Session:
96
+ """
97
+ Create or modify a Requests :py:class:`requests.Session` instance with cookies from the browser.
98
+
99
+ Parameters
100
+ ----------
101
+ browser : str
102
+ The browser to extract cookies from.
103
+ profile : str
104
+ The profile to extract cookies from.
105
+ add_headers : Mapping[str, str]
106
+ Additional headers to add to the requests session.
107
+ backoff_factor : float
108
+ The backoff factor to use for the retry mechanism.
109
+ domains : Iterable[str]
110
+ Filter the cookies to only those that match the specified domains.
111
+ headers : Mapping[str, str]
112
+ The headers to use for the requests session. If not specified, a default set will be used.
113
+ status_forcelist : Collection[int]
114
+ The status codes to retry on.
115
+ setup_retry : bool
116
+ Whether to set up a retry mechanism for the Requests session.
117
+
118
+ Returns
119
+ -------
120
+ requests.Session
121
+ A Requests session.
122
+ """
123
+ headers = headers or SHARED_HEADERS
124
+ add_headers = add_headers or {}
125
+ session = session or requests.Session()
126
+ session.headers.update(headers)
127
+ session.headers.update(add_headers)
128
+ if setup_retry:
129
+ session.mount(
130
+ 'https://',
131
+ HTTPAdapter(max_retries=Retry(backoff_factor=backoff_factor,
132
+ status_forcelist=status_forcelist)))
133
+ extracted = extract_cookies_from_browser(browser, profile)
134
+ if not domains:
135
+ session.cookies.update(extracted)
136
+ else:
137
+ for domain in (d.lstrip('.') for d in domains):
138
+ for cookie in extracted.get_cookies_for_url(f'https://{domain}'):
139
+ if not isinstance(cookie.value, str):
140
+ continue
141
+ session.cookies.set(cookie.name, cookie.value, domain=domain)
142
+ return session
yt_dlp_utils/py.typed ADDED
File without changes
@@ -0,0 +1,67 @@
1
+ Metadata-Version: 2.4
2
+ Name: yt-dlp-utils
3
+ Version: 0.0.6
4
+ Summary: Utilities for programmatic use of yt-dlp.
5
+ License-Expression: MIT
6
+ License-File: LICENSE.txt
7
+ Keywords: command line,yt-dlp
8
+ Author: Andrew Udvare
9
+ Author-email: audvare@gmail.com
10
+ Requires-Python: >=3.10,<4.0
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Typing :: Typed
20
+ Requires-Dist: requests (>=2.32.5,<3.0.0)
21
+ Requires-Dist: typing-extensions (>=4.15.0,<5.0.0)
22
+ Requires-Dist: yt-dlp[default] (>=2025.10.22,<2026.0.0)
23
+ Project-URL: Documentation, https://yt-dlp-utils.readthedocs.org
24
+ Project-URL: Homepage, https://tatsh.github.io/yt-dlp-utils/
25
+ Project-URL: Issues, https://github.com/Tatsh/yt-dlp-utils/issues
26
+ Project-URL: Repository, https://github.com/Tatsh/yt-dlp-utils
27
+ Description-Content-Type: text/markdown
28
+
29
+ # yt-dlp-utils
30
+
31
+ [![Python versions](https://img.shields.io/pypi/pyversions/yt-dlp-utils.svg?color=blue&logo=python&logoColor=white)](https://www.python.org/)
32
+ [![PyPI - Version](https://img.shields.io/pypi/v/yt-dlp-utils)](https://pypi.org/project/yt-dlp-utils/)
33
+ [![GitHub tag (with filter)](https://img.shields.io/github/v/tag/Tatsh/yt-dlp-utils)](https://github.com/Tatsh/yt-dlp-utils/tags)
34
+ [![License](https://img.shields.io/github/license/Tatsh/yt-dlp-utils)](https://github.com/Tatsh/yt-dlp-utils/blob/master/LICENSE.txt)
35
+ [![GitHub commits since latest release (by SemVer including pre-releases)](https://img.shields.io/github/commits-since/Tatsh/yt-dlp-utils/v0.0.6/master)](https://github.com/Tatsh/yt-dlp-utils/compare/v0.0.6...master)
36
+ [![CodeQL](https://github.com/Tatsh/yt-dlp-utils/actions/workflows/codeql.yml/badge.svg)](https://github.com/Tatsh/yt-dlp-utils/actions/workflows/codeql.yml)
37
+ [![QA](https://github.com/Tatsh/yt-dlp-utils/actions/workflows/qa.yml/badge.svg)](https://github.com/Tatsh/yt-dlp-utils/actions/workflows/qa.yml)
38
+ [![Tests](https://github.com/Tatsh/yt-dlp-utils/actions/workflows/tests.yml/badge.svg)](https://github.com/Tatsh/yt-dlp-utils/actions/workflows/tests.yml)
39
+ [![Coverage Status](https://coveralls.io/repos/github/Tatsh/yt-dlp-utils/badge.svg?branch=master)](https://coveralls.io/github/Tatsh/yt-dlp-utils?branch=master)
40
+ [![Documentation Status](https://readthedocs.org/projects/yt-dlp-utils/badge/?version=latest)](https://yt-dlp-utils.readthedocs.org/?badge=latest)
41
+ [![mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
42
+ [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
43
+ [![pydocstyle](https://img.shields.io/badge/pydocstyle-enabled-AD4CD3)](http://www.pydocstyle.org/en/stable/)
44
+ [![pytest](https://img.shields.io/badge/pytest-zz?logo=Pytest&labelColor=black&color=black)](https://docs.pytest.org/en/stable/)
45
+ [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
46
+ [![Downloads](https://static.pepy.tech/badge/yt-dlp-utils/month)](https://pepy.tech/project/yt-dlp-utils)
47
+ [![Stargazers](https://img.shields.io/github/stars/Tatsh/yt-dlp-utils?logo=github&style=flat)](https://github.com/Tatsh/yt-dlp-utils/stargazers)
48
+
49
+ [![@Tatsh](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fpublic.api.bsky.app%2Fxrpc%2Fapp.bsky.actor.getProfile%2F%3Factor%3Ddid%3Aplc%3Auq42idtvuccnmtl57nsucz72%26query%3D%24.followersCount%26style%3Dsocial%26logo%3Dbluesky%26label%3DFollow%2520%40Tatsh&query=%24.followersCount&style=social&logo=bluesky&label=Follow%20%40Tatsh)](https://bsky.app/profile/Tatsh.bsky.social)
50
+ [![Mastodon Follow](https://img.shields.io/mastodon/follow/109370961877277568?domain=hostux.social&style=social)](https://hostux.social/@Tatsh)
51
+
52
+ Utilities for programmatic use of yt-dlp.
53
+
54
+ ## Installation
55
+
56
+ ### Poetry
57
+
58
+ ```shell
59
+ poetry add yt-dlp-utils
60
+ ```
61
+
62
+ ### Pip
63
+
64
+ ```shell
65
+ pip install yt-dlp-utils
66
+ ```
67
+
@@ -0,0 +1,8 @@
1
+ yt_dlp_utils/__init__.py,sha256=1ka6IliJRGC5LJlOjB2yp5Ri0Wfblqd-wm8XMcgKig4,250
2
+ yt_dlp_utils/constants.py,sha256=SNzzxIkyJRsVopyLQJtYGlZT_znvYbPFOg6klw5KDdI,746
3
+ yt_dlp_utils/lib.py,sha256=jCAMpLXruT-BZY5fGB5kf_n-fLxJyRxNfyemBWATtfU,5292
4
+ yt_dlp_utils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ yt_dlp_utils-0.0.6.dist-info/METADATA,sha256=qvHZgYZ2e0XGxomyHAp2YYWzAhnWxhQz2veKUsw6Xgc,4213
6
+ yt_dlp_utils-0.0.6.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
7
+ yt_dlp_utils-0.0.6.dist-info/licenses/LICENSE.txt,sha256=1z3v176A2bAtCVZXlb8HZXUf3HpQIjW2GanQd8PaVK4,1087
8
+ yt_dlp_utils-0.0.6.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 2.2.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,18 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 yt-dlp-utils authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6
+ associated documentation files (the "Software"), to deal in the Software without restriction,
7
+ including without limitation the rights to use, copy, modify, merge, publish, distribute,
8
+ sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all copies or
12
+ substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15
+ NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18
+ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.