yourub 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/yourub/count_filter.rb +4 -4
- data/lib/yourub/search.rb +33 -13
- data/lib/yourub/version.rb +1 -1
- data/spec/count_filter_spec.rb +6 -6
- data/spec/search_spec.rb +14 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0017033502058c653e334a10a41c6185fdd90ef
|
4
|
+
data.tar.gz: 5c54a0c481cd6a1c2dc9807c1576b123efde2e1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05c785075b9221f3c4cdad383af982d1f4ec3095a6a8023aeea0399ca5b648e29e0412d3e80fd28f2f37c5f357dcc0aed50d55102010988cbebf57c7ee54d106
|
7
|
+
data.tar.gz: 723d9580ebad77e7e7d27d9987307897aea7dda50f9695064f893f5a8ac6c06aa74523d8e23bdeaae0de1ef9f7c8306f08c4d62fb57a0de6ecb7a7813ba58c2e
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# Yourub
|
4
4
|
|
5
|
-
Yourub is a gem that
|
5
|
+
Yourub is a gem that parse the Youtube API. It is possible to look for a given nation, a category, a given video ID or searching by the of views. It's updated to the version 3 of the youtube api
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -63,6 +63,10 @@ Or all the options together
|
|
63
63
|
filter = {'views' => "<= 200"}
|
64
64
|
Yourub::Search.new(nation: "FR", category: "Comedy", max_results: 25, filter: filter)
|
65
65
|
|
66
|
+
To get all the available metatags for a single video, simply give the Youtube video id as parameter
|
67
|
+
|
68
|
+
Yourub::Search.new(video_id: "mN0Dbj-xHY0")
|
69
|
+
|
66
70
|
##TODO
|
67
71
|
|
68
72
|
1. filter by search term
|
data/lib/yourub/count_filter.rb
CHANGED
@@ -22,15 +22,15 @@ module Yourub
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def validate_count_filter
|
25
|
-
count_number_invalid = (@filter[
|
26
|
-
operator_invalid = (@filter[
|
25
|
+
count_number_invalid = (@filter[:views] =~ VIEWS_COUNT_NUMBER).nil?
|
26
|
+
operator_invalid = (@filter[:views] =~ ARITHMETIC_OPERATOR).nil?
|
27
27
|
raise ArgumentError.new("arithmetic operator not detected") if operator_invalid
|
28
28
|
raise ArgumentError.new("view count number not detected") if count_number_invalid
|
29
29
|
end
|
30
30
|
|
31
31
|
def apply_filter(video)
|
32
|
-
operator = @filter[
|
33
|
-
number_to_compare = @filter[
|
32
|
+
operator = @filter[:views].match(ARITHMETIC_OPERATOR).to_s
|
33
|
+
number_to_compare = @filter[:views].match(VIEWS_COUNT_NUMBER).to_s.to_i
|
34
34
|
number_founded = get_views_count(video)
|
35
35
|
return number_founded.send(operator, number_to_compare)
|
36
36
|
end
|
data/lib/yourub/search.rb
CHANGED
@@ -4,25 +4,33 @@ module Yourub
|
|
4
4
|
attr_reader :videos, :nations, :categories
|
5
5
|
attr_accessor :config
|
6
6
|
|
7
|
-
def initialize(nation: "US", category: 'all', max_results: 2, filter: nil)
|
7
|
+
def initialize(nation: "US", category: 'all', max_results: 2, video_id: nil, filter: nil)
|
8
8
|
local_variables.each do |k|
|
9
9
|
v = eval(k.to_s)
|
10
10
|
instance_variable_set("@#{k}", v) unless v.nil?
|
11
11
|
end
|
12
12
|
|
13
13
|
valid?
|
14
|
-
|
14
|
+
@extended_video_info = false
|
15
15
|
@categories, @videos = [], []
|
16
16
|
@options = default_search_video_options
|
17
17
|
|
18
|
-
|
19
|
-
retrieve_videos
|
18
|
+
starting_search
|
20
19
|
end
|
21
20
|
|
22
21
|
def config
|
23
22
|
Yourub::Config
|
24
23
|
end
|
25
24
|
|
25
|
+
def starting_search
|
26
|
+
unless @video_id.nil?
|
27
|
+
@extended_video_info = true
|
28
|
+
return video_info_request @video_id
|
29
|
+
end
|
30
|
+
retrieve_categories
|
31
|
+
retrieve_videos
|
32
|
+
end
|
33
|
+
|
26
34
|
NATIONS = [
|
27
35
|
'AR','AU','AT','BE','BR','CA','CL','CO','CZ','EG','FR','DE','GB','HK',
|
28
36
|
'HU','IN','IE','IL','IT','JP','JO','MY','MX','MA','NL','NZ','PE','PH',
|
@@ -92,8 +100,8 @@ module Yourub
|
|
92
100
|
:regionCode => @nation,
|
93
101
|
:type => 'video',
|
94
102
|
:order => 'date',
|
95
|
-
:safeSearch => 'none'
|
96
|
-
|
103
|
+
:safeSearch => 'none'
|
104
|
+
#:videoCategoryId => '10'
|
97
105
|
}
|
98
106
|
end
|
99
107
|
|
@@ -131,12 +139,27 @@ module Yourub
|
|
131
139
|
end
|
132
140
|
|
133
141
|
def video_params(result_video_id)
|
134
|
-
fields = 'items(snippet(title,thumbnails),statistics(viewCount))'
|
135
142
|
parameters = {
|
136
143
|
:id => result_video_id,
|
137
|
-
:part => 'statistics
|
138
|
-
:fields => URI::encode(fields)
|
144
|
+
:part => 'snippet,statistics',
|
139
145
|
}
|
146
|
+
unless @extended_video_info
|
147
|
+
fields = 'items(snippet(title,thumbnails),statistics(viewCount))'
|
148
|
+
parameters[:fields] = URI::encode(fields)
|
149
|
+
end
|
150
|
+
return parameters
|
151
|
+
end
|
152
|
+
|
153
|
+
def add_video_to_search_result(entry, video_id)
|
154
|
+
if @extended_video_info
|
155
|
+
founded_video = entry
|
156
|
+
else
|
157
|
+
founded_video = {
|
158
|
+
'title' => entry['snippet']['title'],
|
159
|
+
'url' => 'https://www.youtube.com/watch?v='<< video_id
|
160
|
+
}
|
161
|
+
end
|
162
|
+
@videos.push(founded_video)
|
140
163
|
end
|
141
164
|
|
142
165
|
def store_video(video_id, video_response)
|
@@ -144,10 +167,7 @@ module Yourub
|
|
144
167
|
result = JSON.parse(video_response.data.to_json)
|
145
168
|
entry = result['items'].first
|
146
169
|
if Yourub::CountFilter.accept?(entry, @filter)
|
147
|
-
|
148
|
-
'title' => entry['snippet']['title'],
|
149
|
-
'url' => 'https://www.youtube.com/watch?v='<< video_id
|
150
|
-
})
|
170
|
+
add_video_to_search_result(entry, video_id)
|
151
171
|
end
|
152
172
|
rescue StandardError => e
|
153
173
|
Yourub.logger.error "Error #{e} reading the video stream"
|
data/lib/yourub/version.rb
CHANGED
data/spec/count_filter_spec.rb
CHANGED
@@ -17,20 +17,20 @@ describe Yourub::CountFilter do
|
|
17
17
|
}
|
18
18
|
|
19
19
|
it "accept the video if satisfy the condition" do
|
20
|
-
|
21
|
-
res = Yourub::CountFilter.accept?(video,
|
20
|
+
filter = {views: ">= 100"}
|
21
|
+
res = Yourub::CountFilter.accept?(video, filter)
|
22
22
|
expect(res).to be_true
|
23
23
|
end
|
24
24
|
|
25
25
|
it "accept the video if filter is nil" do
|
26
|
-
|
27
|
-
res = Yourub::CountFilter.accept?(video,
|
26
|
+
filter = nil
|
27
|
+
res = Yourub::CountFilter.accept?(video, filter)
|
28
28
|
expect(res).to be_true
|
29
29
|
end
|
30
30
|
|
31
31
|
it "not accept the video if it does not satisfy the condition" do
|
32
|
-
|
33
|
-
res = Yourub::CountFilter.accept?(video,
|
32
|
+
filter = {views: "<= 100"}
|
33
|
+
res = Yourub::CountFilter.accept?(video, filter)
|
34
34
|
expect(res).to be_false
|
35
35
|
end
|
36
36
|
|
data/spec/search_spec.rb
CHANGED
@@ -3,15 +3,15 @@ require 'yourub'
|
|
3
3
|
describe Yourub::Search do
|
4
4
|
|
5
5
|
context "on initialize" do
|
6
|
-
it "retrieves all the available categories for the default country" do
|
7
|
-
|
8
|
-
|
9
|
-
end
|
6
|
+
# it "retrieves all the available categories for the default country" do
|
7
|
+
# result = Yourub::Search.new()
|
8
|
+
# expect(result.categories).to be_a_kind_of(Array)
|
9
|
+
# end
|
10
10
|
|
11
|
-
it "retrieves all the available categories for a given country" do
|
12
|
-
|
13
|
-
|
14
|
-
end
|
11
|
+
# it "retrieves all the available categories for a given country" do
|
12
|
+
# result = Yourub::Search.new(nation: "US")
|
13
|
+
# expect(result.categories).to be_a_kind_of(Array)
|
14
|
+
# end
|
15
15
|
|
16
16
|
it "return an error if the given country does not exists" do
|
17
17
|
expect{ Yourub::Search.new(nation: "MOON") }.to raise_error(ArgumentError)
|
@@ -38,10 +38,15 @@ describe Yourub::Search do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
it "retrieves videos that have more than 100 views" do
|
41
|
-
filter = {
|
41
|
+
filter = {views: ">= 100"}
|
42
42
|
result = Yourub::Search.new(nation: "US", category: "Sports", max_results: 5, filter: filter)
|
43
43
|
expect(result.videos).to be_a_kind_of(Array)
|
44
44
|
end
|
45
|
+
|
46
|
+
it "retrieves a video for the given id" do
|
47
|
+
result = Yourub::Search.new(video_id: "mN0Dbj-xHY0")
|
48
|
+
expect(result.videos.count).to eq(1)
|
49
|
+
end
|
45
50
|
end
|
46
51
|
|
47
52
|
end
|