Created
December 2, 2023 23:05
-
-
Save umihico/3d650875b7e80ea3ee78a31e2ce5d1d5 to your computer and use it in GitHub Desktop.
Youtubeのdescriptionの先頭に「提供」と「(引数)」がついている動画を検索する
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
require "google/apis/youtube_v3" | |
# rails r 'puts YoutubeHelper.search' | |
# Youtubeのdescriptionの先頭に「提供」と「(引数)」がついている動画を検索する | |
module YoutubeHelper | |
class << self | |
def service | |
youtube_service = Google::Apis::YoutubeV3::YouTubeService.new | |
youtube_service.key = ENV.fetch("GOOGLE_API_KEY", nil) | |
youtube_service | |
end | |
def search(word) | |
next_page_token = nil | |
items = [] | |
10.times do |_i| | |
response = service.list_searches("snippet", | |
q: "提供 #{word}", | |
type: "video", | |
max_results: 50, | |
page_token: next_page_token) | |
response.items.each do |item| | |
description = item.snippet.description | |
items << item if description[0..29].include?("提供") && description[0..29].include?(word) | |
end | |
next_page_token = response.next_page_token | |
break unless next_page_token | |
end | |
items.each do |item| | |
puts "https://www.youtube.com/watch?v=#{item.id.video_id} #{item.snippet.description[0..29]}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment