Class: SubtitleIt::Subdown

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

Overview

Download subtitles

Constant Summary collapse

HOST =
'http://api.opensubtitles.org/xml-rpc'
HOST_DEV =
'http://dev.opensubtitles.org/xml-rpc'
USER_AGENT =
"SubtitleIt #{SubtitleIt::VERSION}"
NO_TOKEN =
%w(ServerInfo LogIn)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = HOST) ⇒ Subdown

Returns a new instance of Subdown.



20
21
22
23
# File 'lib/subtitle_it/subdown.rb', line 20

def initialize(host = HOST)
  @client = XMLRPC::Client.new2(host)
  @token = nil
end

Class Method Details

.subtitle_languagesObject



71
72
73
74
75
# File 'lib/subtitle_it/subdown.rb', line 71

def self.subtitle_languages
  LANGS.map do |k, v|
    "#{k} -> #{v}"
  end.join("\n")
end

Instance Method Details

#download_subtitle(sub) ⇒ Object



58
59
60
61
# 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) ⇒ Object



66
67
68
69
# 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!Object



25
26
27
28
# File 'lib/subtitle_it/subdown.rb', line 25

def log_in!
  result = request('LogIn', '', '', '', USER_AGENT)
  @token = result['token'].to_s
end

#log_out!Object



34
35
36
37
# File 'lib/subtitle_it/subdown.rb', line 34

def log_out!
  request('LogOut')
  @token = nil
end

#logged_in?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/subtitle_it/subdown.rb', line 30

def logged_in?
  !@token.nil? && !@token.empty?
end

#search_subtitles(movie, lang_name = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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_infoObject



39
40
41
# File 'lib/subtitle_it/subdown.rb', line 39

def server_info
  request('ServerInfo')
end

#upload_subtitle(_movie, _subs) ⇒ Object



63
64
# File 'lib/subtitle_it/subdown.rb', line 63

def upload_subtitle(_movie, _subs)
end