Class: Srt::Downloader
- Inherits:
-
Object
- Object
- Srt::Downloader
- Includes:
- HTTParty
- Defined in:
- lib/srt-downloader.rb
Instance Method Summary collapse
- #download ⇒ Object
- #get_result ⇒ Object
-
#initialize(filename) ⇒ Downloader
constructor
A new instance of Downloader.
- #post ⇒ Object
- #vhash ⇒ Object
- #write_file ⇒ Object
Constructor Details
#initialize(filename) ⇒ Downloader
Returns a new instance of Downloader.
10 11 12 13 |
# File 'lib/srt-downloader.rb', line 10 def initialize(filename) @filename = filename @res end |
Instance Method Details
#download ⇒ Object
15 16 17 18 |
# File 'lib/srt-downloader.rb', line 15 def download get_result write_file end |
#get_result ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/srt-downloader.rb', line 20 def get_result @post_response = post latest_result = @post_response[-1] # latest one latest_result_link = latest_result["Files"][0]["Link"] @get_response = HTTParty.get(latest_result_link) # http://stackoverflow.com/questions/13393725/ruby-how-to-get-the-name-of-a-file-with-open-uri @download_file_name = @get_response.headers["content-disposition"].match(/filename=(\"?)(.+)\1/)[2] end |
#post ⇒ Object
38 39 40 41 |
# File 'lib/srt-downloader.rb', line 38 def post = { :body => {'filehash' => vhash , 'pathinfo' => @filename , 'format' => 'json', 'lang' => 'Chn'} } @results = self.class.post('/api/subapi.php', ) end |
#vhash ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/srt-downloader.rb', line 43 def vhash file_size = File.size?(@filename) offset = [] offset[0] = 4096 offset[1] = file_size / 3 * 2 offset[2] = file_size / 3 offset[3] = file_size - 8192 arr = [] offset.each do |position| vblock = IO.binread(@filename, 4096, position) # length , offset md5 = Digest::MD5.hexdigest(vblock) arr << md5 end hash = arr.join(";") return hash end |
#write_file ⇒ Object
31 32 33 34 35 36 |
# File 'lib/srt-downloader.rb', line 31 def write_file File.open(@download_file_name, "wb") do |f| f.write @get_response.parsed_response puts "Download : #{@download_file_name} complete!" end end |