Use Pydantic models to work with the YouTube API.
Project description
youtube-pydantic-models
A Python library that contains the most popular YouTube models based on Pydantic. If you are working with the YouTube API, youtube-pydantic-models can help you validate, manipulate, and retrieve data.
The YouTube API returns data using camel case, but you can choose to return data using camel case or snake case. With the parameter by_alias=True
, data is returned using camel case.
When using the model, every parameter is accessed using snake case.
Features
- Validate YouTube API responses using Pydantic models
- Convert data between camel case and snake case
- Easy-to-use interface for common YouTube resources
Requirements
- Python 3.7+
- A YouTube Data API Key
Installation
You can install the library using pip:
pip install youtube-pydantic-models
Example usage
Channel Model
import requests
from youtube_pydantic_models import YoutubeChannelResource
params = {
'id': "UC_x5XG1OV2P6uZZ5FSM9Ttw",
'key': "YOUR_API_KEY",
'part': "snippet, contentDetails"
}
response = requests.get(
"https://www.googleapis.com/youtube/v3/channels",
params=params
).json()
channel = YoutubeChannelResource(**response)
print(channel.id)
print(channel.snippet.custom_url)
channel_dict = channel.model_dump(
by_alias=True,
exclude_none=True
)
Playlist Model
import requests
from youtube_pydantic_models import YoutubePlaylistResource
params = {
'channelId': "UC_x5XG1OV2P6uZZ5FSM9Ttw",
'key': "YOUR_API_KEY",
'part': "snippet, player"
}
response = requests.get(
"https://www.googleapis.com/youtube/v3/playlists",
params=params
).json()
playlist = YoutubePlaylistResource(**response)
print(playlist.snippet.channel_title)
print(playlist.player.embed_html)
playlist_dict = playlist.model_dump(
by_alias=True,
exclude_none=True
)
Video Model
import requests
from youtube_pydantic_models import YoutubeVideoResource
params = {
'id': "PJm8WNajZtw",
'key': "YOUR_API_KEY",
'part': "statistics"
}
response = requests.get(
"https://www.googleapis.com/youtube/v3/videos",
params=params
).json()
video = YoutubeVideoResource(**response)
print(video.id)
print(video.statistics.view_count)
video_dict = video.model_dump(
by_alias=True,
exclude_none=True
)
Search Model
import requests
from youtube_pydantic_models import YoutubeSearchResource
params = {
'channelId': "UC_x5XG1OV2P6uZZ5FSM9Ttw",
'key': "YOUR_API_KEY",
'part': "id, snippet"
}
response = requests.get(
"https://www.googleapis.com/youtube/v3/search",
params=params
).json()
resource = YoutubeSearchResource(**response)
print(resource.id.kind)
print(resource.snippet.thumbnails.default.url)
resource_dict = resource.model_dump(
by_alias=True,
exclude_none=True
)
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for youtube_pydantic_models-0.1.12.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94b0ad35839e979f8cbbc60f77ad5d16e77a1aa68d3c0de6529e154a42e4c9e7 |
|
MD5 | a2705019d8952f4eb420cb5d57c330ee |
|
BLAKE2b-256 | 13954be2da4e755f73c002f34e61856992388a4bad65fac3b10e7b3f76c99314 |
Hashes for youtube_pydantic_models-0.1.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c383f93aaff99793cf2fa1faeb920daaf83770fae065b706c4d41efa0b97e0d1 |
|
MD5 | 38459de8b655ba5ec96a52c053b10e49 |
|
BLAKE2b-256 | b75bbd50e8b9c2055697489310fa9f52d7df9a25ee72769a0339d04a91079267 |