video/app/controllers/videos_controller.rb

79 lines
3.2 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 = []
nextPageToken = result['nextPageToken'] rescue ""
while !nextPageToken.nil?
if !result.nil? && !result['items'].nil?
url = (result['items'].first["id"]["videoId"].present? ? "https://www.youtube.com/embed/#{result['items'].first["id"]["videoId"]}" : "#") rescue "#"
vc << {
"title" => (result['items'].first['snippet']['title'] rescue ""),
"description" => (result['items'].first['snippet']['description'] rescue ""),
"publish_date" => (result['items'].first['snippet']['publishedAt'] rescue ""),
"img1_src" => (result['items'].first['snippet']['thumbnails']['default']['url'] rescue ""),
"img2_src" => (result['items'].first['snippet']['thumbnails']['medium']['url'] rescue ""),
"img3_src" => (result['items'].first['snippet']['thumbnails']['high']['url'] rescue ""),
"video_url" => url,
"channel_title" => (result['items'].first['snippet']['channelTitle'] rescue "")
}
end
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
url = (result['items'].first["id"]["videoId"].present? ? "https://www.youtube.com/embed/#{result['items'].first["id"]["videoId"]}" : "#") rescue "#"
vc << {
"title" => (result['items'].first['snippet']['title'] rescue ""),
"description" => (result['items'].first['snippet']['description'] rescue ""),
"publish_date" => (result['items'].first['snippet']['publishedAt'] rescue ""),
"img1_src" => (result['items'].first['snippet']['thumbnails']['default']['url'] rescue ""),
"img2_src" => (result['items'].first['snippet']['thumbnails']['medium']['url'] rescue ""),
"img3_src" => (result['items'].first['snippet']['thumbnails']['high']['url'] rescue ""),
"video_url" => url,
"channel_title" => (result['items'].first['snippet']['channelTitle'] rescue "")
}
{
"video_channels" => vc ,
"extras" => {
"th_title" => t('title'),
"th_description" => t('video.description'),
"th_publish_date" => t('video.publish_date')
}
}
end
end