Python script that uses the YouTube API to retrieve the titles, descriptions, and URLs of all the videos in a given YouTube channel

Here is an example python script that uses the YouTube API to retrieve the titles, descriptions, and URLs of all the videos in a given YouTube channel:

import requests
import json
import csv

def get_channel_info(channel_id, api_key):
    # Make an API call to retrieve channel information
    url = "https://www.googleapis.com/youtube/v3/search?key={}&channelId={}&part=snippet&order=date&maxResults=50".format(api_key, channel_id)
    response = requests.get(url)
    data = json.loads(response.text)
    
    # Extract the relevant information from the API response
    videos = []
    for item in data["items"]:
        if item['id']['kind'] == 'youtube#video':
            video = {}
            video["title"] = item["snippet"]["title"]
            video["description"] = item["snippet"]["description"]
            video["url"] = "https://www.youtube.com/watch?v={}".format(item['id']['videoId'])
            videos.append(video)
    
    return videos

if __name__ == "__main__":
    # Replace channel_id and api_key with your own values
    channel_id = "YOUR_CHANNEL_ID"
    api_key = "YOUR_API_KEY"
    videos = get_channel_info(channel_id, api_key)

    # Write the results to a CSV file
    with open("videos.csv", "w", newline="") as csvfile:
        fieldnames = ["title", "description", "url"]
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        for video in videos:
            writer.writerow(video)

Remember to replace YOUR_CHANNEL_ID and YOUR_API_KEY with your own YouTube channel ID and API key, respectively.

🚀 **Support Our DevOps Blog with Your Amazon Shopping!** 🚀 Love shopping on Amazon? Now you can fuel your shopping spree *and* support our blog at no extra cost! Just use our link for your next purchase: **[Shop on Amazon & Support Us!] Browse Stuff on Amazon Every click helps us keep sharing the DevOps love. Happy shopping!

Leave a Comment