YoutubeTools 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Maykon Luís Capellari
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,46 @@
1
+ == YoutubeTools
2
+
3
+ * http://github.com/maykon/YoutubeTools
4
+
5
+ == DESCRIPTION
6
+
7
+ Gem from search, download and converter youtube videos to mp3
8
+
9
+ == Dependencies
10
+
11
+ * ruby 1.8.6
12
+ * hpricot[http://github.com/hpricot]
13
+ * open-uri
14
+
15
+ == SUPPORT:
16
+
17
+ If you need support send e-mail to:
18
+
19
+ * maykon_capellari@yahoo.com.br
20
+
21
+ The bug tracker is available here:
22
+
23
+ * http://github.com/maykon/YoutubeTools/issues
24
+
25
+ == Installation
26
+
27
+ Install the gem:
28
+
29
+ gem install "youtube_tools"
30
+
31
+ Add it to your Gemfile:
32
+
33
+ gem "youtube_tools"
34
+
35
+ == Usage
36
+
37
+
38
+ == Authors
39
+
40
+ Copyright (c) 2010:
41
+
42
+ * Maykon Luís Capellari [http://github.com/maykon] (maykon_capellari@yahoo.com.br)
43
+
44
+ == License
45
+
46
+ This library is distributed under the GPL. Please see the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "YoutubeTools"
8
+ gem.summary = %Q{Gem from search, download and converter youtube videos to mp3}
9
+ gem.description = %Q{Gem from search, download and converter youtube videos to mp3}
10
+ gem.email = "maykon_capellari@yahoo.com.br"
11
+ gem.homepage = "http://github.com/maykon/YoutubeTools"
12
+ gem.authors = ["Maykon Luis Capellari"]
13
+ gem.add_development_dependency "minitest", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "YoutubeTools #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/TODO.rdoc ADDED
@@ -0,0 +1 @@
1
+ * Improvement support to converter more file types
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'youtube_tools'
@@ -0,0 +1,19 @@
1
+ module YoutubeTools
2
+ class Converter
3
+ attr_accessor :file, :folder, :file_name
4
+
5
+ def initialize(file, path=nil)
6
+ @folder = path.nil? ? FOLDER_PATH : path
7
+
8
+ @file = "#{@folder}/#{file}"
9
+ converter_to
10
+ end
11
+
12
+ protected
13
+ def converter_to(format=:mp3)
14
+ @file_name = "#{@file}.#{format}"
15
+ system "ffmpeg -i #{@file} -ab 128k -ac 2 -acodec libmp3lame -vn -y #{@file_name}"
16
+ system "rm -Rf #{@file}" if File.exist? @file
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,86 @@
1
+ module YoutubeTools
2
+ class Downloader
3
+ attr_accessor :href, :name, :full_path, :content, :link_dw, :percent, :total
4
+
5
+ LINK_PATTERN = /"fmt_stream_map": ("[\w |:\/\\.?=&\,%-]+")/
6
+ NAME_PATTERN = /-\s+(.*)\s*/
7
+
8
+ def initialize(href, path=nil)
9
+ @href = href
10
+ @music_folder = path.nil? ? FOLDER_PATH : path
11
+
12
+ create_folder
13
+ init_information
14
+ end
15
+
16
+ def name
17
+ @name ||= search_name(@doc.search("//title").first)
18
+ end
19
+
20
+ def link_dw
21
+ @link_dw ||= search_link(@doc.search("//script")[8].to_plain_text)
22
+ end
23
+
24
+ def full_path
25
+ @full_path ||= File.join(@music_folder, name.split("/").last)
26
+ end
27
+
28
+ # method dowload video from selected folder
29
+ def download_video
30
+ @percent = @total = 0
31
+ print "Downloading...0%"
32
+ open(@full_path, 'wb') do |file|
33
+ file.write(open(@link_dw, :content_length_proc => lambda {|t|
34
+ if t && 0 < t
35
+ @total = t
36
+ end
37
+ }, :progress_proc => lambda {|s|
38
+ old_percent = @percent
39
+ @percent = (s * 100)/@total
40
+ print "..#{@percent}%" if @percent != old_percent
41
+ }).read)
42
+ end
43
+ end
44
+
45
+ protected
46
+ def init_information
47
+ @content = open(@href)
48
+ @doc = Hpricot(@content)
49
+ @name = search_name(@doc.search("//title").first)
50
+ @link_dw = search_link(@doc.search("//script")[8].to_plain_text)
51
+ @full_path = File.join(@music_folder, name.split("/").last)
52
+ end
53
+
54
+ # method fo search link to download video
55
+ # values for type :
56
+ # 5 = low quality
57
+ # 18 = mid quality (mp4)
58
+ # 22 = HD quality
59
+ # 34 or 4 = mid quality
60
+ # 35 = hight quality
61
+ def search_link(link_dw, type=5)
62
+ url_pattern = /,#{type}\|(http[^\|]+)(?:\|.*)?$/
63
+ link = link_dw.gsub(LINK_PATTERN).first
64
+ link = link[19...link.length]
65
+ link = link.gsub(url_pattern).first
66
+ link = link.gsub(/[\\,"]/, "").gsub(/(\|\|.*)/, "")
67
+ link[2..link.length]
68
+ end
69
+
70
+ # method for search name for video
71
+ def search_name(content)
72
+ name = content.to_plain_text.gsub(NAME_PATTERN).first
73
+ name = name[1..name.length].gsub(/[\n\s()`'"\/,;<>?!&%$@*+=.]/, "")
74
+ name[0..name.length-1]
75
+ end
76
+
77
+ # method create folder case not exist
78
+ def create_folder
79
+ #create folder if no exist
80
+ unless File.exist? @music_folder
81
+ puts "Creating #{@music_folder}"
82
+ FileUtils.mkdir_p(@music_folder)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,30 @@
1
+ module YoutubeTools
2
+ class Searcher
3
+ attr_accessor :term, :search_term, :href, :links
4
+ YOUTUBE_LINK = "http://gdata.youtube.com/feeds/api/videos"
5
+
6
+ def initialize(term)
7
+ @term = term
8
+ @search_term = url_term term
9
+ @href = "#{YOUTUBE_LINK}?vq=#{@search_term}"
10
+ @content = open(@href)
11
+ @links = []
12
+ process
13
+ end
14
+
15
+ def process
16
+ doc = Hpricot(@content)
17
+
18
+ @links = doc.search("//entry").collect do |entry|
19
+ { :title => entry.get_elements_by_tag_name("title").text,
20
+ :link => entry.get_elements_by_tag_name("link").first.get_attribute("href")
21
+ }
22
+ end
23
+ end
24
+
25
+ protected
26
+ def url_term(term)
27
+ term.gsub(/[ ]/, "+")
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'hpricot'
3
+ require 'open-uri'
4
+
5
+ module YoutubeTools
6
+ FOLDER_PATH = File.join(ENV['HOME'],"/Musica/RTubeToMp3") # Folder Path from musics
7
+
8
+ autoload :Searcher, 'youtube_tools/searcher'
9
+ autoload :Downloader, 'youtube_tools/downloader'
10
+ autoload :Converter, 'youtube_tools/converter'
11
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'minitest/unit'
3
+ require 'minitest/spec'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'youtube_tools'
8
+
9
+ class MiniTest::Unit::TestCase
10
+ end
11
+
12
+ MiniTest::Unit.autorun
@@ -0,0 +1,56 @@
1
+ require 'helper'
2
+
3
+ describe YoutubeTools do
4
+ before do
5
+ @term = "Bon Jovi"
6
+ @search_term = "Bon+Jovi"
7
+ @href = "http://www.youtube.com/watch?v=vx2u5uUu3DE&feature=youtube_gdata"
8
+ @yapi = "http://gdata.youtube.com/feeds/api/videos?vq="
9
+ @title = "Bon Jovi - It's My Life"
10
+ @file_name = "BonJovi-ItsMyLife"
11
+ @path = File.join(ENV['HOME'],"/Musica/RTubeToMp3")
12
+ end
13
+
14
+ describe "when search a video" do
15
+ before do
16
+ @yts = YoutubeTools::Searcher.new @term
17
+ end
18
+
19
+ it "should have a search name and href" do
20
+ @yts.search_term.must_equal @search_term
21
+ @yts.href.must_equal "#{@yapi}#{@search_term}"
22
+ end
23
+
24
+ it "should have not empty links and included link" do
25
+ @yts.links.wont_be_empty
26
+ link = {:title=>@title, :link=>@href}
27
+ @yts.links.must_include link
28
+ end
29
+ end
30
+
31
+ describe "when download and converter a video" do
32
+ before do
33
+ @ytd = YoutubeTools::Downloader.new @href
34
+ end
35
+
36
+ it "should have music path and name" do
37
+ @ytd.name.must_equal @file_name
38
+ @ytd.full_path.must_equal "#{@path}/#{@file_name}"
39
+ ytd = YoutubeTools::Downloader.new @href, "/home/maykon/Musica"
40
+ ytd.full_path.must_equal "/home/maykon/Musica/#{@file_name}"
41
+ end
42
+
43
+ it "should have download file and converter video" do
44
+ @ytd.link_dw.wont_be_nil
45
+ @ytd.link_dw.must_match /(http[^\|]+)(?:\|.*)?$/
46
+ @ytd.download_video
47
+ File.exist?(@ytd.full_path).must_equal true
48
+
49
+ ## converter video
50
+ @ytc = YoutubeTools::Converter.new @file_name
51
+ @ytc.file_name.must_equal "#{@path}/#{@file_name}.mp3"
52
+ File.exist?(@ytc.file_name).must_equal true
53
+ File.exist?(@ytc.file).wont_equal true
54
+ end
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: YoutubeTools
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Maykon Luis Capellari
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-27 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: minitest
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: Gem from search, download and converter youtube videos to mp3
34
+ email: maykon_capellari@yahoo.com.br
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - LICENSE
41
+ - README.rdoc
42
+ files:
43
+ - LICENSE
44
+ - README.rdoc
45
+ - Rakefile
46
+ - TODO.rdoc
47
+ - VERSION
48
+ - init.rb
49
+ - lib/youtube_tools.rb
50
+ - lib/youtube_tools/converter.rb
51
+ - lib/youtube_tools/downloader.rb
52
+ - lib/youtube_tools/searcher.rb
53
+ - test/helper.rb
54
+ - test/test_YoutubeTools.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/maykon/YoutubeTools
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Gem from search, download and converter youtube videos to mp3
87
+ test_files:
88
+ - test/test_YoutubeTools.rb
89
+ - test/helper.rb