A Python library for searching YouTube shorts and retrieving metadata
Project description
YouTube Short Search
A Python library for searching YouTube shorts and retrieving metadata including title, views, and URL.
Features
- Search for YouTube shorts using query strings
- Retrieve video metadata (title, views, URL, channel, etc.)
- Support for both YouTube Data API v3 and web scraping fallback
- Type hints and comprehensive error handling
- Easy to use and integrate
Installation
pip install youtube-short-search
Quick Start
Basic Usage
from youtube_short_search import YouTubeShortSearcher
# Initialize searcher (with API key - recommended)
searcher = YouTubeShortSearcher(api_key="YOUR_YOUTUBE_API_KEY")
# Search for shorts
results = searcher.search_shorts("funny cats", max_results=5)
for video in results:
print(f"Title: {video['title']}")
print(f"Views: {video['views']}")
print(f"URL: {video['url']}")
print(f"Channel: {video['channel']}")
print("-" * 50)
Without API Key (Web Scraping)
from youtube_short_search import YouTubeShortSearcher
# Initialize searcher without API key (less reliable)
searcher = YouTubeShortSearcher()
try:
results = searcher.search_shorts("cooking tips", max_results=3)
for video in results:
print(f"{video['title']} - {video['views']}")
except Exception as e:
print(f"Error: {e}")
Get Video Details
from youtube_short_search import YouTubeShortSearcher
searcher = YouTubeShortSearcher(api_key="YOUR_YOUTUBE_API_KEY")
# Get details for a specific video
video_url = "https://www.youtube.com/watch?v=VIDEO_ID"
details = searcher.get_video_details(video_url)
print(f"Title: {details['title']}")
print(f"Views: {details['views']}")
print(f"Likes: {details['likes']}")
API Key Setup
To use the YouTube Data API v3 (recommended for reliability):
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the YouTube Data API v3
- Create credentials (API Key)
- Use the API key in your code
searcher = YouTubeShortSearcher(api_key="YOUR_API_KEY_HERE")
Response Format
Each video result contains the following information:
{
'title': 'Video Title',
'views': '1.2M views',
'url': 'https://www.youtube.com/watch?v=VIDEO_ID',
'channel': 'Channel Name',
'published': '2023-01-01T00:00:00Z',
'description': 'Video description...'
}
Error Handling
The library provides custom exceptions for different error cases:
from youtube_short_search import (
YouTubeShortSearcher,
YouTubeSearchError,
InvalidSearchQueryError,
APIKeyError,
NetworkError,
NoResultsFoundError
)
searcher = YouTubeShortSearcher(api_key="YOUR_API_KEY")
try:
results = searcher.search_shorts("python tutorials")
except InvalidSearchQueryError:
print("Search query is invalid")
except APIKeyError:
print("API key is invalid or missing")
except NetworkError:
print("Network connection error")
except NoResultsFoundError:
print("No shorts found for this query")
except YouTubeSearchError as e:
print(f"General error: {e}")
Requirements
- Python 3.7+
- requests library
Development
Setting up for development
git clone https://github.com/yourusername/youtube-short-search.git
cd youtube-short-search
# Install in development mode
pip install -e .[dev]
# Run tests
pytest
# Format code
black .
# Type checking
mypy youtube_short_search/
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=youtube_short_search
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Disclaimer
This library is for educational and research purposes. Please respect YouTube's Terms of Service and rate limits when using this library. The web scraping functionality is provided as a fallback but using the official YouTube Data API is strongly recommended.
Changelog
0.1.0 (2024-01-01)
- Initial release
- Support for YouTube shorts search
- API and web scraping methods
- Comprehensive error handling
- Type hints support
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file youtube_short_search_package-0.1.0.tar.gz.
File metadata
- Download URL: youtube_short_search_package-0.1.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f40aec57f1aa8ce4f33f109b6559ab82cf2bbb58240e6902860fb96a65491ae
|
|
| MD5 |
8d57786c1b516e02c377ff20b2384952
|
|
| BLAKE2b-256 |
3553174997bdadb0fe34ad18208ca977f0a1d0671c88576e0b03225901af98ed
|
File details
Details for the file youtube_short_search_package-0.1.0-py3-none-any.whl.
File metadata
- Download URL: youtube_short_search_package-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3dda0a9b248dda04d8069c7c7db651626ddc4a07124c66007096e3fd6360ad90
|
|
| MD5 |
c02deb3b7fdee4c8a5c56809cc489d9b
|
|
| BLAKE2b-256 |
9e8d926804790b5dce2cddac70fceac9399ac4e35fc01a6ce7400439de1a94eb
|