Class: Subdl

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

Instance Method Summary collapse

Constructor Details

#initialize(itasa, subtitles_net, stdout, file_system) ⇒ Subdl

Returns a new instance of Subdl.



17
18
19
20
21
22
23
# File 'lib/subdl.rb', line 17

def initialize itasa, subtitles_net, stdout, file_system
  @itasa = Itasa.new itasa
  @subtitles_net = subtitles_net
  @credentials = Credentials.new file_system
  @file_system = file_system
  @stdout = stdout
end

Instance Method Details

#download_sub_for(path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/subdl.rb', line 25

def download_sub_for path
  movie_file = MovieFile.new path, @stdout
  ids = @itasa.search_subtitles(movie_file.search_term)

  if ids.any? 
    ids.each do |id|
      @itasa. *@credentials.read
      @itasa.download_zip id do |zip_contents|
        unpack_subtitle_to zip_contents, movie_file, 'itasa'
      end
    end
  else
    @stdout.puts "No subtitles found on ITASA for: #{path}"
    show    = movie_file.show
    season  = movie_file.season
    episode = movie_file.episode
    
    search_results = SearchResults.new(
      @subtitles_net.search show, season, episode)

    details_page_link = search_results.subtitles.first.href
    page = @subtitles_net.get details_page_link
    details_page = DetailsPage.new page

    zip_contents = @subtitles_net.get details_page.zip_location
    unpack_subtitle_to zip_contents, movie_file, 'subtitles-net'
  end
end

#main(argv) ⇒ Object



11
12
13
14
15
# File 'lib/subdl.rb', line 11

def main argv
  until argv.empty? do
    download_sub_for argv.shift
  end
end

#unpack_subtitle_to(zip_contents, movie_file, source_id) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/subdl.rb', line 54

def unpack_subtitle_to zip_contents, movie_file, source_id
  Zip::Archive.open_buffer(zip_contents) do |archive|
    archive.each do |entry|
      movie_file.save_subtitle entry.read, @file_system, source_id
    end
  end
end