top_video 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/top_video.rb +115 -0
  3. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bc6ea4ded815057f1c72d81f097cdea07cae67a4
4
+ data.tar.gz: 0cd9970cb2a0947dba0c53dc32ff18e6beee949b
5
+ SHA512:
6
+ metadata.gz: cca48051fb136770e68ba9a3315e912016a04c96cc4f5945453928a987ca133e7a27462c0a0275b3373f83e70bf7aff78b4c73fbd6d465ea504a0acdcd1fca7d
7
+ data.tar.gz: 4f23f1baaf9ad7a12ef37dbbabbd3e79793199589decf7a8216923a25ecba372fde9a35ba09592c765272bb1c2f293e7b83990c9d10705a3fe9b6d0fba0390c0
data/lib/top_video.rb ADDED
@@ -0,0 +1,115 @@
1
+ require 'yt'
2
+ require 'thread'
3
+ require 'benchmark'
4
+
5
+ class ChannelInfo
6
+ attr_accessor :channel
7
+ attr_accessor :average
8
+ attr_accessor :total
9
+
10
+ def initialize(channel)
11
+ @channel = channel
12
+ @average = 0
13
+ @total = 0
14
+ end
15
+ end
16
+
17
+ class VideoList
18
+ attr_accessor :video
19
+ attr_accessor :percent
20
+
21
+ def initialize(video, percent)
22
+ @video = video
23
+ @percent = percent
24
+ end
25
+ end
26
+
27
+ class TopVideo
28
+ attr_accessor :videos
29
+ attr_accessor :channels
30
+ attr_accessor :limit
31
+ attr_accessor :mutex_channel
32
+ attr_accessor :mutex_video
33
+
34
+ # Return the difference in days between now and date where the video has been published
35
+ def numberOfDay(date)
36
+ t = Time.now
37
+ diff = t - date
38
+ diff = diff / (60 * 60 * 24)
39
+ return diff
40
+ end
41
+
42
+ # dump function to show the array of video
43
+ def dump(tab)
44
+ puts "dump :"
45
+ tab.each do |x|
46
+ puts x.video.title
47
+ puts x.percent
48
+ end
49
+ puts "end of dump"
50
+ end
51
+
52
+ # Select Video in channel result from date and average view by day
53
+ def selectVideo(videos, average)
54
+ tmp = Array.new
55
+ videos.each do |video|
56
+ diff = numberOfDay(video.published_at)
57
+ averageVideo = video.view_count / diff
58
+ if ((diff < 30) && (averageVideo > average))
59
+ increase = ((averageVideo - average) / average) * 100
60
+ videoList = VideoList.new(video, increase)
61
+ tmp << videoList
62
+ end
63
+ end
64
+ tmp.sort_by! {|item| item.percent}.reverse!
65
+ @mutex_channel.synchronize do
66
+ tmp.first(@limit).each do |x|
67
+ @videos << x
68
+ end
69
+ end
70
+ end
71
+
72
+ # calcul the averageView by day of the channel
73
+ def averageView(video, id)
74
+ diff = numberOfDay(video.published_at)
75
+ @mutex_video.synchronize do
76
+ @channels.at(id).total = @channels.at(id).total + (video.view_count / diff)
77
+ end
78
+ end
79
+
80
+ # global function which retries the average and select the video
81
+ def global(channel, id)
82
+ threads = Array.new
83
+ videos = channel.videos
84
+ videos.each do |video|
85
+ threads << Thread.new(video) {|x| averageView(x, id)}
86
+ end
87
+ threads.each do |thread|
88
+ thread.join
89
+ end
90
+ @channels[id].average = @channels[id].total / channel.video_count
91
+ selectVideo(videos, @channels[id].average)
92
+ end
93
+
94
+ def initialize(key, idChannels, limit = 3)
95
+ @videos = Array.new
96
+ @channels = Array.new
97
+ @limit = limit
98
+ @mutex_channel = Mutex.new
99
+ @mutex_video = Mutex.new
100
+ threads = Array.new
101
+ i = 0
102
+ Yt.configuration.api_key = key
103
+ idChannels.each do |id|
104
+ channel = Yt::Channel.new id: id
105
+ channelInfo = ChannelInfo.new(channel)
106
+ @channels << channelInfo
107
+ threads << Thread.new(channel, i) {|chan, id| global(chan, id)}
108
+ i = i + 1
109
+ end
110
+ threads.each do |thread|
111
+ thread.join
112
+ end
113
+ @videos.sort_by! {|x| x.video.channel_title}
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: top_video
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Valentin Wallet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.25.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.25.5
27
+ description: Using Youtube API, top_video is a gem which will allow you to retrieve
28
+ rising videos from your favorite music channels with a release date less than one
29
+ month
30
+ email: valentinwallet@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/top_video.rb
36
+ homepage: https://github.com/Zanclus/top_video
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.0.14
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Retrieve rising videos from your favorite music channels
60
+ test_files: []