Class: TopVideo
- Inherits:
-
Object
- Object
- TopVideo
- Defined in:
- lib/top_video.rb
Instance Attribute Summary collapse
-
#channels ⇒ Object
Returns the value of attribute channels.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#mutex_channel ⇒ Object
Returns the value of attribute mutex_channel.
-
#mutex_video ⇒ Object
Returns the value of attribute mutex_video.
-
#videos ⇒ Object
Returns the value of attribute videos.
Instance Method Summary collapse
-
#averageView(video, id) ⇒ Object
calcul the averageView by day of the channel.
-
#dump(tab) ⇒ Object
dump function to show the array of video.
-
#global(channel, id) ⇒ Object
global function which retries the average and select the video.
-
#initialize(key, idChannels, limit = 3) ⇒ TopVideo
constructor
A new instance of TopVideo.
-
#numberOfDay(date) ⇒ Object
Return the difference in days between now and date where the video has been published.
-
#selectVideo(videos, average) ⇒ Object
Select Video in channel result from date and average view by day.
Constructor Details
#initialize(key, idChannels, limit = 3) ⇒ TopVideo
Returns a new instance of TopVideo.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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 |
Instance Attribute Details
#channels ⇒ Object
Returns the value of attribute channels.
29 30 31 |
# File 'lib/top_video.rb', line 29 def channels @channels end |
#limit ⇒ Object
Returns the value of attribute limit.
30 31 32 |
# File 'lib/top_video.rb', line 30 def limit @limit end |
#mutex_channel ⇒ Object
Returns the value of attribute mutex_channel.
31 32 33 |
# File 'lib/top_video.rb', line 31 def mutex_channel @mutex_channel end |
#mutex_video ⇒ Object
Returns the value of attribute mutex_video.
32 33 34 |
# File 'lib/top_video.rb', line 32 def mutex_video @mutex_video end |
#videos ⇒ Object
Returns the value of attribute videos.
28 29 30 |
# File 'lib/top_video.rb', line 28 def videos @videos end |
Instance Method Details
#averageView(video, id) ⇒ Object
calcul the averageView by day of the channel
73 74 75 76 77 78 |
# 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) ⇒ Object
dump function to show the array of video
43 44 45 46 47 48 49 50 |
# 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) ⇒ Object
global function which retries the average and select the video
81 82 83 84 85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
Return the difference in days between now and date where the video has been published
35 36 37 38 39 40 |
# 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) ⇒ Object
Select Video in channel result from date and average view by day
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# 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 |