Class: Rapidshare::Ext::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/rapidshare-ext/download.rb

Overview

Downloads files from Rapidshare. Separate from Rapidshare::API class because downloading is much more complex than other service calls.

Displays text progress bar during download.

Note from odiszapc: 1 oct 2012 the Rapidshare download API (images.rapidshare.com/apidoc.txt) was changed so the base download function provided by rapidshare gem became broken

In the original method the parameter :filename being interpreted incorrectly now. It’s being interpreted like a ‘save file as’ parameter. Actually it must be equal to the file name you want to download So, to download file now you must specify exactly two parameters: file id and file name.

Constant Summary collapse

DOWNLOAD_URL =
'https://rs%s%s.rapidshare.com/cgi-bin/rsapi.cgi?%s'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, api, options = {}) ⇒ Download

Options:

  • filename (optional) - specifies filename under which the file will be saved. Default: filename parsed from Rapidshare link.

  • downloads_dir (optional) - specifies directory into which downloaded files will be saved. Default: current directory.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rapidshare-ext/download.rb', line 28

def initialize(url, api, options = {})
  @url = url
  @api = api
  @filename = options[:save_as]
  @downloads_dir = options[:downloads_dir] || Dir.pwd

  # OPTIMIZE replace these simple status variables with status codes

  # and corresponding errors like "File not found"

  #

  # set to true when file is successfully downloaded

  @downloaded = false
  # non-critical error is stored here, beside being displayed

  @error = nil

  @local_filename = options[:save_as]
  @filename = nil # It will be filled automatically by the #check method of the base class

end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def api
  @api
end

#downloadedObject (readonly)

Returns the value of attribute downloaded.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def downloaded
  @downloaded
end

#downloads_dirObject (readonly)

Returns the value of attribute downloads_dir.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def downloads_dir
  @downloads_dir
end

#errorObject (readonly)

Returns the value of attribute error.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def error
  @error
end

#fileidObject (readonly)

Returns the value of attribute fileid.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def fileid
  @fileid
end

#filenameObject (readonly)

Returns the value of attribute filename.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def filename
  @filename
end

#filesizeObject (readonly)

Returns the value of attribute filesize.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def filesize
  @filesize
end

#server_idObject (readonly)

Returns the value of attribute server_id.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def server_id
  @server_id
end

#short_hostObject (readonly)

Returns the value of attribute short_host.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def short_host
  @short_host
end

#urlObject (readonly)

Returns the value of attribute url.



19
20
21
# File 'lib/rapidshare-ext/download.rb', line 19

def url
  @url
end

Instance Method Details

#checkObject

Checks if file exists (using checkfiles service) and gets data necessary for download.

Returns true or false, which determines whether the file can be downloaded.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rapidshare-ext/download.rb', line 50

def check
  # PS: Api#checkfiles throws exception when file cannot be found

  response = @api.checkfiles(@url).first rescue {}

  if response[:file_status] == :ok
    @fileid = response[:file_id]
    @filename ||= response[:file_name]
    @filesize = response[:file_size].to_i
    @server_id = response[:server_id]
    @short_host = response[:short_host]

    @remote_filename = @filename
    @filename = @local_filename || @remote_filename
    true
  else
    # TODO report errors according to actual file status

    @error = 'File not found'
    false
  end
end

Generates link which downloads file by Rapidshare API



107
108
109
110
# File 'lib/rapidshare-ext/download.rb', line 107

def download_link
  download_params = { :sub => 'download', :fileid => @fileid, :filename => @remote_filename, :cookie => @api.cookie }
  DOWNLOAD_URL % [ @server_id, @short_host, download_params.to_query ]
end

#downloaded?Boolean

Says whether file has been successfully downloaded.

Returns:

  • (Boolean)


114
115
116
# File 'lib/rapidshare-ext/download.rb', line 114

def downloaded?
  @downloaded
end

#performObject

Downloads file. Calls check method first. When block is given a custom progress bar interpretation can be implemented



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rapidshare-ext/download.rb', line 73

def perform
  # before downloading we have to check if file exists. checkfiles service

  # also gives us information for the download: hostname, file size for

  # progressbar

  return self unless self.check

  file = open(File.join(@downloads_dir, @filename), 'wb')
  block_response = Proc.new do |response|
    downloaded = 0
    total = response.header['content-length'].to_i

    unless total == @filesize
      @error = 'Access denied'
      return self
    end

    response.read_body do |chunk|
      file << chunk
      downloaded += chunk.size
      progress = ((downloaded * 100).to_f / total).round(2)
      yield chunk.size, downloaded, total, progress if block_given?
    end
  end

  RestClient::Request.execute(:method => :get,
                              :url => self.download_link,
                              :block_response => block_response)
  file.close()
  @downloaded = true
  self
end