Class: Episode

Inherits:
Object
  • Object
show all
Defined in:
lib/episode.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(episode_no, link) ⇒ Episode

Returns a new instance of Episode.



5
6
7
8
# File 'lib/episode.rb', line 5

def initialize(episode_no, link)
  @number = episode_no
  @link = link
end

Instance Attribute Details

Returns the value of attribute link.



9
10
11
# File 'lib/episode.rb', line 9

def link
  @link
end

#numberObject (readonly)

Returns the value of attribute number.



9
10
11
# File 'lib/episode.rb', line 9

def number
  @number
end

Instance Method Details

#detailsObject



42
43
44
# File 'lib/episode.rb', line 42

def details
  return "Episode #{@number}: #{@link}\n"
end

#download_to(path = ".") ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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