Usage
Installation
You can install YTubeInsight using pip:
pip install ytubeinsight
Basic Usage
Here’s a simple example of how to use YTubeInsight:
from ytubeinsight import analyze_channel
# Replace with your YouTube Data API key
API_KEY = 'YOUR_API_KEY_HERE'
# Analyze a channel by URL
channel_url = 'https://www.youtube.com/channel/UC_x5XG1OV2P6uZZ5FSM9Ttw'
result = analyze_channel(channel_url, API_KEY)
print(f"Videos published in the last year: {result['video_count']}")
# Print details of the first video
if result['video_data']:
video = result['video_data'][0]
print(f"Latest video:")
print(f"Title: {video['title']}")
print(f"Published on: {video['published_at']}")
print(f"URL: {video['url']}")
# Analyze a channel by ID
channel_id = 'UCJFp8uSYCjXOMnkUyb3CQ3Q'
result = analyze_channel(channel_id, API_KEY, is_channel_id=True)
print(f"Videos published in the last year: {result['video_count']}")
API Reference
- ytubeinsight.analyze_channel(channel_input, api_key, is_channel_id=False)[source]
Analyze a YouTube channel and return video data for the past year.
- Parameters:
channel_input (str) – Either a full channel URL or channel ID
api_key (str) – YouTube Data API key
is_channel_id (bool) – Indicates if channel_input is a channel ID
- Returns:
Contains video count and list of video data
- Return type:
dict
- Raises:
YTubeInsightError – If any error occurs during the analysis process
Exceptions
Getting a YouTube Data API Key
To use YTubeInsight, you need a YouTube Data API key. Here’s how to get one:
Go to the Google Developers Console.
Create a new project or select an existing one.
Enable the YouTube Data API v3 for your project.
Create credentials (API key) for your project.
Use this API key in your YTubeInsight calls.
Remember to keep your API key secret and never share it publicly.
Error Handling
YTubeInsight uses custom exceptions to handle errors. Here’s an example of how to handle errors:
from ytubeinsight import analyze_channel, YTubeInsightError
try:
result = analyze_channel('https://www.youtube.com/channel/UC_x5XG1OV2P6uZZ5FSM9Ttw', API_KEY)
except YTubeInsightError as e:
print(f"An error occurred: {str(e)}")