downthetube 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +41 -0
  2. data/downthetube.gemspec +18 -0
  3. data/lib/youtube.rb +113 -0
  4. metadata +84 -0
data/README ADDED
@@ -0,0 +1,41 @@
1
+ Many libraries have been written to pull things off Youtube. This is one of them.
2
+
3
+
4
+
5
+ irb(main):006:0> playlist = Youtube.playlists_for("stephensam").first
6
+ => #<Youtube::Playlist:0x7fae33fc06a0 @author="stephensam", @title="Capoeira Angola", @url="http://gdata.youtube.com/feeds/api/playlists/76E01802262A6694", @id="76E01802262A6694", @xml=<entry gd:etag='W/&quot;CUQGR347eCp7ImA9WhZVGEk.&quot;'> ... </>>
7
+ irb(main):007:0> playlist.title
8
+ => "Capoeira Angola"
9
+ irb(main):008:0> playlist.url
10
+ => "http://gdata.youtube.com/feeds/api/playlists/76E01802262A6694"
11
+ irb(main):009:0> playlist.videos(1)
12
+ => [#<Youtube::Video:0x7fae340c4e98 @uploader="cdoucrania", @description="www.cdo.net.ua", @title="Mestre Jogo de Dentro on Capoerando 2010", @duration="218", @url="http://www.youtube.com/watch?v=aoBRfYkEH2w", @id="aoBRfYkEH2w", @small_thumbnail="http://i.ytimg.com/vi/aoBRfYkEH2w/default.jpg", @xml=<entry gd:etag='W/&quot;DUUMRX4_eip7ImA9WhZVGEU.&quot;'> ... </>, @large_thumbnail="http://i.ytimg.com/vi/aoBRfYkEH2w/hqdefault.jpg">]
13
+ irb(main):010:0> video = playlist.videos(1)
14
+ => [#<Youtube::Video:0x7fae340c4e98 @uploader="cdoucrania", @description="www.cdo.net.ua", @title="Mestre Jogo de Dentro on Capoerando 2010", @duration="218", @url="http://www.youtube.com/watch?v=aoBRfYkEH2w", @id="aoBRfYkEH2w", @small_thumbnail="http://i.ytimg.com/vi/aoBRfYkEH2w/default.jpg", @xml=<entry gd:etag='W/&quot;DUUMRX4_eip7ImA9WhZVGEU.&quot;'> ... </>, @large_thumbnail="http://i.ytimg.com/vi/aoBRfYkEH2w/hqdefault.jpg">]
15
+ irb(main):012:0> video = playlist.videos(1).first
16
+ => #<Youtube::Video:0x7fae340c4e98 @uploader="cdoucrania", @description="www.cdo.net.ua", @title="Mestre Jogo de Dentro on Capoerando 2010", @duration="218", @url="http://www.youtube.com/watch?v=aoBRfYkEH2w", @id="aoBRfYkEH2w", @small_thumbnail="http://i.ytimg.com/vi/aoBRfYkEH2w/default.jpg", @xml=<entry gd:etag='W/&quot;DUUMRX4_eip7ImA9WhZVGEU.&quot;'> ... </>, @large_thumbnail="http://i.ytimg.com/vi/aoBRfYkEH2w/hqdefault.jpg">
17
+ irb(main):013:0> video.title
18
+ => "Mestre Jogo de Dentro on Capoerando 2010"
19
+ irb(main):014:0> video.description
20
+ => "www.cdo.net.ua"
21
+ irb(main):015:0> video.thumbnail(:large)
22
+ => "http://i.ytimg.com/vi/aoBRfYkEH2w/hqdefault.jpg"
23
+ irb(main):016:0> video.thumbnail
24
+ => "http://i.ytimg.com/vi/aoBRfYkEH2w/default.jpg"
25
+ irb(main):017:0> video.url
26
+ => "http://www.youtube.com/watch?v=aoBRfYkEH2w"
27
+ irb(main):018:0>
28
+
29
+
30
+ LICENSE:
31
+
32
+ (The MIT License)
33
+
34
+ Copyright © 2008 - 2010 Mike Williamson
35
+
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ specification = Gem::Specification.new do |spec|
2
+ # Descriptive and source information for this gem.
3
+ spec.name = "downthetube"
4
+ spec.version = "0.0.1"
5
+ spec.description = "A library to make downloading playlists and videos from Youtube less nasty."
6
+ spec.summary = "Downloads playlists and videos from Youtube."
7
+ spec.author = "Mike Williamson"
8
+ spec.email = "blessedbyvirtuosity@gmail.com"
9
+ spec.homepage = ""
10
+ require 'rake'
11
+ spec.files = %w(README lib/youtube.rb downthetube.gemspec)
12
+ spec.has_rdoc = false
13
+ spec.add_dependency("gdata", ">= 1.0.0")
14
+ spec.extra_rdoc_files = ["README"]
15
+ spec.require_path = ["lib"]
16
+ spec.test_files = ["spec"]
17
+ end
18
+
data/lib/youtube.rb ADDED
@@ -0,0 +1,113 @@
1
+ class Youtube
2
+ require 'rubygems'
3
+ require 'gdata'
4
+
5
+ def Youtube.playlists_for user
6
+ @client = GData::Client::YouTube.new
7
+ feed = @client.get("http://gdata.youtube.com/feeds/api/users/#{user}/playlists?v=2").to_xml
8
+ playlists = []
9
+ feed.elements.each('entry') do |entry|
10
+ playlists<< Playlist.new(entry)
11
+ end
12
+ playlists
13
+ end
14
+
15
+
16
+ class Playlist
17
+ attr_reader :title, :author, :url, :id
18
+
19
+ def initialize entry
20
+ if entry.class == REXML::Element
21
+ @xml = entry
22
+ @id = @xml.get_text('yt:playlistId')
23
+ @title = @xml.get_text('title')
24
+ @author = @xml.get_text('author/name')
25
+ @xml.each_element_with_attribute('src'){|a| @url = a.attribute('src').to_s }
26
+ else
27
+ raise "What was passed into Playlist class was not an REXML object"
28
+ end
29
+ end
30
+
31
+ def videos limit=nil
32
+
33
+ case
34
+ when limit.nil?
35
+ get_videos unless @videos
36
+ when limit
37
+ if limit == @limit
38
+ get_videos(limit) unless @videos
39
+ else
40
+ get_videos(limit)
41
+ @limit = limit
42
+ end
43
+ else
44
+ puts "Your case has gone badly wrong"
45
+ end
46
+ @videos
47
+ end
48
+
49
+ private
50
+
51
+ def get_videos number=nil
52
+ @client ||= GData::Client::YouTube.new
53
+ feed = @client.get(self.url).to_xml
54
+ @videos = []
55
+ counter = 1
56
+ if number
57
+ feed.elements.each('entry') do |entry|
58
+ if counter <= number
59
+ @videos<< Video.new(entry)
60
+ counter += 1
61
+ end
62
+ end
63
+ else
64
+ feed.elements.each('entry') do |entry|
65
+ @videos<< Video.new(entry)
66
+ end
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+
73
+ class Video
74
+ attr_reader :url, :title, :description, :id, :thumbnail, :uploader, :duration
75
+
76
+ def initialize entry
77
+ if entry.class == REXML::Element
78
+ @xml = entry
79
+ @xml.elements.each('media:group') do |element|
80
+ @title = element.get_text('media:title')
81
+ @id = element.get_text('yt:videoid')
82
+ @description = element.get_text('media:description')
83
+ @uploader = element.get_text('media:credit')
84
+ element.elements.each('media:player'){|mp| @url = mp.attribute('url').to_s.gsub('&amp;feature=youtube_gdata_player', '')}
85
+ element.elements.each('yt:duration'){|a| @duration = a.attribute('seconds').to_s }
86
+ element.elements.each('media:thumbnail') do |mt|
87
+ case mt.attribute('yt:name').to_s
88
+ when 'hqdefault'
89
+ @large_thumbnail = mt.attribute('url').to_s
90
+ when 'default'
91
+ @small_thumbnail = mt.attribute('url').to_s
92
+ end
93
+ end
94
+ end
95
+ else
96
+ raise "What was passed into Playlist class was not an REXML object"
97
+ end
98
+ end
99
+
100
+
101
+ def thumbnail size=:small
102
+ if size == :large
103
+ @large_thumbnail
104
+ else
105
+ @small_thumbnail
106
+ end
107
+ end
108
+
109
+
110
+ end
111
+
112
+
113
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: downthetube
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mike Williamson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-31 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: gdata
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: A library to make downloading playlists and videos from Youtube less nasty.
38
+ email: blessedbyvirtuosity@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README
45
+ files:
46
+ - README
47
+ - lib/youtube.rb
48
+ - downthetube.gemspec
49
+ has_rdoc: true
50
+ homepage: ""
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.5.3
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Downloads playlists and videos from Youtube.
83
+ test_files: []
84
+