Class: Subfinder::Parser::Subscene

Inherits:
Object
  • Object
show all
Defined in:
lib/subfinder/parser/subscene.rb

Overview

Find related subtitle file from Subscene list page

Constant Summary collapse

DOMAIN =
'https://subscene.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ Subscene

Returns a new instance of Subscene.



12
13
14
# File 'lib/subfinder/parser/subscene.rb', line 12

def initialize(file_name)
  @file_name = file_name
end

Instance Attribute Details

Returns the value of attribute link.



9
10
11
# File 'lib/subfinder/parser/subscene.rb', line 9

def link
  @link
end

Instance Method Details

#convert_to_code(language) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/subfinder/parser/subscene.rb', line 82

def convert_to_code(language)
  case language
  when 'Farsi/Persian'
    'fa'
  when 'English'
    'en'
  when 'Arabic'
    'ar'
  when 'French'
    'fr'
  when 'Spanish'
    'es'
  else
    language
  end
  # TODO: add other languages codes
end


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/subfinder/parser/subscene.rb', line 68

def create_links_list(page)
  array = []
  doc = Nokogiri::HTML(page)
  doc.xpath('//table//tr').each do |link|
    next if link.xpath('./td/a/span/text()')[0].nil? # language

    array << [convert_to_code(link.xpath('./td/a/span/text()')[0].to_s.strip), # language
              link.xpath('./td/a/span/text()')[1].to_s.strip, # name
              link.xpath('./td/a/@href')[0].to_s.strip] # link
  end
  Logger.debug "array_list: #{array}"
  array
end


28
29
30
31
32
33
34
35
36
# File 'lib/subfinder/parser/subscene.rb', line 28

def find_download_link(page)
  doc = Nokogiri::HTML(page)
  download_link = nil
  doc.css('.download').each do |link|
    download_link = link.xpath('./a/@href').to_s.strip # link
  end
  Logger.debug "download_link: #{download_link}"
  DOMAIN + download_link
end


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/subfinder/parser/subscene.rb', line 38

def find_link(links)
  max_match_point = 0
  winner = ''

  links.each do |link|
    next if link[0] != Config.language

    match_point = match_point_for link[1]
    if match_point > max_match_point
      max_match_point = match_point
      winner = link[2]
    end
  end
  Logger.debug "winner is #{winner}"
  winner
end

#getObject



16
17
18
19
# File 'lib/subfinder/parser/subscene.rb', line 16

def get
  target_uri = find_link(create_links_list(open_online_document(Config.url)))
  Subfinder::Parser::Download.new(find_download_link(open_online_document(DOMAIN + target_uri))).save
end

#match_point_for(target) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/subfinder/parser/subscene.rb', line 55

def match_point_for(target)
  point = 0
  file_name_array = @file_name.split('.')
  # next we want to know if name of the sub is seprataed by . or space
  target_array = target.split('.').size > target.split(' ').size ? target.split('.') : target.split(' ')
  target_array = target_array.map(&:downcase)

  file_name_array.each do |word|
    point += 1 if target_array.include? word.downcase
  end
  (point * 100) / file_name_array.size
end

#open_online_document(url) ⇒ Object



21
22
23
24
25
26
# File 'lib/subfinder/parser/subscene.rb', line 21

def open_online_document(url)
  RestClient.get url
rescue StandardError => e
  Logger.info "Error when connecting to '#{url}'\n Error message: #{e}\n".red
  abort('Check your internet connection or VPN and try again')
end