YoutubeTools 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{YoutubeTools}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Maykon Luis Capellari"]
12
+ s.date = %q{2010-09-28}
13
+ s.description = %q{Gem from search, download and converter youtube videos to mp3}
14
+ s.email = %q{maykon_capellari@yahoo.com.br}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "TODO.rdoc",
24
+ "VERSION",
25
+ "YoutubeTools.gemspec",
26
+ "init.rb",
27
+ "lib/youtube_tools.rb",
28
+ "lib/youtube_tools/converter.rb",
29
+ "lib/youtube_tools/downloader.rb",
30
+ "lib/youtube_tools/searcher.rb",
31
+ "test/helper.rb",
32
+ "test/test_YoutubeTools.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/maykon/YoutubeTools}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{Gem from search, download and converter youtube videos to mp3}
39
+ s.test_files = [
40
+ "test/test_YoutubeTools.rb",
41
+ "test/helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<minitest>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<minitest>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<minitest>, [">= 0"])
55
+ end
56
+ end
57
+
@@ -1,28 +1,25 @@
1
1
  module YoutubeTools
2
2
  class Downloader
3
- attr_accessor :href, :name, :full_path, :content, :link_dw, :percent, :total
4
-
3
+ attr_accessor :href, :name, :quality, :full_path, :link_dw, :links
4
+
5
5
  LINK_PATTERN = /"fmt_stream_map": ("[\w |:\/\\.?=&\,%-]+")/
6
6
  NAME_PATTERN = /-\s+(.*)\s*/
7
+ QUALITY = { :low => 5, :mp4 => 18, :mid => 34, :hd => 22, :hight => 35 }
7
8
 
8
- def initialize(href, path=nil)
9
+ def initialize(href, options={})
9
10
  @href = href
10
- @music_folder = path.nil? ? FOLDER_PATH : path
11
-
11
+ @video_folder = options[:path].nil? ? FOLDER_PATH : options[:path]
12
+ @quality = options[:quality].nil? ? set_quality(:low) : set_quality(options[:quality])
13
+ @error_quality = 0
14
+ @links = {}
15
+
12
16
  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)
17
+ init_information
22
18
  end
23
19
 
24
- def full_path
25
- @full_path ||= File.join(@music_folder, name.split("/").last)
20
+ def set_quality(quality)
21
+ return QUALITY[quality] if QUALITY.include? quality
22
+ QUALITY[:low]
26
23
  end
27
24
 
28
25
  # method dowload video from selected folder
@@ -40,46 +37,61 @@ module YoutubeTools
40
37
  print "..#{@percent}%" if @percent != old_percent
41
38
  }).read)
42
39
  end
40
+ puts "Download complete!"
43
41
  end
44
42
 
45
43
  protected
46
44
  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)
45
+ content = open(@href)
46
+ doc = Hpricot(content)
47
+ @name = search_name(doc.search("//title").first)
48
+ @link_dw = search_link(doc)
49
+ @full_path = File.join(@video_folder, @name.split("/").last)
52
50
  end
53
51
 
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]
52
+ def quality_key(value)
53
+ case value
54
+ when 18
55
+ return :mp4
56
+ when 22
57
+ return :hd
58
+ when 34, 4
59
+ return :mid
60
+ when 35
61
+ return :hight
62
+ else
63
+ return :low
64
+ end
68
65
  end
69
66
 
70
67
  # method for search name for video
71
68
  def search_name(content)
72
69
  name = content.to_plain_text.gsub(NAME_PATTERN).first
