class Addic7ed::Parser

Public Class Methods

new(episode, lang) click to toggle source
# File lib/addic7ed/parser.rb, line 8
def initialize(episode, lang)
  @episode, @lang = episode, lang
  @subtitles = []
end

Public Instance Methods

extract_subtitles() click to toggle source
# File lib/addic7ed/parser.rb, line 13
def extract_subtitles
  @dom = subtitles_page_dom
  check_subtitles_presence
  parse_subtitle_nodes_list
  @subtitles
end

Protected Instance Methods

check_subtitles_presence() click to toggle source
# File lib/addic7ed/parser.rb, line 33
def check_subtitles_presence
  raise NoSubtitleFound unless @dom.css('select#filterlang ~ font[color="yellow"]').empty?
end
extract_comment(sub_node) click to toggle source
# File lib/addic7ed/parser.rb, line 99
def extract_comment(sub_node)
    comment_node = sub_node.css('tr:nth-child(2) td.newsDate').first
    raise Addic7ed::ParsingError unless comment_node
    comment_node.text.gsub(/<img[^>]+\>/i, "")
end
extract_downloads(sub_node) click to toggle source
# File lib/addic7ed/parser.rb, line 93
def extract_downloads(sub_node)
  downloads_node = sub_node.css('tr:nth-child(4) td.newsDate').first
  raise Addic7ed::ParsingError unless downloads_node
  /(?<downloads>\d*) Downloads/.match(downloads_node.text)[:downloads]
end
extract_hi(sub_node) click to toggle source
# File lib/addic7ed/parser.rb, line 87
def extract_hi(sub_node)
  hi_node = sub_node.css('tr:nth-child(4) td.newsDate img').last
  raise Addic7ed::ParsingError unless hi_node
  !hi_node.attribute("title").nil?
end
extract_language(sub_node) click to toggle source
# File lib/addic7ed/parser.rb, line 64
def extract_language(sub_node)
  language_node = sub_node.css('.language').first
  raise Addic7ed::ParsingError unless language_node
  language_node.text.gsub(/\A\W*/, '').gsub(/[^\w\)]*\z/, '')
end
extract_source(sub_node) click to toggle source
# File lib/addic7ed/parser.rb, line 82
def extract_source(sub_node)
  source_node = sub_node.css('tr:nth-child(3) td:first-child a').first
  source_node['href'] if source_node
end
extract_status(sub_node) click to toggle source
# File lib/addic7ed/parser.rb, line 70
def extract_status(sub_node)
  status_node = sub_node.css('tr:nth-child(3) td:nth-child(4) b').first
  raise Addic7ed::ParsingError unless status_node
  status_node.text.strip
end
extract_url(sub_node) click to toggle source
# File lib/addic7ed/parser.rb, line 76
def extract_url(sub_node)
  url_node = sub_node.css('a.buttonDownload').last
  raise Addic7ed::ParsingError unless url_node
  'http://www.addic7ed.com' + url_node['href']
end
extract_version(sub_node) click to toggle source
# File lib/addic7ed/parser.rb, line 58
def extract_version(sub_node)
  version_node = sub_node.css('.NewsTitle').first
  raise Addic7ed::ParsingError unless version_node
  version_node.text
end
parse_subtitle_node(sub_node) click to toggle source
# File lib/addic7ed/parser.rb, line 45
def parse_subtitle_node(sub_node)
  Addic7ed::Subtitle.new(
    version:   extract_version(sub_node),
    language:  extract_language(sub_node),
    status:    extract_status(sub_node),
    url:       extract_url(sub_node),
    source:    extract_source(sub_node),
    hi:        extract_hi(sub_node),
    downloads: extract_downloads(sub_node),
    comment:   extract_comment(sub_node)
  )
end
parse_subtitle_nodes_list() click to toggle source
# File lib/addic7ed/parser.rb, line 37
def parse_subtitle_nodes_list
  sublist_node = @dom.css('#container95m table.tabel95 table.tabel95')
  raise NoSubtitleFound if sublist_node.size == 0
  sublist_node.each do |sub_node|
    @subtitles << parse_subtitle_node(sub_node)
  end
end
subtitles_page_dom() click to toggle source
# File lib/addic7ed/parser.rb, line 22
def subtitles_page_dom
  uri = URI(@episode.url(@lang))
  response = Net::HTTP.start(uri.hostname, uri.port) do |http|
    request = Net::HTTP::Get.new(uri.request_uri)
    request["User-Agent"] = USER_AGENTS.sample
    http.request(request)
  end
  raise EpisodeNotFound unless response.body
  Oga.parse_html(response.body)
end