ruboty-youtube 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86b5bd73c93e91658d17838358d507878b6352c5
4
- data.tar.gz: 3e005e8a0fdebc43029c9750ff365f7320421d26
3
+ metadata.gz: 01271da19abfd2f82e2559926460a955ed4045ef
4
+ data.tar.gz: cfe2bb7b9da167a6f5c728e634731ac4093cf8a5
5
5
  SHA512:
6
- metadata.gz: 59597a09dc05dd0320692926c4108cf832afe5877765cfcff8a4a9e531c3aa881314b9742a91c1ec859c07bb99e79536ae0922de5f39b0d00dff741aa475112a
7
- data.tar.gz: a0a458be42b7da047e31e1a507956be165ffacdf693f79dfc7d884d4e99608712b5d60120e771306165d42e2305bffd913810ff755d551cf3b4a9c4ebf6f048d
6
+ metadata.gz: a46ebbef24baee29293c179d72cc826610afb35fb2db1109ae3daea6d3f714d4d6c91cae12f209b6d8e1bbf5bb3f9267ccf91a1680a75285e3c90faf2fa4ef2d
7
+ data.tar.gz: 6886af5e9036d931e6488459b010cf2beed9bbcebaa07b0db78452e69974b919bba21c5b34439ae38d533bfdce8024fd32f211e9d63aa8a88798b7d6e58eba48
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Ruboty::Youtube
2
2
 
3
- Ruboty handler to search a movie from YouTube.
3
+ Ruboty handler to search from YouTube.
4
4
 
5
5
  ## Installation
6
6
 
@@ -14,5 +14,6 @@ gem 'ruboty-youtube'
14
14
 
15
15
  ```
16
16
  @ruboty yt top <keyword> - Search a movie from YouTube
17
- @ruboty yt rand <keyword> - Search a random movie from YouTube
17
+ @ruboty yt rand <keyword> - Search a movie randomly from YouTube
18
+ @ruboty yt pl <keyword> - Search a playlist from YouTube
18
19
  ```
@@ -1,13 +1,16 @@
1
1
  require 'json'
2
2
 
3
3
  require 'faraday'
4
+ require 'mechanize'
4
5
 
5
6
  module Ruboty
6
7
  module Youtube
7
8
  class Client
9
+ YOUTUBE_URL = 'https://www.youtube.com'
8
10
  YOUTUBE_API_URL = 'http://gdata.youtube.com'
9
11
 
10
12
  def initialize(query, mode)
13
+ @agent = Mechanize.new
11
14
  @conn = Faraday.new(url: YOUTUBE_API_URL) do |faraday|
12
15
  faraday.request :url_encoded
13
16
  faraday.adapter Faraday.default_adapter
@@ -18,35 +21,65 @@ module Ruboty
18
21
 
19
22
  def get
20
23
  response = @conn.get do |req|
21
- req.url '/feeds/api/videos', params
24
+ req.url api_path, params
22
25
  end.body
23
- videos = JSON.parse(response)['feed']['entry']
24
- # when @mode is :top -> videos.length is 1
25
- videos.sample['link'].first['href']
26
+ parse_response(response)
26
27
  rescue
27
28
  nil
28
29
  end
29
30
 
30
31
  private
31
32
 
32
- def params
33
+ def parse_response(response)
34
+ videos = JSON.parse(response)['feed']['entry']
35
+
33
36
  case @mode
34
- when :top
35
- {
36
- orderBy: "relevance",
37
- 'max-results' => 1,
38
- alt: 'json',
39
- q: @query
40
- }
41
- when :rand
42
- {
43
- orderBy: "relevance",
44
- 'max-results' => 15,
45
- alt: 'json',
46
- q: @query
47
- }
37
+ when :top, :rand
38
+ # when @mode is :top -> videos.length is 1
39
+ videos.sample['link'][0]['href']
40
+ when :pl
41
+ pl_url = videos.sample['link'][1]['href']
42
+ page = @agent.get(pl_url)
43
+ YOUTUBE_URL + page.link_with(href: /\/watch/).href
48
44
  end
49
45
  end
46
+
47
+ def api_path
48
+ case @mode
49
+ when :top, :rand
50
+ '/feeds/api/videos'
51
+ when :pl
52
+ '/feeds/api/playlists/snippets'
53
+ end
54
+ end
55
+
56
+ def params
57
+ default_params = {
58
+ alt: 'json',
59
+ q: @query
60
+ }
61
+
62
+ params =
63
+ case @mode
64
+ when :top
65
+ {
66
+ orderBy: "relevance",
67
+ 'max-results' => 1
68
+ }
69
+ when :rand
70
+ {
71
+ orderBy: "relevance",
72
+ 'max-results' => 15
73
+ }
74
+ when :pl
75
+ {
76
+ v: 2,
77
+ 'max-results' => 1
78
+ }
79
+ end
80
+
81
+ default_params.merge(params)
82
+ end
50
83
  end
51
84
  end
52
85
  end
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Youtube
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
6
6
  spec.version = Ruboty::Youtube::VERSION
7
7
  spec.authors = ['kaihar4']
8
8
  spec.email = ['kaihar4@gmail.com']
9
- spec.summary = 'Search a movie from YouTube'
9
+ spec.summary = 'Ruboty handler to search from YouTube.'
10
10
  spec.description = spec.summary
11
11
  spec.homepage = 'https://github.com/kaihar4/ruboty-youtube'
12
12
  spec.license = 'MIT'
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.add_dependency 'ruboty'
20
20
  spec.add_dependency 'faraday'
21
+ spec.add_dependency 'mechanize'
21
22
 
22
23
  spec.add_development_dependency 'bundler', '~> 1.7'
23
24
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-youtube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaihar4
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-08 00:00:00.000000000 Z
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mechanize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +80,7 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '10.0'
69
- description: Search a movie from YouTube
83
+ description: Ruboty handler to search from YouTube.
70
84
  email:
71
85
  - kaihar4@gmail.com
72
86
  executables: []
@@ -106,5 +120,5 @@ rubyforge_project:
106
120
  rubygems_version: 2.2.2
107
121
  signing_key:
108
122
  specification_version: 4
109
- summary: Search a movie from YouTube
123
+ summary: Ruboty handler to search from YouTube.
110
124
  test_files: []