Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

PowerShell script is unable to retrieve all the videos listed for a YouTube channel [duplicate]

$
0
0

I came up with the following PowerShell script, which when I enter the Channel_ID and my API Key, it generates a CSV file with approx. 500 of the 1500 videos on the channel.

# Define the YouTube API endpoint$apiEndpoint = "https://www.googleapis.com/youtube/v3/"# Prompt the user to enter a channel ID$channelId = Read-Host "Enter the YouTube channel ID"# Set the max results per page and initial page token$maxResults = 500$pageToken = ""# Create an array to store the video information$videoArray = @()# Loop through the pages of video results until there are no more pagesdo {    # Define the API request URL for the current page    $requestUrl = $apiEndpoint +"search?part=snippet&channelId=$channelId&type=video&maxResults=$maxResults&pageToken=$pageToken&key=<YOUR_API_KEY>"    # Send the API request for the current page    $apiResponse = Invoke-RestMethod -Uri $requestUrl    # Loop through the videos on the current page and add them to the array    foreach ($video in $apiResponse.items) {        $date = [DateTime]::Parse($video.snippet.publishedAt).ToString("yyyy-MM-dd")        $videoInfo = [PSCustomObject]@{            Date = $date            Title = $video.snippet.title            URL = "https://www.youtube.com/watch?v=$($video.id.videoId)"        }        $videoArray += $videoInfo    }    # Update the page token for the next page (if there is one)    $pageToken = $apiResponse.nextPageToken} until (-not $pageToken)# Sort the video information by date in descending order (newest first)$sortedVideos = $videoArray | Sort-Object -Property Date -Descending# Save the video information to a CSV file on the desktop$desktopPath = [Environment]::GetFolderPath("Desktop")$csvPath = "$desktopPath\YouTube Videos.csv"$sortedVideos | Export-Csv $csvPath -NoTypeInformation -Encoding UTF8# Get the total number of videos for the channel$channelUrl = $apiEndpoint +"channels?part=statistics&id=$channelId&key=<YOUR_API_KEY>"$channelInfo = Invoke-RestMethod -Uri $channelUrl$totalVideos = $channelInfo.items.statistics.videoCount# Check the number of videos in the CSV file$csvVideos = $sortedVideos.Count# Display the number of videos in the CSV file and the total number of videos for the channelWrite-Host "Saved $csvVideos videos to $csvPath"Write-Host "YouTube channel $channelId has a total of $totalVideos videos"

I switched the MaxResults from 50 to 200, and 500, and I still get the same result. The script is supposed to iterate through until all videos have been accounted for.


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>