From c33b2157047330df085319500ef9a8305ca773a3 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 11 Oct 2016 15:09:13 +0530 Subject: [PATCH] changed the logic of whole show page and module --- app/controllers/videos_controller.rb | 66 ++++++++++++++-------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index d7a2139..74f854a 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -26,50 +26,48 @@ class VideosController < ApplicationController 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}" + 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/search?part=snippet&channelId=' + channel_id + '&key=AIzaSyCTFdyLgb7GJfrr1JOI3gvVslZFw7td2zQ') + 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 = [] - - 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 "") - } + 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 - 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" => { + "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')