Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell script that downloads information from all JRE episodes on YouTube. Store in a CSV file. Example of data imported into coda.io: https://coda.io/@eduardo-cavazos/jre-video-list
$api_key = (Get-Content C:\Users\dharm\Dropbox\Documents\youtube-api-key)
function get-channel-uploads ($channel_id)
{
$result = Invoke-RestMethod `
"https://www.googleapis.com/youtube/v3/channels?id=$channel_id&key=$api_key&part=contentDetails"
$result.items[0].contentDetails.relatedPlaylists.uploads
}
function get-playlist-videos ($id)
{
$items = @()
$ls = ''
while ($true)
{
if ($ls.nextPageToken -eq $null)
{
$ls = Invoke-RestMethod `
('https://www.googleapis.com/youtube/v3/playlistItems?playlistId={0}&key={1}&part=snippet&maxResults=50' -f $id, $api_key)
$items = $items + $ls.items
}
else
{
$ls = Invoke-RestMethod `
('https://www.googleapis.com/youtube/v3/playlistItems?playlistId={0}&key={1}&part=snippet&maxResults=50&pageToken={2}' -f $id, $api_key, $ls.nextPageToken)
$items = $items + $ls.items
}
Write-Host -ForegroundColor Yellow ('totalResults = {0} Count = {1}' -f $ls.pageInfo.totalResults, $items.Count)
Start-Sleep -Seconds 1
if ($items.Count -ge $ls.pageInfo.totalResults)
{
break
}
}
$items
}
$items = get-playlist-videos (get-channel-uploads 'UCzQUP1qoWDoEbmsQxvdjxgQ') # Joe Rogan
# Get youtube votes for all items (This takes a few minutes.)
$items | ForEach-Object {
$result = Invoke-RestMethod `
('https://www.googleapis.com/youtube/v3/videos?key={0}&part=statistics&id={1}' -f $api_key, $_.snippet.resourceId.videoId)
$_ | Add-Member statistics $result.items[0].statistics
Start-Sleep -Milliseconds 250
Write-Host -ForegroundColor Yellow -NoNewline '-'
}
# Write data to CSV
$items | ForEach-Object {
[PSCustomObject]@{
title = $_.snippet.title
id = $_.snippet.resourceId.videoId
publishedAt = $_.snippet.publishedAt
views = $_.statistics.viewCount
likes = $_.statistics.likeCount
dislikes = $_.statistics.dislikeCount
comments = $_.statistics.commentCount
}
} | Export-Csv -Path 'c:\temp\joe-rogan.csv'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.