73
- name = name[1..name.length].gsub(/[\n\s()`'"\/,;<>?!&%$@*+=.]/, "")
70
+ name = name[1..name.length].gsub(/[\n\s()`'"\/,;<>?!&%$@*+=.\[\]]/, "")
74
71
  name[0..name.length-1]
75
72
  end
76
73
 
74
+ def search_link(doc)
75
+ fmt = doc.to_html.gsub(LINK_PATTERN).first
76
+ fmt = fmt.gsub!(/([^,]+)/)
77
+ fmt.each_with_index do |l, i|
78
+ l.gsub!(/"fmt_stream_map": "/, "") if i == 0
79
+ ql = l.gsub(/^(\d)+|$/).first
80
+ qlk = quality_key(ql.to_i)
81
+ l.gsub!(/(^(\d)+\|)/, "").gsub!(/[\\,"]/, "").gsub!(/(\|\|.*)/, "")
82
+ @links[qlk] = l
83
+ end
84
+ quality_present = @links.keys.include? quality_key(@quality)
85
+ return @links[quality_key(@quality)] if quality_present
86
+ @links[:low]
87
+ end
88
+
77
89
  # method create folder case not exist
78
90
  def create_folder
79
91
  #create folder if no exist
80
- unless File.exist? @music_folder
81
- puts "Creating #{@music_folder}"
82
- FileUtils.mkdir_p(@music_folder)
92
+ unless File.exist? @video_folder
93
+ puts "Creating #{@video_folder}"
94
+ FileUtils.mkdir_p(@video_folder)
83
95
  end
84
96
  end
85
97
  end
@@ -1,12 +1,17 @@
1
1
  module YoutubeTools
2
2
  class Searcher
3
- attr_accessor :term, :search_term, :href, :links
3
+ attr_accessor :term, :search_term, :href, :links, :order_by, :start_index, :max_results
4
+
5
+ ORDER_BY = %w(relevance published viewCount rating)
4
6
  YOUTUBE_LINK = "http://gdata.youtube.com/feeds/api/videos"
5
7
 
6
- def initialize(term)
8
+ def initialize(term, options={})
7
9
  @term = term
8
- @search_term = url_term term
9
- @href = "#{YOUTUBE_LINK}?vq=#{@search_term}"
10
+ @search_term = url_term term
11
+ @order_by = options[:order_by].nil? ? set_order(0) : set_order(options[:order_by])
12
+ @start_index = options[:start_index].nil? ? 0 : set_start(options[:start_index])
13
+ @max_results = options[:max_results].nil? ? 10 : set_max(options[:max_results])
14
+ @href = make_url
10
15
  @content = open(@href)
11
16
  @links = []
12
17
  process
@@ -22,9 +27,36 @@ module YoutubeTools
22
27
  end
23
28
  end
24
29
 
30
+ def set_order(index)
31
+ return ORDER_BY[index] if index > 0 && index < 4
32
+ ORDER_BY[0]
33
+ end
34
+
35
+ def set_start(val)
36
+ return val if val >= 0
37
+ 0
38
+ end
39
+
40
+ def set_max(max)
41
+ return max if max > 0 && max <= 50
42
+ 10
43
+ end
44
+
25
45
  protected
46
+ def make_url
47
+ href = "#{YOUTUBE_LINK}?vq=#{@search_term}"
48
+ href << "&orderby=#{@order_by}" if @order_by != ORDER_BY[0]
49
+ href << "&start-index=#{@start_index}" if @start_index > 0
50
+ href << "&max-results=#{@max_results}" if @max_results != 10
51
+ href
52
+ end
53
+
26
54
  def url_term(term)
27
- term.gsub(/[ ]/, "+")
55
+ CHARACTERS.each do |char, val|
56
+ puts "#{char}: #{val}"
57
+ term.gsub!(/[#{char}]/, val)
58
+ end
59
+ term
28
60
  end
29
61
  end
30
62
  end
data/lib/youtube_tools.rb CHANGED
@@ -5,6 +5,11 @@ require 'open-uri'
5
5
  module YoutubeTools
6
6
  FOLDER_PATH = File.join(ENV['HOME'],"/Musica/RTubeToMp3") # Folder Path from musics
7
7
 
8
+ CHARACTERS = { " " => "+", "$" => "%24", "-" => "%2D", "_" => "%5F", "." => "%2E", "+" => "%2B",
9
+ "!" => "%21", "*" => "%2A", "\"" => "%22", "'" => "%27", "(" => "%28", ")" => "%29",
10
+ ";" => "%3B", "/" => "%2F", "?" => "%3F", ":" => "%3A", "@" => "%40", "=" => "%3D",
11
+ "&" => "%26", "|" => "%7C" }
12
+
8
13
  autoload :Searcher, 'youtube_tools/searcher'
9
14
  autoload :Downloader, 'youtube_tools/downloader'
10
15
  autoload :Converter, 'youtube_tools/converter'
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Maykon Luis Capellari
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-27 00:00:00 -03:00
17
+ date: 2010-09-28 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -45,6 +45,7 @@ files:
45
45
  - Rakefile
46
46
  - TODO.rdoc
47
47
  - VERSION
48
+ - YoutubeTools.gemspec
48
49
  - init.rb
49
50
  - lib/youtube_tools.rb
50
51
  - lib/youtube_tools/converter.rb