class TopVideo
Attributes
channels[RW]
limit[RW]
mutex_channel[RW]
mutex_video[RW]
videos[RW]
Public Class Methods
new(key, idChannels, limit = 3)
click to toggle source
# File lib/top_video.rb, line 94 def initialize(key, idChannels, limit = 3) @videos = Array.new @channels = Array.new @limit = limit @mutex_channel = Mutex.new @mutex_video = Mutex.new threads = Array.new i = 0 Yt.configuration.api_key = key idChannels.each do |id| channel = Yt::Channel.new id: id channelInfo = ChannelInfo.new(channel) @channels << channelInfo threads << Thread.new(channel, i) {|chan, id| global(chan, id)} i = i + 1 end threads.each do |thread| thread.join end @videos.sort_by! {|x| x.video.channel_title} end
Public Instance Methods
averageView(video, id)
click to toggle source
calcul the averageView by day of the channel
# File lib/top_video.rb, line 73 def averageView(video, id) diff = numberOfDay(video.published_at) @mutex_video.synchronize do @channels.at(id).total = @channels.at(id).total + (video.view_count / diff) end end
dump(tab)
click to toggle source
dump function to show the array of video
# File lib/top_video.rb, line 43 def dump(tab) puts "dump :" tab.each do |x| puts x.video.title puts x.percent end puts "end of dump" end
global(channel, id)
click to toggle source
global function which retries the average and select the video
# File lib/top_video.rb, line 81 def global(channel, id) threads = Array.new videos = channel.videos videos.each do |video| threads << Thread.new(video) {|x| averageView(x, id)} end threads.each do |thread| thread.join end @channels[id].average = @channels[id].total / channel.video_count selectVideo(videos, @channels[id].average) end
numberOfDay(date)
click to toggle source
Return the difference in days between now and date where the video has been published
# File lib/top_video.rb, line 35 def numberOfDay(date) t = Time.now diff = t - date diff = diff / (60 * 60 * 24) return diff end
selectVideo(videos, average)
click to toggle source
Select Video in channel result from date and average view by day
# File lib/top_video.rb, line 53 def selectVideo(videos, average) tmp = Array.new videos.each do |video| diff = numberOfDay(video.published_at) averageVideo = video.view_count / diff if ((diff < 30) && (averageVideo > average)) increase = ((averageVideo - average) / average) * 100 videoList = VideoList.new(video, increase) tmp << videoList end end tmp.sort_by! {|item| item.percent}.reverse! @mutex_channel.synchronize do tmp.first(@limit).each do |x| @videos << x end end end