Module: Cherrypicker

Defined in:
lib/cherrypicker/linkchecker.rb,
lib/cherrypicker/helpers.rb,
lib/cherrypicker/version.rb,
lib/cherrypicker/cherrypick.rb,
lib/cherrypicker/plugins/vimeo.rb,
lib/cherrypicker/plugins/rghost.rb,
lib/cherrypicker/plugins/hotfile.rb,
lib/cherrypicker/plugins/youtube.rb,
lib/cherrypicker/plugins/megavideo.rb,
lib/cherrypicker/plugins/rapidshare.rb

Overview

<!– _

__   _|_|_ __ ___   ___  ___
\ \ / / | '_ ' _ \ / _ \/ _ \
 \ V /| | | | | | |  __/ |_| |
  \_/ |_|_| |_| |_|\___|\___/
             you know, for videos

–>

Defined Under Namespace

Classes: Cherrypick, Hotfile, LinkChecker, Megavideo, Rapidshare, Rghost, SingleLink, Vimeo, Youtube

Constant Summary collapse

VERSION =
"0.3.12"

Class Method Summary collapse

Class Method Details

.download_file(link, opts = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cherrypicker/helpers.rb', line 32

def self.download_file(link, opts={})
  o = {
    :location => nil,
    :size => nil,
    :filename => nil
  }.merge(opts)
  
  @link = link
  @size = o[:size]
  @location = o[:location] ||= ""
  @filename = o[:filename]
  @progress = 0
  @finished = false 
  
  uri = URI.parse(@link.to_s)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == "https"
  request = Net::HTTP::Get.new(uri.request_uri)
  request.initialize_http_header({"User-Agent" => Cherrypicker::random_agent})
  unless (uri.host.include? 'youtube.com') && (uri.request_uri.include? 'videoplayback') #youtube throws EOFError
    head = http.request_head(URI.escape(uri.path))
  case head
    when Net::HTTPForbidden
      @size = nil  #no content-length no progress bar
    else
      @size = head['content-length'] if @size.nil? && head['content-length'].to_i > 1024
    end
  end
  puts "unknown file size for #{@filename}" if @size.nil?
  http.request(request) do |response|
    bar = ProgressBar.new((@filename ||= File.basename(uri.path)), @size.to_i) unless @size.nil?
    bar.format_arguments=[:title, :percentage, :bar, :stat_for_file_transfer] unless @size.nil?
          
    File.open(@location + (@filename ||= File.basename(uri.path)), "wb") do |file|
      response.read_body do |segment|
        @progress += segment.length
        bar.set(@progress) unless @size.nil?
        file.write(segment)
      end
    end
  end
  @finished = true
  puts
  puts "download completed"
  puts
end

.hash_to_url(hash) ⇒ Object



79
80
81
82
83
84
# File 'lib/cherrypicker/helpers.rb', line 79

def self.hash_to_url(hash)
  hash.keys.inject('') do |query_string, key|
    query_string << '&' unless key == hash.keys.first
    query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key])}"
  end
end

.random_agentObject



86
87
88
89
# File 'lib/cherrypicker/helpers.rb', line 86

def self.random_agent
  @useragent = ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.50 Safari/534.24"]
  return @useragent[0]
end

.remote_query(url) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cherrypicker/helpers.rb', line 15

def self.remote_query(url)  
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == "https"
  http.open_timeout = 3 # seconds
  http.read_timeout = 3 # seconds
  request = Net::HTTP::Get.new(uri.request_uri)
  request.initialize_http_header({"User-Agent" => random_agent})
  response = http.request(request)
  if response["X-APICPU"]
    if (response["X-APICPU"][/(\d*)\/\d*/, 1]).to_i > 9500
      sleep 60  # seconds
    end
  end
  return http.request(request)
end