Class: Subs::SubDB

Inherits:
Provider show all
Includes:
HashSearcher
Defined in:
lib/subs/providers/sub_db.rb

Instance Attribute Summary

Attributes inherited from Provider

#name, #uri, #user_agent

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ SubDB

Returns a new instance of SubDB.

Yields:

  • (_self)

Yield Parameters:

  • _self (Subs::SubDB)

    the object that the method was called on



8
9
10
11
12
# File 'lib/subs/providers/sub_db.rb', line 8

def initialize(&block)
  agent = "SubDB/1.0 (subs/#{Subs::VERSION}; http://github.com/ForeverZer0/subs)"
  super('TheSubDB.com', URI('http://api.thesubdb.com/'), agent)
  yield self if block_given?
end

Instance Method Details

#all_supported_languagesObject



71
72
73
74
75
76
77
78
# File 'lib/subs/providers/sub_db.rb', line 71

def all_supported_languages
  request = Net::HTTP::Get.new(@uri.path + Subs.query_string(action: 'languages'))
  request['User-Agent'] = @user_agent
  Net::HTTP.start(@uri.host, @uri.port) do |net|
    body = net.request(request).body
    body.split(',').map { |value| Subs::Language.from_alpha2(value) }.compact
  end
end

#compute_hash(path) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/subs/providers/sub_db.rb', line 14

def compute_hash(path)
  size = 64 * 1024
  File.open(path, 'rb') do |io|
    buffer = io.read(size)
    io.seek(-size, IO::SEEK_END)
    buffer << io.read
    return Digest::MD5.hexdigest(buffer)
  end
end

#hash_search(path, *languages) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/subs/providers/sub_db.rb', line 44

def hash_search(path, *languages)
  results = []
  unless File.exist?(path)
    Subs.log.error { "Cannot search, cannot find '#{path}'" }
    return results
  end
  hash = compute_hash(path)
  Subs.log.debug { "Searching #{@name.blue} by hash using '#{hash}'" }
  supported = supported_languages(hash)
  languages.each do |language|
    next unless supported.include?(language)
    name = File.basename(Subs.build_subtitle_path(path, language))
    results << Subs::SearchResult.new(@name, self.class, name, language, path, hash)
  end
  Subs.log.debug { "Found #{results.size.to_s.light_blue} result(s)." }
  results
end

#process_result(io, result) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/subs/providers/sub_db.rb', line 24

def process_result(io, result)
  return false unless super
  hash = result.data
  lang = result.language.alpha2
  request = Net::HTTP::Get.new(@uri.path + Subs.query_string(action: :download, hash: hash, language: lang ))
  request['User-Agent'] = @user_agent
  begin
    Net::HTTP.start(@uri.host, @uri.port) do |net|
      response = net.request(request)
      return false unless response.code.to_i == 200
      io.write(response.body)
      Subs.log.debug { 'Success'.green }
      return true
    end
  rescue
    Subs.log.debug { 'Failed'.red }
    return false
  end
end

#supported_languages(hash) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/subs/providers/sub_db.rb', line 62

def supported_languages(hash)
  request = Net::HTTP::Get.new(@uri.path + Subs.query_string(action: 'search', hash: hash))
  request['User-Agent'] = @user_agent
  Net::HTTP.start(@uri.host, @uri.port) do |net|
    body = net.request(request).body
    body.split(',').map { |value| Subs::Language.from_alpha2(value) }.compact
  end
end