Class: SubtitleIt::Subdown

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

Constant Summary collapse

HOST =
"http://api.opensubtitles.org/xml-rpc"
HOST_DEV =
"http://dev.opensubtitles.org/xml-rpc"
USER_AGENT =

USER_AGENT = “SubDownloader #VERSION::STRING

"SubtitleIt #{SubtitleIt::VERSION::STRING}"
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



74
75
76
77
78
# File 'lib/subtitle_it/subdown.rb', line 74

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

Instance Method Details

#download_subtitle(sub) ⇒ Object



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

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



69
70
71
72
# File 'lib/subtitle_it/subdown.rb', line 69

def imdb_info(movie)
  result = request('CheckMovieHash', [movie.haxx])
  movie.info = result['data'][movie.haxx] # TODO: Handle if no result for movie
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
57
58
59
# File 'lib/subtitle_it/subdown.rb', line 43

def search_subtitles(movie, lang_name=nil)
  # lang_name, lang_code = LANGS[lang_id.to_sym] if lang_id
  # print "Searching for "
  # puts lang_id ? lang_name + "..." : "all languages."
  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'].inject([]) 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



66
67
# File 'lib/subtitle_it/subdown.rb', line 66

def upload_subtitle(movie, subs)
end