motion-tube 0.0.3 → 0.0.4

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -11
  3. data/lib/motion-tube/parser.rb +52 -15
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d61199257e623e4c13bf49f2ed1a3a59a435af26
4
- data.tar.gz: efa6b69acde194fb09ddc8ca0a10617e1a904eee
3
+ metadata.gz: d3d75ebbbd4ee6e24b09712dd114249a7d0bb616
4
+ data.tar.gz: 922b857a2fbf0c9b1e562b28881a1223682679e7
5
5
  SHA512:
6
- metadata.gz: d4e6f57b5c8d3ff23b2c77363fac769185b9220df612e54db6eb3f980c4f68d847eee7a4cc0b13b9d8fa1a155c9bbbff69da993a2404f65d492148488f0f1863
7
- data.tar.gz: 6c3dc841e56616fe4d181059650ef0bed56ad6430772fd84130253fd00392f73c8f95b30f0121b7fa5158ff3cda9b8404f6037d9f6d82500b3df2df3057d3776
6
+ metadata.gz: 9b13730c9afa0f147d4a30e41d5c42179e4ce46e558e1d163a2840edca0c03a90b1b7c519b67679fda3742c067fa461571124af6bfd0442e44b1c824f79b47f9
7
+ data.tar.gz: dd50d23856bd84c51e1b4e103b573a86786f9b5226b1f311ec3af6854504d32e700d5ae5445b51b0b870ef4acc4f2d2357e2b99f0bb82638718d30ca8786f7e7
data/README.md CHANGED
@@ -22,17 +22,20 @@ Or install it yourself as:
22
22
  To play a youtube video can be as simple as:
23
23
 
24
24
  ```
25
- (main)> parser = MotionTube::Parser.new
26
- "https://www.youtube.com/watch?v=_S1S1gGS3lU"
27
- => #<MotionTube::Parser:0x10ef5e8b0
28
- @url="https://www.youtube.com/watch?v=_S1S1gGS3lU"
29
- @rest_client=#<AFHTTPRequestOperationManager:0x10ef5fdd0>>
30
- (main)> parser.parse do |result|
31
- (main)> puts result[:title]
32
- (main)> BW::Media.play_modal result[:media]["mp4"].last
33
- (main)> end
34
- => #<AFHTTPRequestOperation:0x10ec3faa0>
35
- (main)> CoreDataQuery+(CDQ)+In+Action
25
+ parser = MotionTube::Parser.new
26
+ parser.parse(source: {url: "https://www.youtube.com/watch?v=_S1S1gGS3lU"} ) do |result|
27
+ puts result[:title]
28
+ BW::Media.play_modal result[:media]["mp4"].last
29
+ end
30
+ ```
31
+
32
+ To get back a playlist
33
+
34
+ ```
35
+ parser = MotionTube::Parser.new
36
+ parser.parse(source: {playlist: "PL7QBhjs24ko_dq2Nm1NovIqIAr7kAuWJb"}) do |result|
37
+ puts result
38
+ end
36
39
  ```
37
40
 
38
41
  ## Contributing
@@ -1,15 +1,13 @@
1
1
  module MotionTube
2
2
  class Parser
3
- attr_accessor :rest_client, :url, :response
4
- def initialize(url)
5
- @url = url
6
- @rest_client = AFMotion::Client.build("http://www.youtube.com") do
7
- header "Accept", "application/json"
8
- response_serializer :json
9
- end
3
+ attr_accessor :rest_client, :url, :response, :options
4
+ def initialize(options = {})
5
+ self.options = options
6
+ set_rest_client "http://www.youtube.com"
10
7
  end
11
8
 
12
- def parse(&block)
9
+ def parse(options, &block)
10
+ self.options.merge! options
13
11
  if block
14
12
  get_response(&block)
15
13
  else
@@ -20,6 +18,7 @@ module MotionTube
20
18
  private
21
19
 
22
20
  def id
21
+ url = options[:source][:url]
23
22
  if url =~ /(?:v=|youtu\.be\/|youtube\.com\/v\/)([^.|&]+)/
24
23
  url.scan(/(?:v=|youtu\.be\/|youtube\.com\/v\/)([^.|&]+)/)[0][0]
25
24
  elsif url =~ /\/embed\//
@@ -34,7 +33,7 @@ module MotionTube
34
33
  end
35
34
 
36
35
  def title
37
- @response.scan(/title=([^&]+)/)[0][0].unescape_url
36
+ response.scan(/title=([^&]+)/)[0][0].unescape_url
38
37
  end
39
38
 
40
39
 
@@ -61,17 +60,55 @@ module MotionTube
61
60
  end
62
61
 
63
62
  def get_response(&result_callback)
63
+ if options[:source][:url]
64
+ get_video_info_from_id id, &result_callback
65
+ elsif playlist = options[:source][:playlist]
66
+ set_rest_client("http://gdata.youtube.com")
67
+ get_video_info_from_playlist playlist, &result_callback
68
+ end
69
+ end
70
+
71
+ def set_rest_client(url)
72
+ self.rest_client = AFMotion::Client.build(url) do
73
+ header "Accept", "application/json"
74
+ response_serializer :json
75
+ end
76
+ end
77
+
78
+ def get_video_info_from_playlist(playlist, &result_callback)
79
+ path = "/feeds/api/playlists/#{playlist}?v=2&alt=json&max-results=50"
80
+ rest_client.get(path) do |result|
81
+ self.response = result.body
82
+
83
+ hash = result.object
84
+ entry = hash["feed"]["entry"]
85
+ youtube_playlist_title = hash["feed"]["title"]["$t"]
86
+
87
+ video_list = entry.map do |e|
88
+ e_hash = {}
89
+ e_hash["title"] = e["title"]["$t"]
90
+ e_hash["href"] = e["link"][0]["href"]
91
+ e_hash
92
+ end
64
93
 
65
- rest_client.post("get_video_info", video_id: id) do |result|
66
- @response = result.body
94
+ result_hash = {}
95
+ result_hash[youtube_playlist_title] = video_list
96
+ result_callback.call result_hash
97
+ end
98
+ end
99
+
100
+ def get_video_info_from_id(video_id, &result_callback)
101
+ rest_client.post("get_video_info", video_id: video_id) do |result|
102
+ self.response = result.body
67
103
  hash = {
68
- id: id,
69
- title: title,
70
- cover: cover,
71
- media: media
104
+ "id" => id,
105
+ "title" => title,
106
+ "cover" => cover,
107
+ "media" => media
72
108
  }
73
109
  result_callback.call hash
74
110
  end
75
111
  end
112
+
76
113
  end
77
114
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-tube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Ruan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-03 00:00:00.000000000 Z
11
+ date: 2015-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: motion-cocoapods