class SubtitleIt::Subdown

Download subtitles

Constants

HOST
HOST_DEV
NO_TOKEN
USER_AGENT

Public Class Methods

new(host = HOST) click to toggle source
# File lib/subtitle_it/subdown.rb, line 20
def initialize(host = HOST)
  @client = XMLRPC::Client.new2(host)
  @token = nil
end
subtitle_languages() click to toggle source
# File lib/subtitle_it/subdown.rb, line 71
def self.subtitle_languages
  LANGS.map do |k, v|
    "#{k} -> #{v}"
  end.join("\n")
end

Private Class Methods

decode_and_unzip(data) click to toggle source
# File lib/subtitle_it/subdown.rb, line 122
def self.decode_and_unzip(data)
  Zlib::GzipReader.new(StringIO.new(XMLRPC::Base64.decode(data))).read
end
result_status_ok?(result) click to toggle source

Returns true if status is OK (ie. in range 200-299) or don't exists.

# File lib/subtitle_it/subdown.rb, line 114
def self.result_status_ok?(result)
  !result.key?('status') || (200...300) === result['status'].to_i
end

Public Instance Methods

download_subtitle(sub) click to toggle source
# File lib/subtitle_it/subdown.rb, line 58
def download_subtitle(sub)
  result = request('DownloadSubtitles', [sub.osdb_id])
  sub.data = self.class.decode_and_unzip(result['data'][0]['data'])
end
imdb_info(movie) click to toggle source
# File lib/subtitle_it/subdown.rb, line 66
def imdb_info(movie)
  result = request('CheckMovieHash', [movie.haxx])
  movie.info = result['data'][movie.haxx] # TODO: Handle if movie 404
end
log_in!() click to toggle source
# File lib/subtitle_it/subdown.rb, line 25
def log_in!
  result = request('LogIn', '', '', '', USER_AGENT)
  @token = result['token'].to_s
end
log_out!() click to toggle source
# File lib/subtitle_it/subdown.rb, line 34
def log_out!
  request('LogOut')
  @token = nil
end
logged_in?() click to toggle source
# File lib/subtitle_it/subdown.rb, line 30
def logged_in?
  !@token.nil? && !@token.empty?
end
search_subtitles(movie, lang_name = nil) click to toggle source
# File lib/subtitle_it/subdown.rb, line 43
def search_subtitles(movie, lang_name = nil)
  args = {
    'sublanguageid' => lang_name || '',
    'moviehash'     => movie.haxx,
    #  'moviebytesize' => movie.size
  }

  result = request('SearchSubtitles', [args])
  return [] unless result['data'] # if no results result['data'] == false
  result['data'].reduce([]) do |subs, sub_info|
    subs << Subtitle.new(info: sub_info)
    subs
  end
end
server_info() click to toggle source
# File lib/subtitle_it/subdown.rb, line 39
def server_info
  request('ServerInfo')
end
upload_subtitle(_movie, _subs) click to toggle source
# File lib/subtitle_it/subdown.rb, line 63
def upload_subtitle(_movie, _subs)
end

Private Instance Methods

prevent_session_expiration() click to toggle source
# File lib/subtitle_it/subdown.rb, line 118
def prevent_session_expiration
  request('NoOperation')
end
request(method, *args) click to toggle source

def Subdown.subtitle_languages

lang_ary = []
OPSUB_LANGS.each_key do |key|
  lang_ary.push( LANGS[key] )
end
lang_ary.sort.inject( "" ) { |str, lang| str << lang + " " }

end

# File lib/subtitle_it/subdown.rb, line 96
def request(method, *args)
  unless NO_TOKEN.include? method
    raise 'Need to be logged in for this.' unless logged_in?
    args = [@token, *args]
  end

  result = @client.call(method, *args)

  unless self.class.result_status_ok?(result)
    # 'status' of the form 'XXX Message'
    raise XMLRPC::FaultException.new(result['status'].to_i,
                                     result['status'][4..-1])
  end

  result
end