Created
September 14, 2023 08:48
-
-
Save jmahoney/82add734570b1b1cb43261cbb0cc456a to your computer and use it in GitHub Desktop.
create and add files to a youtube playlist
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
require 'google/apis/youtube_v3' | |
require 'googleauth' | |
require 'googleauth/stores/file_token_store' | |
require 'fileutils' | |
def valid_youtube_id?(id) | |
id =~ /^[a-zA-Z0-9_-]{11}$/ | |
end | |
def authorize | |
FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH)) | |
client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH) | |
token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH) | |
authorizer = Google::Auth::UserAuthorizer.new( | |
client_id, SCOPES, token_store) | |
user_id = 'default' | |
credentials = authorizer.get_credentials(user_id) | |
if credentials.nil? | |
url = authorizer.get_authorization_url(base_url: REDIRECT_URI) | |
puts "Open the following URL in the browser and enter the " + | |
"resulting code after authorization" | |
puts url | |
code = gets | |
credentials = authorizer.get_and_store_credentials_from_code( | |
user_id: user_id, code: code, base_url: REDIRECT_URI) | |
end | |
credentials | |
end | |
# Path to client secret file downloaded from the Google Cloud Console | |
CLIENT_SECRETS_PATH = 'client_secret.json' | |
# Authorization scopes required by the YouTube API | |
SCOPES = [Google::Apis::YoutubeV3::AUTH_YOUTUBE_READONLY, Google::Apis::YoutubeV3::AUTH_YOUTUBE_FORCE_SSL, Google::Apis::YoutubeV3::AUTH_YOUTUBE] | |
# Path to file containing authorization credentials | |
CREDENTIALS_PATH = File.join(Dir.home, '.credentials', 'youtube-ruby-snippet-store.yaml') | |
REDIRECT_URI = 'http://localhost' | |
APPLICATION_NAME = 'Kicking Television' | |
youtube = Google::Apis::YoutubeV3::YouTubeService.new | |
youtube.client_options.application_name = APPLICATION_NAME | |
youtube.authorization = authorize | |
PLAYLIST_ID = '' | |
video_ids = ARGV.reject { |id| !valid_youtube_id?(id) } | |
if video_ids.empty? | |
puts "No valid video IDs specified" | |
exit | |
end | |
max_results = 50 | |
playlist_items = youtube.list_playlist_items('snippet', playlist_id: PLAYLIST_ID, max_results: max_results) | |
while playlist_items.next_page_token do | |
next_page_items = youtube.list_playlist_items('snippet', playlist_id: PLAYLIST_ID, max_results: max_results, page_token: playlist_items.next_page_token) | |
playlist_items.items.concat(next_page_items.items) | |
end | |
existing_video_ids = playlist_items.items.map { |item| item.snippet.resource_id.video_id } | |
video_ids.each do |video_id| | |
next if existing_video_ids.include?(video_id) | |
playlist_item = Google::Apis::YoutubeV3::PlaylistItem.new( | |
snippet: { | |
playlist_id: PLAYLIST_ID, | |
resource_id: { | |
kind: 'youtube#video', | |
video_id: video_id | |
} | |
} | |
) | |
youtube.insert_playlist_item('snippet', playlist_item) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment