yourub 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25ceaed05b4774168968449eddaf924867751a36
4
- data.tar.gz: f74dff2b944640f9d26de9835409d98278dc07fe
3
+ metadata.gz: b0017033502058c653e334a10a41c6185fdd90ef
4
+ data.tar.gz: 5c54a0c481cd6a1c2dc9807c1576b123efde2e1b
5
5
  SHA512:
6
- metadata.gz: d3f14d4add074c03a25f4cd489b0688b22a7ea0b88c647391669db9f53036505fe67e541919c9726ee505f628bbf02b300a0b97e43c21d6af043eb552a2ba20e
7
- data.tar.gz: 07bae00f9cc0df24035d9a2487e75ad21f614194e088d52e3bb5c888603c423d69be5963b5cfb65c24dd2c10e48c830dcb783d358652da3f17493521396d96ff
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 finds the most recent videos from the Youtube API for the given nation, category and number of views. It's updated to the version 3 of the youtube api
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
@@ -22,15 +22,15 @@ module Yourub
22
22
  end
23
23
 
24
24
  def validate_count_filter
25
- count_number_invalid = (@filter['views'] =~ VIEWS_COUNT_NUMBER).nil?
26
- operator_invalid = (@filter['views'] =~ ARITHMETIC_OPERATOR).nil?
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['views'].match(ARITHMETIC_OPERATOR).to_s
33
- number_to_compare = @filter['views'].match(VIEWS_COUNT_NUMBER).to_s.to_i
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
@@ -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
- retrieve_categories
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
- :videoCategoryId => '10'
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,snippet',
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
- @videos.push({
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"
@@ -1,3 +1,3 @@
1
1
  module Yourub
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -17,20 +17,20 @@ describe Yourub::CountFilter do
17
17
  }
18
18
 
19
19
  it "accept the video if satisfy the condition" do
20
- count_filter = {'views' => ">= 100"}
21
- res = Yourub::CountFilter.accept?(video, count_filter)
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
- count_filter = nil
27
- res = Yourub::CountFilter.accept?(video, count_filter)
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
- count_filter = {'views' => "< 10"}
33
- res = Yourub::CountFilter.accept?(video, count_filter)
32
+ filter = {views: "<= 100"}
33
+ res = Yourub::CountFilter.accept?(video, filter)
34
34
  expect(res).to be_false
35
35
  end
36
36
 
@@ -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
- result = Yourub::Search.new()
8
- expect(result.categories).to be_a_kind_of(Array)
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
- result = Yourub::Search.new(nation: "US")
13
- expect(result.categories).to be_a_kind_of(Array)
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 = {'views' => ">= 100"}
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yourub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Davide Prati