Class: Undertexter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Undertexter

Returns a new instance of Undertexter.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/undertexter.rb', line 13

def initialize(options)
  @options = {
    :language => {
      :swedish => "soek",
      :english => "eng_search"
    },
    :preferred_language => options[:language]
  }
  
  # If a non existing language is being used, swedish will be the default
  lang = @options[:language][options[:language]] || @options[:language][:swedish]
  
  @base_details = "http://www.undertexter.se/?p=#{lang}&add=arkiv&str="
  @subtitles = []
end

Instance Attribute Details

#base_detailsObject

Returns the value of attribute base_details.



11
12
13
# File 'lib/undertexter.rb', line 11

def base_details
  @base_details
end

#raw_dataObject

Returns the value of attribute raw_data.



11
12
13
# File 'lib/undertexter.rb', line 11

def raw_data
  @raw_data
end

#search_stringObject

Returns the value of attribute search_string.



11
12
13
# File 'lib/undertexter.rb', line 11

def search_string
  @search_string
end

#subtitlesObject

Returns the value of attribute subtitles.



11
12
13
# File 'lib/undertexter.rb', line 11

def subtitles
  @subtitles
end

Class Method Details

.find(search_string, options = {:language => :swedish}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/undertexter.rb', line 29

def self.find(search_string, options = {:language => :swedish})
  this = self.new(options)
  this.search_string = search_string
  
  # Downloading the page
  this.get!
  
  # If something went wrong, like a timeout, {raw_data} could be nil
  return [] if this.raw_data.nil?
  
  this.parse!
  
  this.build!
  
  return this.subtitles
end

Instance Method Details

#build!Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/undertexter.rb', line 80

def build!
  @block.each do |movie|
    next unless movie.count == 5
    @subtitles << SContainer::Subtitle.new({
      :cds => movie[0].match(/\d+/)[0].to_i,
      :downloads => movie[1].match(/\d+$/)[0].to_i,
      :title => movie[2],
      :details => movie[3],
      :movie_title => movie[4],
      :language => @options[:preferred_language]
    })
  end
end

#get!Object



94
95
96
97
# File 'lib/undertexter.rb', line 94

def get!
  @raw_data = RestClient.get(url, :timeout => 10) rescue nil
  @raw_data = Iconv.conv("utf-8","ISO-8859-1", @raw_data)
end

#parse!Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/undertexter.rb', line 46

def parse!
  # Example output
  # [["(1 cd)", "Nedladdningar: 11891", "Avatar (2009) PROPER DVDSCR XviD-MAXSPEED", "http://www.undertexter.se/?p=undertext&id=19751", "Avatar"]]
  
  doc = Nokogiri::HTML(@raw_data)
  @block = []
  
  tbody = doc.css("table").to_a.reject do |i| 
    ! i.content.match(/Nedladdningar/i) or i.css("table").any?
  end.sort_by do |inner| 
    inner.css("table").count 
  end.first
  
  return if tbody.nil?
  
  tbody = tbody.css("tr").select do |tr|
    tr.to_s.match(/id=\d+/)  or tr.to_s.match(/Nedladdningar/i)
  end.each_slice(2) do |first, last|
    length = @block.length
    @block[length] = [] if @block[length].nil?
    line = last.content.split(/\n/).map(&:strip)
    value = first.at_css("a")
    
    @block[length] << line[1]             # (cd 1)
    @block[length] << line[3]             # Nedladdningar: 11891
    @block[length] << line[4]             # "Avatar (2009) PROPER DVDSCR XviD-MAXSPEED"
    @block[length] << value.attr("href")  # http://www.undertexter.se/?p=undertext&id=19751
    @block[length] << value.attr("title") # Avatar
    @block[length].map!(&:strip)
  end
rescue StandardError => error
  raise SourceHasBeenChangedError.new(error, url)
end

#urlObject



99
100
101
# File 'lib/undertexter.rb', line 99

def url
  @base_details + CGI.escape(search_string)
end