Class: Rapidshare::Download
- Inherits:
-
Object
- Object
- Rapidshare::Download
- Defined in:
- lib/rapidshare/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.
Constant Summary
- DOWNLOAD_URL =
'https://rs%s%s.rapidshare.com/cgi-bin/rsapi.cgi?%s'
Instance Attribute Summary (collapse)
-
- (Object) api
readonly
Returns the value of attribute api.
-
- (Object) downloaded
readonly
Returns the value of attribute downloaded.
-
- (Object) downloads_dir
readonly
Returns the value of attribute downloads_dir.
-
- (Object) error
readonly
Returns the value of attribute error.
-
- (Object) fileid
readonly
Returns the value of attribute fileid.
-
- (Object) filename
readonly
Returns the value of attribute filename.
-
- (Object) filesize
readonly
Returns the value of attribute filesize.
-
- (Object) server_id
readonly
Returns the value of attribute server_id.
-
- (Object) short_host
readonly
Returns the value of attribute short_host.
-
- (Object) url
readonly
Returns the value of attribute url.
Instance Method Summary (collapse)
-
- (Object) check
Checks if file exists (using checkfiles service) and gets data necessary for download.
-
- (Object) download_link
Generates link which downloads file by Rapidshare API.
-
- (Boolean) downloaded?
Says whether file has been successfully downloaded.
-
- (Download) initialize(url, api, options = {})
constructor
Options:
-
filename (optional) - specifies filename under which the file will be.
-
-
- (Object) perform
Downloads file.
Constructor Details
- (Download) initialize(url, api, options = {})
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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rapidshare/download.rb', line 22 def initialize(url, api, = {}) @url = url @api = api @filename = [:save_as] @downloads_dir = [: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 end |
Instance Attribute Details
- (Object) api (readonly)
Returns the value of attribute api
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def api @api end |
- (Object) downloaded (readonly)
Returns the value of attribute downloaded
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def downloaded @downloaded end |
- (Object) downloads_dir (readonly)
Returns the value of attribute downloads_dir
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def downloads_dir @downloads_dir end |
- (Object) error (readonly)
Returns the value of attribute error
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def error @error end |
- (Object) fileid (readonly)
Returns the value of attribute fileid
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def fileid @fileid end |
- (Object) filename (readonly)
Returns the value of attribute filename
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def filename @filename end |
- (Object) filesize (readonly)
Returns the value of attribute filesize
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def filesize @filesize end |
- (Object) server_id (readonly)
Returns the value of attribute server_id
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def server_id @server_id end |
- (Object) short_host (readonly)
Returns the value of attribute short_host
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def short_host @short_host end |
- (Object) url (readonly)
Returns the value of attribute url
13 14 15 |
# File 'lib/rapidshare/download.rb', line 13 def url @url end |
Instance Method Details
- (Object) check
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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rapidshare/download.rb', line 41 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_f @server_id = response[:server_id] @short_host = response[:short_host] true else # TODO report errors according to actual file status @error = "File not found" false end end |
- (Object) download_link
Generates link which downloads file by Rapidshare API
94 95 96 97 |
# File 'lib/rapidshare/download.rb', line 94 def download_link download_params = { :sub => 'download', :fileid => @fileid, :filename => @filename, :cookie => @api. } DOWNLOAD_URL % [ @server_id, @short_host, download_params.to_query ] end |
- (Boolean) downloaded?
Says whether file has been successfully downloaded.
101 102 103 |
# File 'lib/rapidshare/download.rb', line 101 def downloaded? @downloaded end |
- (Object) perform
Downloads file. Calls check method first.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rapidshare/download.rb', line 61 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') = ProgressBar.new(@filename, @filesize) .file_transfer_mode Curl::Easy.perform(self.download_link) do |curl| curl.on_progress do |dl_total, dl_now| .set(dl_now) dl_now <= dl_total end curl.on_body do |data| file << data data.length end curl.on_complete { .finish } end file.close @downloaded = true self end |