class Episode

Attributes

number[R]

Public Class Methods

new(episode_no, link) click to toggle source
# File lib/episode.rb, line 5
def initialize(episode_no, link)
  @number = episode_no
  @link = link
end

Public Instance Methods

details() click to toggle source
# File lib/episode.rb, line 42
def details
  return "Episode #{@number}: #{@link}\n"
end
download_to(path = ".") click to toggle source
# File lib/episode.rb, line 11
def download_to(path = ".")
  url_base = @link.split('/')[2]                    # Animeheaven
  url_path = '/'+@link.split('/')[3..-1].join('/')  # Sub Url

  Net::HTTP.start(url_base) do |http|
    response = http.request_head(URI.escape(url_path))
    progress_bar = ProgressBar.create(:progress_mark => "\#", :length => 80, :total => response['content-length'].to_i)
    file = File.new( File.join( path, "Episode #{@number}.mp4.tmp" ), 'w')

    begin
      http.get(URI.escape(url_path)) do |str|
        file.write(str)
        progress_bar.progress += str.length  rescue nil
      end
    rescue Exception
      # Handle SystemExit or Interrupt
      puts "Download incomplete. Deleting file.\n\n"
      file.close
      File.delete(File.join( path, "Episode #{@number}.mp4.tmp" ))
      raise
      exit
    end

    progress_bar.finish
    file.close
    File.rename(file, File.join( path, "Episode #{@number}.mp4" ))
  end

  puts "\n"
end