ytapi-sdk 0.1.0__tar.gz
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.
- ytapi_sdk-0.1.0/LICENSE +21 -0
- ytapi_sdk-0.1.0/PKG-INFO +145 -0
- ytapi_sdk-0.1.0/README.md +125 -0
- ytapi_sdk-0.1.0/pyproject.toml +32 -0
- ytapi_sdk-0.1.0/setup.cfg +4 -0
- ytapi_sdk-0.1.0/src/ytapi/__init__.py +22 -0
- ytapi_sdk-0.1.0/src/ytapi/client.py +421 -0
- ytapi_sdk-0.1.0/src/ytapi/errors.py +50 -0
- ytapi_sdk-0.1.0/src/ytapi/models.py +263 -0
- ytapi_sdk-0.1.0/src/ytapi/py.typed +1 -0
- ytapi_sdk-0.1.0/src/ytapi_sdk.egg-info/PKG-INFO +145 -0
- ytapi_sdk-0.1.0/src/ytapi_sdk.egg-info/SOURCES.txt +13 -0
- ytapi_sdk-0.1.0/src/ytapi_sdk.egg-info/dependency_links.txt +1 -0
- ytapi_sdk-0.1.0/src/ytapi_sdk.egg-info/top_level.txt +1 -0
- ytapi_sdk-0.1.0/tests/test_client.py +519 -0
ytapi_sdk-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 YTAPI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
ytapi_sdk-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ytapi-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for YTAPI: YouTube transcripts, video details, search, channels and playlists.
|
|
5
|
+
Author: YTAPI
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Documentation, https://docs.ytapi.dev
|
|
8
|
+
Project-URL: Homepage, https://ytapi.dev
|
|
9
|
+
Project-URL: Repository, https://github.com/ytapi/ytapi-python
|
|
10
|
+
Project-URL: Issues, https://github.com/ytapi/ytapi-python/issues
|
|
11
|
+
Keywords: youtube,transcript,captions,subtitles,youtube-transcript,ytapi
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Classifier: Topic :: Multimedia :: Video
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# YTAPI Python client
|
|
22
|
+
|
|
23
|
+
Python client for [YTAPI](https://ytapi.dev?utm_source=github): YouTube transcripts, video details, search, channels and playlists over one HTTP API. It works from servers and cloud functions, where fetching YouTube directly tends to get blocked.
|
|
24
|
+
|
|
25
|
+
- No dependencies beyond the standard library. Python 3.10+.
|
|
26
|
+
- Typed responses (`TypedDict`), typed errors, automatic retries and pagination.
|
|
27
|
+
- Reference: [docs.ytapi.dev](https://docs.ytapi.dev).
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install ytapi-sdk
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The package is `ytapi-sdk` on PyPI, and you import it as `ytapi`.
|
|
36
|
+
|
|
37
|
+
## Quickstart
|
|
38
|
+
|
|
39
|
+
Get a key at [ytapi.dev](https://ytapi.dev/app/api-keys?utm_source=github). New accounts get 200 free credits, and no card is needed.
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from ytapi import YTAPI
|
|
43
|
+
|
|
44
|
+
api = YTAPI() # reads YTAPI_API_KEY (or YTAPI_KEY) from the environment
|
|
45
|
+
transcript = api.get_transcript("dQw4w9WgXcQ")
|
|
46
|
+
for segment in transcript["segments"][:3]:
|
|
47
|
+
print(segment["start"], segment["text"])
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
By default you get the captions in the video's own language, as timed segments. A successful request uses 1 credit, and errors are free.
|
|
51
|
+
|
|
52
|
+
## Examples
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
# Other formats. markdown, text, srt and vtt come back as a string.
|
|
56
|
+
srt = api.get_transcript("dQw4w9WgXcQ", format="srt")
|
|
57
|
+
spanish = api.get_transcript("dQw4w9WgXcQ", format="text", languages=["es", "*"])
|
|
58
|
+
words = api.get_transcript("dQw4w9WgXcQ", format="word_timestamps", word_level=True)
|
|
59
|
+
|
|
60
|
+
# Free: title, length, channel and the caption languages a video has.
|
|
61
|
+
basic = api.get_basic_info("dQw4w9WgXcQ")
|
|
62
|
+
# 1 credit: description, counts, chapters and more.
|
|
63
|
+
info = api.get_video_info("dQw4w9WgXcQ")
|
|
64
|
+
|
|
65
|
+
# Channels take an @handle, a channel ID (UC...) or a URL.
|
|
66
|
+
channel = api.get_channel("@3blue1brown")
|
|
67
|
+
for video in api.iter_channel_videos("@3blue1brown", sort_by="popular"):
|
|
68
|
+
print(video["video_id"], video["title"])
|
|
69
|
+
|
|
70
|
+
# Playlists, page by page or as an iterator.
|
|
71
|
+
for video in api.iter_playlist_videos("PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi"):
|
|
72
|
+
print(video["video_id"], video["length_text"])
|
|
73
|
+
|
|
74
|
+
# Search: type is video, channel, playlist, shorts or movie.
|
|
75
|
+
page = api.search("rust async", type="video", upload_date="month", limit=10)
|
|
76
|
+
for hit in page["items"]:
|
|
77
|
+
print(hit["title"])
|
|
78
|
+
|
|
79
|
+
# Search suggestions are free.
|
|
80
|
+
print(api.get_suggestions("nextjs")["suggestions"])
|
|
81
|
+
|
|
82
|
+
# Batch: up to 100 transcript or basic_info tasks per job.
|
|
83
|
+
job = api.create_batch(
|
|
84
|
+
[
|
|
85
|
+
{"id": "a", "type": "transcript", "video_id": "dQw4w9WgXcQ", "format": "text"},
|
|
86
|
+
{"id": "b", "type": "basic_info", "video_id": "jNQXAC9IVRw"},
|
|
87
|
+
]
|
|
88
|
+
)
|
|
89
|
+
done = api.poll_batch(job["id"], timeout=120)
|
|
90
|
+
print(done["successful"], done["credits_deducted"])
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
The iterators (`iter_playlist_videos`, `iter_channel_videos`, `iter_channel_playlists`, `iter_search`) follow `next_cursor` for you. Each page is a request and uses a credit. In a batch, each successful task uses 1 credit and failed tasks are free.
|
|
94
|
+
|
|
95
|
+
## Errors
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from ytapi import InsufficientCreditsError, NotFoundError, RateLimitedError, YTAPIError
|
|
99
|
+
|
|
100
|
+
try:
|
|
101
|
+
api.get_transcript("xxxxxxxxxxx")
|
|
102
|
+
except NotFoundError as exc:
|
|
103
|
+
print(exc.code) # captions_disabled, language_not_found, video_unavailable, ...
|
|
104
|
+
except RateLimitedError as exc:
|
|
105
|
+
print(exc.code, exc.retry_after) # rate_limited or daily_limit_exceeded
|
|
106
|
+
except InsufficientCreditsError:
|
|
107
|
+
print("Out of credits: https://ytapi.dev/#pricing")
|
|
108
|
+
except YTAPIError as exc:
|
|
109
|
+
print(exc.status, exc.code, exc) # status 0 means a network error
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
| Status | Exception |
|
|
113
|
+
| --- | --- |
|
|
114
|
+
| 401 | `AuthError` |
|
|
115
|
+
| 402 | `InsufficientCreditsError` |
|
|
116
|
+
| 404 | `NotFoundError` |
|
|
117
|
+
| 429 | `RateLimitedError` |
|
|
118
|
+
| 5xx | `ServerError` |
|
|
119
|
+
| network error | `YTAPIError` with `status` 0 |
|
|
120
|
+
| other | `YTAPIError` |
|
|
121
|
+
|
|
122
|
+
## Retries
|
|
123
|
+
|
|
124
|
+
The client retries a 429, a 5xx or a network error up to `max_retries` times (default 2). It backs off from 0.5 seconds, doubling up to 8, and waits longer when the server sends `Retry-After`. A few cases are not retried:
|
|
125
|
+
|
|
126
|
+
- **A 429 that asks for a long wait.** The cutoff is `max_retry_wait`, default 60 seconds. This includes a free account's daily limit (`daily_limit_exceeded`), which lasts until 00:00 UTC. The client raises these right away instead of hanging your program.
|
|
127
|
+
- **Creating a batch after a 5xx or a network error.** The job may already exist, so a retry could start a second one.
|
|
128
|
+
|
|
129
|
+
Use `YTAPI(max_retries=0)` to turn retries off.
|
|
130
|
+
|
|
131
|
+
## Releases
|
|
132
|
+
|
|
133
|
+
Each GitHub release publishes the matching version to [PyPI](https://pypi.org/project/ytapi-sdk/) through trusted publishing. The release tag must match the version in `pyproject.toml`, such as `v0.1.0`.
|
|
134
|
+
|
|
135
|
+
## Tests
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
PYTHONPATH=src python3 -m unittest discover -s tests
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The tests mock HTTP and need no network or API key.
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
MIT
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# YTAPI Python client
|
|
2
|
+
|
|
3
|
+
Python client for [YTAPI](https://ytapi.dev?utm_source=github): YouTube transcripts, video details, search, channels and playlists over one HTTP API. It works from servers and cloud functions, where fetching YouTube directly tends to get blocked.
|
|
4
|
+
|
|
5
|
+
- No dependencies beyond the standard library. Python 3.10+.
|
|
6
|
+
- Typed responses (`TypedDict`), typed errors, automatic retries and pagination.
|
|
7
|
+
- Reference: [docs.ytapi.dev](https://docs.ytapi.dev).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install ytapi-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The package is `ytapi-sdk` on PyPI, and you import it as `ytapi`.
|
|
16
|
+
|
|
17
|
+
## Quickstart
|
|
18
|
+
|
|
19
|
+
Get a key at [ytapi.dev](https://ytapi.dev/app/api-keys?utm_source=github). New accounts get 200 free credits, and no card is needed.
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from ytapi import YTAPI
|
|
23
|
+
|
|
24
|
+
api = YTAPI() # reads YTAPI_API_KEY (or YTAPI_KEY) from the environment
|
|
25
|
+
transcript = api.get_transcript("dQw4w9WgXcQ")
|
|
26
|
+
for segment in transcript["segments"][:3]:
|
|
27
|
+
print(segment["start"], segment["text"])
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
By default you get the captions in the video's own language, as timed segments. A successful request uses 1 credit, and errors are free.
|
|
31
|
+
|
|
32
|
+
## Examples
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
# Other formats. markdown, text, srt and vtt come back as a string.
|
|
36
|
+
srt = api.get_transcript("dQw4w9WgXcQ", format="srt")
|
|
37
|
+
spanish = api.get_transcript("dQw4w9WgXcQ", format="text", languages=["es", "*"])
|
|
38
|
+
words = api.get_transcript("dQw4w9WgXcQ", format="word_timestamps", word_level=True)
|
|
39
|
+
|
|
40
|
+
# Free: title, length, channel and the caption languages a video has.
|
|
41
|
+
basic = api.get_basic_info("dQw4w9WgXcQ")
|
|
42
|
+
# 1 credit: description, counts, chapters and more.
|
|
43
|
+
info = api.get_video_info("dQw4w9WgXcQ")
|
|
44
|
+
|
|
45
|
+
# Channels take an @handle, a channel ID (UC...) or a URL.
|
|
46
|
+
channel = api.get_channel("@3blue1brown")
|
|
47
|
+
for video in api.iter_channel_videos("@3blue1brown", sort_by="popular"):
|
|
48
|
+
print(video["video_id"], video["title"])
|
|
49
|
+
|
|
50
|
+
# Playlists, page by page or as an iterator.
|
|
51
|
+
for video in api.iter_playlist_videos("PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi"):
|
|
52
|
+
print(video["video_id"], video["length_text"])
|
|
53
|
+
|
|
54
|
+
# Search: type is video, channel, playlist, shorts or movie.
|
|
55
|
+
page = api.search("rust async", type="video", upload_date="month", limit=10)
|
|
56
|
+
for hit in page["items"]:
|
|
57
|
+
print(hit["title"])
|
|
58
|
+
|
|
59
|
+
# Search suggestions are free.
|
|
60
|
+
print(api.get_suggestions("nextjs")["suggestions"])
|
|
61
|
+
|
|
62
|
+
# Batch: up to 100 transcript or basic_info tasks per job.
|
|
63
|
+
job = api.create_batch(
|
|
64
|
+
[
|
|
65
|
+
{"id": "a", "type": "transcript", "video_id": "dQw4w9WgXcQ", "format": "text"},
|
|
66
|
+
{"id": "b", "type": "basic_info", "video_id": "jNQXAC9IVRw"},
|
|
67
|
+
]
|
|
68
|
+
)
|
|
69
|
+
done = api.poll_batch(job["id"], timeout=120)
|
|
70
|
+
print(done["successful"], done["credits_deducted"])
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The iterators (`iter_playlist_videos`, `iter_channel_videos`, `iter_channel_playlists`, `iter_search`) follow `next_cursor` for you. Each page is a request and uses a credit. In a batch, each successful task uses 1 credit and failed tasks are free.
|
|
74
|
+
|
|
75
|
+
## Errors
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from ytapi import InsufficientCreditsError, NotFoundError, RateLimitedError, YTAPIError
|
|
79
|
+
|
|
80
|
+
try:
|
|
81
|
+
api.get_transcript("xxxxxxxxxxx")
|
|
82
|
+
except NotFoundError as exc:
|
|
83
|
+
print(exc.code) # captions_disabled, language_not_found, video_unavailable, ...
|
|
84
|
+
except RateLimitedError as exc:
|
|
85
|
+
print(exc.code, exc.retry_after) # rate_limited or daily_limit_exceeded
|
|
86
|
+
except InsufficientCreditsError:
|
|
87
|
+
print("Out of credits: https://ytapi.dev/#pricing")
|
|
88
|
+
except YTAPIError as exc:
|
|
89
|
+
print(exc.status, exc.code, exc) # status 0 means a network error
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
| Status | Exception |
|
|
93
|
+
| --- | --- |
|
|
94
|
+
| 401 | `AuthError` |
|
|
95
|
+
| 402 | `InsufficientCreditsError` |
|
|
96
|
+
| 404 | `NotFoundError` |
|
|
97
|
+
| 429 | `RateLimitedError` |
|
|
98
|
+
| 5xx | `ServerError` |
|
|
99
|
+
| network error | `YTAPIError` with `status` 0 |
|
|
100
|
+
| other | `YTAPIError` |
|
|
101
|
+
|
|
102
|
+
## Retries
|
|
103
|
+
|
|
104
|
+
The client retries a 429, a 5xx or a network error up to `max_retries` times (default 2). It backs off from 0.5 seconds, doubling up to 8, and waits longer when the server sends `Retry-After`. A few cases are not retried:
|
|
105
|
+
|
|
106
|
+
- **A 429 that asks for a long wait.** The cutoff is `max_retry_wait`, default 60 seconds. This includes a free account's daily limit (`daily_limit_exceeded`), which lasts until 00:00 UTC. The client raises these right away instead of hanging your program.
|
|
107
|
+
- **Creating a batch after a 5xx or a network error.** The job may already exist, so a retry could start a second one.
|
|
108
|
+
|
|
109
|
+
Use `YTAPI(max_retries=0)` to turn retries off.
|
|
110
|
+
|
|
111
|
+
## Releases
|
|
112
|
+
|
|
113
|
+
Each GitHub release publishes the matching version to [PyPI](https://pypi.org/project/ytapi-sdk/) through trusted publishing. The release tag must match the version in `pyproject.toml`, such as `v0.1.0`.
|
|
114
|
+
|
|
115
|
+
## Tests
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
PYTHONPATH=src python3 -m unittest discover -s tests
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The tests mock HTTP and need no network or API key.
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "ytapi-sdk"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Python client for YTAPI: YouTube transcripts, video details, search, channels and playlists."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
authors = [{ name = "YTAPI" }]
|
|
9
|
+
dependencies = []
|
|
10
|
+
keywords = ["youtube", "transcript", "captions", "subtitles", "youtube-transcript", "ytapi"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Programming Language :: Python :: 3",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
"Typing :: Typed",
|
|
15
|
+
"Topic :: Multimedia :: Video",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Documentation = "https://docs.ytapi.dev"
|
|
20
|
+
Homepage = "https://ytapi.dev"
|
|
21
|
+
Repository = "https://github.com/ytapi/ytapi-python"
|
|
22
|
+
Issues = "https://github.com/ytapi/ytapi-python/issues"
|
|
23
|
+
|
|
24
|
+
[build-system]
|
|
25
|
+
requires = ["setuptools>=77"]
|
|
26
|
+
build-backend = "setuptools.build_meta"
|
|
27
|
+
|
|
28
|
+
[tool.setuptools.packages.find]
|
|
29
|
+
where = ["src"]
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.package-data]
|
|
32
|
+
ytapi = ["py.typed"]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Python client for the YTAPI HTTP API. https://docs.ytapi.dev"""
|
|
2
|
+
|
|
3
|
+
from .client import YTAPI
|
|
4
|
+
from .errors import (
|
|
5
|
+
AuthError,
|
|
6
|
+
InsufficientCreditsError,
|
|
7
|
+
NotFoundError,
|
|
8
|
+
RateLimitedError,
|
|
9
|
+
ServerError,
|
|
10
|
+
YTAPIError,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"AuthError",
|
|
15
|
+
"InsufficientCreditsError",
|
|
16
|
+
"NotFoundError",
|
|
17
|
+
"RateLimitedError",
|
|
18
|
+
"ServerError",
|
|
19
|
+
"YTAPI",
|
|
20
|
+
"YTAPIError",
|
|
21
|
+
]
|
|
22
|
+
__version__ = "0.1.0"
|