class BenchmarkTest
Attributes
max_retries[RW]
save_destination[RW]
search_path[RW]
Public Class Methods
new(search_path=nil, save_destination=nil, max_downloads=2, max_retries=10)
click to toggle source
# File lib/bulk_youtube/benchmark_testing.rb, line 13 def initialize(search_path=nil, save_destination=nil, max_downloads=2, max_retries=10) @search_path = search_path @save_destination = save_destination @max_downloads = max_downloads @max_retries = max_retries end
Public Instance Methods
gather(word)
click to toggle source
returns itself
# File lib/bulk_youtube/benchmark_testing.rb, line 82 def gather(word) @link_arr = [] path = mec_get(@search_path) count = 0 if word.nil? time = Benchmark.realtime do ############################################### path.links_with(:href => /watch\?v=/).each do |youtube| break if count > @max_downloads puts youtube.href include_time = Benchmark.realtime do @link_arr << youtube if youtube count += 1 end puts "time of include check loop: #{include_time*1000} milliseconds" end self end puts "time of loop: #{time*1000} milliseconds" else path.links_with(:text => /#{word}/).each do |link| break if count > @max_downloads # next if link.text.include?('Play') if link.text.include?(word) && link.href.include?('watch?v=') @link_arr << link count += 1 end end end self end
grab_links(word=nil)
click to toggle source
# File lib/bulk_youtube/benchmark_testing.rb, line 45 def grab_links(word=nil) gather(word) end
links()
click to toggle source
# File lib/bulk_youtube/benchmark_testing.rb, line 60 def links @link_arr end
mec_get(path)
click to toggle source
# File lib/bulk_youtube/benchmark_testing.rb, line 65 def mec_get(path) Mechanize.new.get(path) end
noko(path)
click to toggle source
# File lib/bulk_youtube/benchmark_testing.rb, line 70 def noko(path) Nokogiri::HTML(path) end
send_files(links)
click to toggle source
requires an array
# File lib/bulk_youtube/benchmark_testing.rb, line 127 def send_files(links) you_convert = 'http://www.youtubeinmp4.com/' links.each_with_index do |link, i| clicked_link = link.click.uri.to_s form = mec_get(you_convert).forms[0] next if link.nil? puts "printing link #{i}" form.field_with(:class => 'c3').value = clicked_link path = "#{@save_destination}/#{i}.mp4" form.submit.links[1].click.save_as(path) puts "link #{1} successful" end end
show()
click to toggle source
# File lib/bulk_youtube/benchmark_testing.rb, line 49 def show arr = [] @link_arr.each do |link| REPLACE.each do |r| link.text.gsub!(r[0], r[1]) end arr << link end arr end
you_convert()
click to toggle source
def you_convert
retry_count = 0 begin send_files(@link_arr) rescue Net::HTTP::Persistent::Error => e raise unless e.message =~ /too man connection resets/ if retry_count > @max_retries puts "**** WARN: Mechanize retried connection reset #{@max_retries} times" raise end retry_count +=1 Mechanize::HTTP::Agent.http.shutdown retry end
end
# File lib/bulk_youtube/benchmark_testing.rb, line 37 def you_convert begin send_files(@link_arr) rescue StandardError false end end