Created
September 6, 2014 09:41
-
-
Save bdunagan/683ab2a8b901e60c0b47 to your computer and use it in GitHub Desktop.
Ruby code for YouTube API v3 integration from http://bdunagan.com/2014/09/06/ruby-integration-with-youtube-api-v3
This file contains hidden or 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
# List of all uploaded videos and playlists on a YouTube Channel | |
# Note that YouTube API v3 requires a key. Create a browser API key with a referer at https://console.developers.google.com. | |
# | |
# Here are the steps using "curl" that matches the Ruby code below: | |
# | |
# Get channel information. | |
# curl --referer "YOUR_REFERER" "https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails,statistics,status&maxResults=50&forUsername=YOUR_USERNAME&key=YOUR_KEY" | |
# Find "Uploads" playlist ID at items => contentDetails => relatedPlaylists => uploads. | |
# Find Channel ID at items => id. | |
# | |
# Get playlist items for "Uploads". | |
# curl --referer "YOUR_REFERER" "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=YOUR_PLAYLIST_ID&key=YOUR_KEY" | |
# curl --referer "YOUR_REFERER" "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&pageToken=PAGE_TOKEN&playlistId=YOUR_PLAYLIST_ID&key=YOUR_KEY" | |
# | |
# Get playlists for channel. | |
# curl --referer "YOUR_REFERER" "https://www.googleapis.com/youtube/v3/playlists?part=snippet&maxResults=50&channelId=YOUR_CHANNEL_ID&key=YOUR_KEY" | |
require 'net/http' | |
require 'json' | |
def youtube_v3_api(path) | |
# Create URI. | |
uri = URI.parse("https://www.googleapis.com/youtube/v3/#{path}") | |
# Create API request. | |
request = Net::HTTP.new(uri.host, uri.port) | |
request.use_ssl = true | |
request.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
# Execute request. | |
response = request.get(uri.request_uri, {"Referer" => "#{@referer}"}) | |
# Parse response if any. | |
return (response.code == "200") ? JSON.parse(response.body) : nil | |
end | |
# Setup environment. | |
@api_key = "YOUR_API_KEY" | |
@referer = "YOUR_REFERER" | |
@username = "YOUR_USERNAME" | |
# Get "Uploads" playlist ID for username | |
results = youtube_v3_api("channels?part=snippet,contentDetails,statistics,status&maxResults=50&forUsername=#{@username}&key=#{@api_key}") | |
jj results | |
@uploads_playlist_id = results["items"][0]["contentDetails"]["relatedPlaylists"]["uploads"] | |
# Get "Uploads" playlist items. | |
results = youtube_v3_api("playlistItems?part=snippet&maxResults=50&playlistId=#{@uploads_playlist_id}&key=#{@api_key}") | |
playlist_items = results["items"] | |
while results["nextPageToken"] | |
# Add additional items if results are paged. | |
results = youtube_v3_api("playlistItems?part=snippet&maxResults=50&pageToken=#{results["nextPageToken"]}&playlistId=#{@uploads_playlist_id}&key=#{@api_key}") | |
playlist_items += results["items"] | |
end | |
# Get videos with statistics for playlist items. | |
videos = [] | |
playlist_items.each_slice(10) do |items| | |
video_ids = items.collect {|item| item["snippet"]["resourceId"]["videoId"] } | |
results = youtube_v3_api("videos?part=snippet,statistics,contentDetails&id=#{video_ids.join(",")}&key=#{@api_key}") | |
videos += results["items"] | |
end | |
jj videos | |
# Get playlists for username. | |
results = youtube_v3_api("channels?part=snippet,contentDetails,statistics,status&maxResults=50&forUsername=#{@username}&key=#{@api_key}") | |
@channel_id = results["items"][0]["id"] | |
results = youtube_v3_api("playlists?part=snippet&maxResults=50&channelId=#{@channel_id}&key=#{@api_key}") | |
playlists = results["items"] | |
jj playlists |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My bad. I hadn't wrapped the url in quotes! Perfect. Many thanks.