video/app/controllers/videos_controller.rb

77 lines
2.9 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
page_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/channels?id=#{channel_id}&key=AIzaSyCTFdyLgb7GJfrr1JOI3gvVslZFw7td2zQ&part=contentDetails")
result = Net::HTTP.get(uri) # => String
result = JSON.parse(result)
vc = []
if result["pageInfo"]["totalResults"] > 0
if params["nppid"].present?
u = "https://www.googleapis.com/youtube/v3/playlistItems?playlistId=#{result["items"].first["contentDetails"]["relatedPlaylists"]["uploads"]}&key=AIzaSyCTFdyLgb7GJfrr1JOI3gvVslZFw7td2zQ&part=snippet&maxResults=#{OrbitHelper.page_data_count}&pageToken=#{params["nppid"]}"
else
u = "https://www.googleapis.com/youtube/v3/playlistItems?playlistId=#{result["items"].first["contentDetails"]["relatedPlaylists"]["uploads"]}&key=AIzaSyCTFdyLgb7GJfrr1JOI3gvVslZFw7td2zQ&part=snippet&maxResults=#{OrbitHelper.page_data_count}"
end
url = URI(u)
channel = Net::HTTP.get(url) # => String
channel = JSON.parse(channel)
if !channel["items"].blank?
next_page_id = channel["nextPageToken"]
prev_page_id = channel["prevPageToken"]
channel["items"].each do |item|
video = item["snippet"]
if video["resourceId"].present? && video["resourceId"]["kind"] == "youtube#video"
videoid = (video["resourceId"]["videoId"].present? ? video["resourceId"]["videoId"] : "#") rescue "#"
vc << {
"title" => (item['snippet']['title'] rescue ""),
"description" => (video["description"] rescue ""),
"publish_date" => (DateTime.parse(video["publishedAt"]).strftime("%Y-%m-%d %H:%M") rescue ""),
"img1_src" => (video["thumbnails"]["default"]["url"] rescue ""),
"img2_src" => (video["thumbnails"]["medium"]["url"] rescue ""),
"img3_src" => (video["thumbnails"]["standard"]["url"] rescue ""),
# "video_url" => page_url + "?method=showvideo&id=#{videoid}"
"video_url" => "https://www.youtube.com/embed/#{videoid}"
}
end
end
end
end
{
"video_channels" => vc ,
"extras" => {
"next_page_id" => next_page_id,
"prev_page_id" => prev_page_id,
"th_title" => t('title'),
"th_description" => t('video.description'),
"th_publish_date" => t('video.publish_date')
}
}
end
end