Skip to main content

This is a package that wraps around the YouTube API, providing an easier user interface and experience

Project description


youtube_wrapper_py Documentation

Overview

youtube_wrapper_py is a Python package designed to simplify interactions with the YouTube Data API and YouTube Transcript API. It provides an easy-to-use interface for retrieving video details, searching for videos, listing channel videos, and fetching video transcripts with support for multiple languages and translations.


Installation

Prerequisites

Ensure you have Python 3.9 or later installed on your system.

Installing youtube_wrapper_py

You can install the youtube_wrapper_py package using pip. Below are instructions for both Windows and Linux/MacOS.

For Windows:

  1. Open Command Prompt:

    • Press Win + R, type cmd, and press Enter.
  2. Install the Package:

    pip install youtube_wrapper_py
    

For Linux/MacOS:

  1. Open Terminal:

    • On Linux, press Ctrl + Alt + T.
    • On MacOS, press Cmd + Space, type Terminal, and press Enter.
  2. Install the Package:

    pip install youtube_wrapper_py
    

Verifying the Installation

To verify that the installation was successful, try importing the package in a Python script or an interactive shell:

import youtube_wrapper_py

print("Package installed successfully!")

Configuration

Setting Up Your API Key

To use the YouTube Data API, you need to provide an API key. The recommended approach is to store the API key in a .env file for security and convenience.

  1. Create a .env File:

    • In the root directory of your project, create a .env file.

    Windows Command Prompt:

    echo YOUTUBE_API_KEY=your_youtube_api_key_here > .env
    

    Linux/MacOS Terminal:

    touch .env
    echo "YOUTUBE_API_KEY=your_youtube_api_key_here" > .env
    
  2. Load the Environment Variables:

    • Ensure your Python code loads the environment variables using dotenv.
    from dotenv import load_dotenv
    import os
    
    load_dotenv()
    
    api_key = os.getenv("YOUTUBE_API_KEY")
    

Usage

1. Initialize the API

First, import and initialize the YouTubeAPI class:

from youtube_wrapper_py import YouTubeAPI

# Initialize with the API key loaded from the environment
yt_api = YouTubeAPI()

# Or initialize with the API key directly
# yt_api = YouTubeAPI(api_key="your_api_key")

2. YouTube Data API

The YouTubeDataAPI class provides methods to retrieve video details, search for videos, and list videos on a specific channel.

a. Get Video Details

Retrieve details of a specific YouTube video using its video ID:

video_id = "dQw4w9WgXcQ"
video_details = yt_api.data_api.get_video_details(video_id)
print(video_details)

Output Example:

{
    "kind": "youtube#videoListResponse",
    "etag": "2mr-gCW8pi9wUp1GKVla7aLt0Rw",
    "items": [
        {
            "kind": "youtube#video",
            "etag": "Pgk1h32cn-vy5870xIxxxdmZ0js",
            "id": "dQw4w9WgXcQ",
            "snippet": {
                "publishedAt": "2009-10-25T06:57:33Z",
                "channelId": "UCuAXFkgsw1L7xaCfnd5JJOw",
                "title": "Rick Astley - Never Gonna Give You Up (Official Music Video)",
                "description": "The official music video for Rick Astley's 'Never Gonna Give You Up'...",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/dQw4w9WgXcQ/default.jpg",
                        "width": 120,
                        "height": 90
                    },
                    "medium": {
                        "url": "https://i.ytimg.com/vi/dQw4w9WgXcQ/mqdefault.jpg",
                        "width": 320,
                        "height": 180
                    },
                    "high": {
                        "url": "https://i.ytimg.com/vi/dQw4w9WgXcQ/hqdefault.jpg",
                        "width": 480,
                        "height": 360
                    }
                },
                "channelTitle": "RickAstleyVEVO",
                "tags": ["Rick Astley", "Never Gonna Give You Up", "music"],
                "categoryId": "10",
                "liveBroadcastContent": "none",
                "localized": {
                    "title": "Rick Astley - Never Gonna Give You Up (Official Music Video)",
                    "description": "The official music video for Rick Astley's 'Never Gonna Give You Up'..."
                }
            },
            "statistics": {
                "viewCount": "1000000000",
                "likeCount": "10000000",
                "dislikeCount": "1000",
                "favoriteCount": "0",
                "commentCount": "500000"
            }
        }
    ]
}

