Last active
April 9, 2020 06:19
-
-
Save dharmatech/419db595be8acc4ba40d5dfb9c9e7601 to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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