Class: Suby::Downloader::TVSubtitles

Inherits:
Suby::Downloader show all
Defined in:
lib/suby/downloader/tvsubtitles.rb

Constant Summary collapse

SITE =
'www.tvsubtitles.net'
FORMAT =
:zip
SEARCH_URL =
'/search.php'
SUBTITLE_TYPES =
[:tvshow]
SHOW_URLS =

cache

{}
SHOW_PAGES =
{}

Constants inherited from Suby::Downloader

DOWNLOADERS

Instance Attribute Summary

Attributes inherited from Suby::Downloader

#episode, #file, #lang, #season, #show, #video_data

Instance Method Summary collapse

Methods inherited from Suby::Downloader

#convert_to_utf8_from_latin1, #download, #encode, #extract, #find_nfo_file, #get, #get_redirection, #http, #imdbid, inherited, #initialize, #post, #sub_extension, #sub_name, #subtitles, #success_message, #support_video_type?, #to_s, #xmlrpc

Constructor Details

This class inherits a constructor from Suby::Downloader

Instance Method Details

#clean_show_name(show_name) ⇒ Object

“Show (2009-2011)” => “Show”



13
14
15
# File 'lib/suby/downloader/tvsubtitles.rb', line 13

def clean_show_name show_name
  show_name.sub(/ \(\d{4}-\d{4}\)$/, '')
end

#download_urlObject



86
87
88
# File 'lib/suby/downloader/tvsubtitles.rb', line 86

def download_url
  URI.escape '/' + get_redirection(subtitles_url.sub('subtitle', 'download'))
end

#episode_urlObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/suby/downloader/tvsubtitles.rb', line 52

def episode_url
  @episode_url ||= begin
    SHOW_PAGES[show] ||= Nokogiri(get(season_url))
    has_season?

    url = nil
    find_episode_row.children.find { |td|
      td.children.find { |a|
        a.name == 'a' and a[:href].start_with?('episode') and url = a[:href]
      }
    }
    unless url =~ /^episode-(\d+)\.html$/
      raise "invalid episode url: #{episode_url}"
    end

    "/episode-#{$1}-#{lang}.html"
  end
end

#find_episode_rowObject

Raises:



43
44
45
46
47
48
49
50
# File 'lib/suby/downloader/tvsubtitles.rb', line 43

def find_episode_row
  row = SHOW_PAGES[show].css('div.left_articles table tr').find { |tr|
    tr.children.find { |td| td.name == 'td' and
                            td.text =~ /\A#{season}x0?#{episode}\z/ }
  }
  raise NotFoundError, "episode not found" unless row
  row
end

#has_season?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/suby/downloader/tvsubtitles.rb', line 35

def has_season?
  season_text = "Season #{season}"
  bs = SHOW_PAGES[show].css('div.left_articles p.description b')
  unless bs.find { |b| b.text == season_text }
    raise NotFoundError, "season not found"
  end
end

#season_urlObject



31
32
33
# File 'lib/suby/downloader/tvsubtitles.rb', line 31

def season_url
  show_url.sub(/\.html$/, "-#{season}.html")
end

#show_urlObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/suby/downloader/tvsubtitles.rb', line 17

def show_url
  SHOW_URLS[show] ||= begin
    results = Nokogiri(post(SEARCH_URL, 'q' => show))
    a = results.css('ul li div a').find { |a|
      clean_show_name(a.text).casecmp(show) == 0
    }
    raise NotFoundError, "show not found" unless a
    url = a[:href]

    raise 'invalid show url' unless /^\/tvshow-\d+\.html$/ =~ url
    url
  end
end

#subtitles_urlObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/suby/downloader/tvsubtitles.rb', line 71

def subtitles_url
  @subtitles_url ||= begin
    subtitles = Nokogiri(get(episode_url))

    # TODO: choose 720p or most downloaded instead of first found
    a = subtitles.css('div.left_articles a').find { |a|
      a.name == 'a' and a[:href].start_with?('/subtitle')
    }
    raise NotFoundError, "no subtitles available" unless a
    url = a[:href]
    raise 'invalid subtitle url' unless url =~ /^\/subtitle-(\d+)\.html/
    url
  end
end