b. Search for Videos

Search for videos based on a query string:

query = "Python programming"
search_results = yt_api.data_api.search_videos(query, max_results=5)
print(search_results)

Output Example:

{
    "kind": "youtube#searchListResponse",
    "etag": "9fRhN9nXalRjdZ8g8T8eOihPIgQ",
    "items": [
        {
            "kind": "youtube#searchResult",
            "etag": "yFUPmGzD8EAdTg9M7kTXWAmTWuY",
            "id": {
                "kind": "youtube#video",
                "videoId": "rfscVS0vtbw"
            },
            "snippet": {
                "publishedAt": "2021-01-01T00:00:00Z",
                "channelId": "UC8butISFwT-Wl7EV0hUK0BQ",
                "title": "Learn Python Programming - Full Course for Beginners",
                "description": "This is a full Python course for beginners...",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/rfscVS0vtbw/default.jpg",
                        "width": 120,
                        "height": 90
                    }
                },
                "channelTitle": "Programming with Mosh",
                "liveBroadcastContent": "none",
                "publishTime": "2021-01-01T00:00:00Z"
            }
        },
        {
            "kind": "youtube#searchResult",
            "etag": "ZrTrNmzR3AWPH4rx9rAYTtSTZmU",
            "id": {
                "kind": "youtube#video",
                "videoId": "YYXdXT2l-Gg"
            },
            "snippet": {
                "publishedAt": "2021-02-01T00:00:00Z",
                "channelId": "UCsD4u8w0w6o-h0KlWmZf2gA",
                "title": "Python Tutorial for Beginners [Full Course]",
                "description": "A comprehensive tutorial on Python for beginners...",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/YYXdXT2l-Gg/default.jpg",
                        "width": 120,
                        "height": 90
                    }
                },
                "channelTitle": "Tech with Tim",
                "liveBroadcastContent": "none",
                "publishTime": "2021-02-01T00:00:00Z"
            }
        }
    ]
}

c. List Channel Videos

List the most recent videos from a specific YouTube channel:

channel_id = "UCW5YeuERMmlnqo4oq8vwUpg"
channel_videos = yt_api.data_api.list_channel_videos(channel_id, max_results=5)
print(channel_videos)

Output Example:

{
    "kind": "youtube#playlistItemListResponse",
    "etag": "R13F_8A_FmO_y1QKxPZUkHgkNHc",
    "items": [
        {
            "kind": "youtube#playlistItem",
            "etag": "DgpT4Y1J2ksc8WcFsU1NkpEXtPE",
            "id": "UExkN0o0YmY5NjRmb3dBSzQ0Qk1oVFBkZ1EwQjQ0Q0FqUjlET3U0Q3JjZzU0",
            "snippet": {
                "publishedAt": "2024-07-15T12:34:56

Z",
                "channelId": "UCW5YeuERMmlnqo4oq8vwUpg",
                "title": "Python Tutorial",
                "description": "A new Python tutorial video...",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/exampleVideoId1/default.jpg",
                        "width": 120,
                        "height": 90
                    }
                },
                "channelTitle": "Your Channel Name",
                "playlistId": "PLkIo9xzG6bYpNYY_N2f8P0i7_pzdk02nC",
                "position": 0,
                "resourceId": {
                    "kind": "youtube#video",
                    "videoId": "exampleVideoId1"
                }
            }
        },
        {
            "kind": "youtube#playlistItem",
            "etag": "F9zzJ-QTyXUMRtE6NFrkIGnxX5M",
            "id": "UExkN0o0YmY5NjRmb3dBSzQ0Qk1oVFBkZ1EwQjQ0Q0FqUjlET3U0Q3JjZzU1",
            "snippet": {
                "publishedAt": "2024-07-14T12:34:56Z",
                "channelId": "UCW5YeuERMmlnqo4oq8vwUpg",
                "title": "Advanced Python Techniques",
                "description": "An advanced Python tutorial video...",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/exampleVideoId2/default.jpg",
                        "width": 120,
                        "height": 90
                    }
                },
                "channelTitle": "Your Channel Name",
                "playlistId": "PLkIo9xzG6bYpNYY_N2f8P0i7_pzdk02nC",
                "position": 1,
                "resourceId": {
                    "kind": "youtube#video",
                    "videoId": "exampleVideoId2"
                }
            }
        }
    ]
}

