RubyからストリーミングサービスAPIで指定ユーザーが配信しているか確認(Twitch Mixer YouTubeLive)
配信サービスのユーザー名から現在配信しているか否かを判定するためのRubyコード
が前提
Twitch
ここのGetting a client IDの(register your application on the Twitch developer portal.)からClientID("twichclientidxxxxxxxxxxxxxxxxx")を取得しておく。
streamer_idはTwitterのユーザー名
def get_streaming_status( streamer_id )
response = twitch_client( "https://api.twitch.tv/helix/users?login=#{streamer_id}" )
res_json = JSON.parse(response.body)
if res_json["data"].present? then
id = res_json["data"][0]["id"]
response = twitch_client( "https://api.twitch.tv/helix/streams?user_id=#{id}" )
res_json = JSON.parse(response.body)
return res_json
else
return nil
end
end
def twitch_client( url )
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
headers = { "Content-Type" => "application/json", "Client-ID" => "twichclientidxxxxxxxxxxxxxxxxx" }
response = http.get(uri, headers)
return response
end
Mixer
ここからClientID("mixerclientidxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")を取得する。
def get_streaming_status_mixer( streamer_id )
response = client_mixer( "https://mixer.com/api/v1/channels/#{streamer_id}?fields=online" )
if response.code.to_i != 200 then
return true
end
res_json = JSON.parse(response.body)
if res_json["online"].present? && res_json["online"] == true then
return true
else
return nil
end
enddef client_mixer(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
headers = { "Content-Type" => "application/json", "Client-ID" => "mixerclientidxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }
response = http.get(uri, headers)
return response
end
YouTube Live
ここからYouTubeDataAPIキー(YouTubeDataAPIkeyxxxxxxxxxxxxxxxxxxx)を取得しておく。
※YouTubeのAPIキーの1日あたりの制限はしぶっちいので5分に1回とかだと簡単に上限に達するので注意
YouTube LiveはビデオIDかチャンネルIDかで方法が違う
def get_streaming_status_youtube_videoID( video_id )
url = "https://www.googleapis.com/youtube/v3/videos?id=#{video_id}&key=YouTubeDataAPIkeyxxxxxxxxxxxxxxxxxxx&part=snippet"
return get_streaming_status_youtube( url );
end
def get_streaming_status_youtube_channelID( channel_id )
url = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&eventType=live&channelId=#{channel_id }&key=YouTubeDataAPIkeyxxxxxxxxxxxxxxxxxxx"
return get_streaming_status_youtube( url );
enddef get_streaming_status_youtube( url )
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme === "https"
headers = { "Content-Type" => "application/json" }
response = http.get(uri, headers)
if response.code.to_i != 200 then
return true
end
res_json = JSON.parse(response.body)
if res_json.blank? || res_json["items"].blank? || res_json["items"][0]["snippet"]["liveBroadcastContent"] == "none" then
return false
else
return true
end
end
以上
全然関係ないけどオススメの漫画はる
とてもオススメしたい
上記スクリプトは配信チャットで実装するのに必要でした
https://ikasekai.com/highchat/