video/app/controllers/videos_controller.rb

79 lines
3.0 KiB
Ruby

class VideosController < ApplicationController
def index
video_channels = VideoChannel.all
vcs = video_channels.collect do |vc|
{
"link_to_show" => OrbitHelper.url_to_show(vc.to_param),
"title" => vc.title,
"owner" => vc.owner,
"channel_link" => vc.channel_link,
"description" => vc.description
}
end
{
"video_channels" => vcs,
"extras" => {
"th_title" => t('title'),
"th_owner" => t('video.owner'),
"th_description" => t('video.description')
}
}
end
def show
params = OrbitHelper.params
video_channel = VideoChannel.find_by_param(params[:uid])
page = Page.where(:page_id => params[:page_id]).first rescue nil
url = page.nil? ? "#" : "/#{I18n.locale.to_s}#{page.url}"
channel_id = video_channel.channel_link.split("/")[-1]
uri = URI('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + channel_id + '&key=AIzaSyCTFdyLgb7GJfrr1JOI3gvVslZFw7td2zQ')
result = Net::HTTP.get(uri) # => String
result = JSON.parse(result)
vc = []
video_channels.each do |video_channel|
channel_id = video_channel.channel_link.split("/")[-1]
uri = URI('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + channel_id + '&key=AIzaSyCTFdyLgb7GJfrr1JOI3gvVslZFw7td2zQ')
result = Net::HTTP.get(uri) # => String
result = JSON.parse(result)
nextPageToken = result['nextPageToken'] rescue ""
while !nextPageToken.nil?
vc << {
"title" => result['items'].first['snippet']['title'],
"description" => result['items'].first['snippet']['description'] ,
"publish_date" => result['items'].first['snippet']['publishedAt'],
"img1_src" => result['items'].first['snippet']['thumbnails']['default']['url'],
"img2_src" => result['items'].first['snippet']['thumbnails']['medium']['url'],
"img3_src" => result['items'].first['snippet']['thumbnails']['high']['url'] ,
"channel_title" => result['items'].first['snippet']['channelTitle']
}
uri = URI('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + channel_id + '&key=AIzaSyCTFdyLgb7GJfrr1JOI3gvVslZFw7td2zQ&pageToken=' + nextPageToken)
result = Net::HTTP.get(uri) # => String
result = JSON.parse(result)
nextPageToken = result['nextPageToken'] rescue ""
end
vc << {
"title" => result['items'].first['snippet']['title'],
"description" => result['items'].first['snippet']['description'] ,
"publish_date" => result['items'].first['snippet']['publishedAt'],
"img1_src" => result['items'].first['snippet']['thumbnails']['default']['url'],
"img2_src" => result['items'].first['snippet']['thumbnails']['medium']['url'],
"img3_src" => result['items'].first['snippet']['thumbnails']['high']['url'] ,
"channel_title" => result['items'].first['snippet']['channelTitle']
}
end
{
"video_channels" => vc ,
"extras" => {
"th_title" => t('title'),
"th_description" => t('video.description'),
"th_publish_date" => t('video.publish_date')
}
}
end
end