3. YouTube Transcript API

The YouTubeTranscriptAPI class provides methods to retrieve video transcripts and handle translations.

a. Get Video Transcript

Retrieve the transcript of a YouTube video, with support for multiple languages:

video_id = "dQw4w9WgXcQ"
transcript = yt_api.transcript_api.get_transcript_with_translation(video_id, languages=['en', 'es'])
print(transcript)

Output Example:

[
    {"text": "We're no strangers to love", "start": 0.0, "duration": 4.0},
    {"text": "You know the rules and so do I", "start": 4.0, "duration": 4.2},
    {"text": "A full commitment's what I'm thinking of", "start": 8.2, "duration": 4.0}
]

If a transcript is not available, you will receive an appropriate error message.

4. Unified API Interface

You can also use the unified interface provided by YouTubeAPI to get both video details and its transcript in one call:

video_id = "dQw4w9WgXcQ"
result = yt_api.get_video_info_with_transcript(video_id, languages=['en'])
print(result)

This function returns a dictionary containing both the video details and its transcript:

Output Example:

{
    "video_details": {
        "title": "Never Gonna Give You Up",
        "description": "Official Rick Astley YouTube Channel",
        "viewCount": "1000000000",
        "likeCount": "10000000",
        "dislikeCount": "1000",
        "commentCount": "500000"
    },
    "transcript": [
        {"text": "We're no strangers to love", "start": 0.0, "duration": 4.0},
        {"text": "You know the rules and so do I", "start": 4.0, "duration": 4.2}
    ]
}

Error Handling

Transcripts Unavailable

If a transcript is not available for a video, the get_transcript_with_translation method will return an error message string.

Example:

result = yt_api.transcript_api.get_transcript_with_translation(video_id)
if isinstance(result, str):
    print(f"Error: {result}")
else:
    print(result)

Best Practices

  • Environment Variables: Store sensitive information like API keys in environment variables and access them securely in your code.
  • Error Handling: Implement proper error handling to manage cases where data is unavailable or API calls fail.
  • Documentation: Keep your code well-documented to ensure ease of use and maintenance.

License

This package is licensed under the MIT License. See the LICENSE file for more information.


Contributing

If you wish to contribute to this project, please fork the repository and submit a pull request. Make sure to update tests as appropriate.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

youtube_wrapper_py-0.0.2.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

youtube_wrapper_py-0.0.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file youtube_wrapper_py-0.0.2.tar.gz.

File metadata

  • Download URL: youtube_wrapper_py-0.0.2.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for youtube_wrapper_py-0.0.2.tar.gz
Algorithm Hash digest
SHA256 39bc1fde61f49c6892a384733f4787531a26d96e8a74783e8777547b5cbacf74
MD5 f0106ecdbf0f238a23d18d45aeaa84e4
BLAKE2b-256 01bce5f6bdd334a90662b6a71019588b4efa5b97fd40fd6eb1f5ac90b6c8eced

See more details on using hashes here.

File details

Details for the file youtube_wrapper_py-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for youtube_wrapper_py-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b20d8630218ead8b7f20a9dce22c00e270d0a87952e5bc83caca607dc593b65c
MD5 44c39ef2b5e6d1f7eac2c7fa6e2c480e
BLAKE2b-256 0ea93a2b1b2937eac23eab9e6e173cf8900a2c0ecad728cfc7539515ddee7261

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page