Class: Addic7ed::Episode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Episode

Returns a new instance of Episode.



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

def initialize(filename)
  @filename = Addic7ed::Filename.new(filename)
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

Instance Method Details

#best_subtitle(lang = 'fr') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/addic7ed/episode.rb', line 38

def best_subtitle(lang = 'fr')
  raise LanguageNotSupported unless LANGUAGES[lang]
  unless @best_subtitle and @best_subtitle[lang]
    @best_subtitle ||= {}
    subtitles(lang).each do |sub|
      if sub.status == 'Completed' and (sub.version == @filename.group or COMPATIBILITY_720P[sub.version] == @filename.group)
        @best_subtitle[lang] = sub unless @best_subtitle[lang] and (@best_subtitle[lang].downloads > sub.downloads or @best_subtitle[lang].via == 'http://addic7ed.com')
      end
    end
    raise NoSubtitleFound unless @best_subtitle[lang]
  end
  return @best_subtitle[lang]
end

#download_best_subtitle!(lang = 'fr', redirect_limit = 8) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/addic7ed/episode.rb', line 52

def download_best_subtitle!(lang = 'fr', redirect_limit = 8)
  raise WTFError.new('Too many HTTP redirects') if redirect_limit == 0
  begin
    uri = URI(best_subtitle(lang).url)
    response = Net::HTTP.start(uri.hostname, uri.port) do |http|
      request = Net::HTTP::Get.new(uri.request_uri)
      # Addic7ed needs the Referer to be correct. User-agent is just here to fake a real browser request, because we never know...
      request['Referer'] = url(lang)
      request['User-Agent'] = USER_AGENTS.sample
      http.request(request)
    end
  rescue
    raise DownloadError
  end
  if response.kind_of?(Net::HTTPRedirection)
    # Addic7ed is serving redirection URL not-encoded.
    # Ruby does not support it yet (see http://bugs.ruby-lang.org/issues/7396)
     best_subtitle(lang).url = URI.escape(response['location'])
     raise DownloadLimitReached if /^\/downloadexceeded.php/.match best_subtitle(lang).url
     download_best_subtitle!(lang, redirect_limit - 1)
  else
    begin
      open "#{filename}".gsub(/\.\w{3}$/, '.srt'), 'w' do |f|
        f << response.body
      end
    rescue
      raise SubtitleCannotBeSaved
    end
  end
end

#subtitles(lang = 'fr') ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/addic7ed/episode.rb', line 20

def subtitles(lang = 'fr')
  raise LanguageNotSupported unless LANGUAGES[lang]
  unless @subtitles and @subtitles[lang]
    @subtitles ||= {}
    @subtitles[lang] ||= []
    response = Net::HTTP.get_response(URI(url(lang)))
    raise EpisodeNotFound if response.body == " "
    doc = Nokogiri::HTML(response.body)
    raise NoSubtitleFound unless doc.css('select#filterlang ~ font[color="yellow"]').empty?
    sublist_node = doc.css('#container95m table.tabel95 table.tabel95')
    raise NoSubtitleFound if sublist_node.size == 0
    sublist_node.each do |sub_node|
      @subtitles[lang] << parse_subtitle_node(sub_node, lang)
    end
  end
  @subtitles[lang]
end

#url(lang = 'fr') ⇒ Object



14
15
16
17
18
# File 'lib/addic7ed/episode.rb', line 14

def url(lang = 'fr')
  raise LanguageNotSupported unless LANGUAGES[lang]
  @localized_urls ||= {}
  @localized_urls[lang] ||= "http://www.addic7ed.com/serie/#{@filename.encoded_showname}/#{@filename.season}/#{@filename.episode}/#{LANGUAGES[lang][:id]}"
end