Module: Tzispa::Helpers::Crawler

Includes:
HashTrans
Defined in:
lib/tzispa/helpers/crawler.rb

Defined Under Namespace

Classes: CrawlerError

Instance Method Summary collapse

Methods included from HashTrans

#hash_fussion!

Instance Method Details

#crawler_save_file(url, dest_file, accept_schemes = ['http', 'https', 'ftp']) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tzispa/helpers/crawler.rb', line 20

def crawler_save_file(url, dest_file, accept_schemes = ['http', 'https', 'ftp'])
  begin
    uri = URI(url)
    raise ArgumentError.new "Inavlid url: #{url}" unless accept_schemes.include?(uri.scheme) && uri.host
    File.delete(dest_file) if File.exist?(dest_file)
    File.open("#{dest_file}", 'wb') do |fo|
      fo.write open(url).read
    end
  rescue => ex
    raise CrawlerError.new "Error in crawler_save_file '#{url}': #{ex.message}"
  end
end

#crawler_table(noko, table_path, columns) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tzispa/helpers/crawler.rb', line 66

def crawler_table(noko, table_path, columns)
  Hash.new.tap { |sections|
    htmee = HTMLEntities.new(:expanded)
    noko = noko.xpath(table_path)
    colspans = "td[@colspan=\"#{columns}\"]"
    if noko.xpath(colspans).count == 0
      noko.collect { |row|
        dterm = htmee.decode(row.at_xpath('td[1]')&.content).gsub(/\n|\r|\t/,' ').strip
        unless dterm.empty?
          sections[dterm] ||= Array.new
          sections[dterm] << (2..columns).map { |i|
              ReverseMarkdown.convert(
                htmee.decode(row.at_xpath("td[#{i}]")&.children&.to_s || row.at_xpath("td[#{i}]")&.to_s).strip, unknown_tags: :bypass
              ).gsub(/\r|\t/,' ').strip
          }.join('\n')
        end
      }
    else
      current_section = nil
      noko.collect { |row|
         unless row.xpath(colspans)&.text.strip.empty?
           current_section = htmee.decode(row.xpath("td[@colspan=\"#{columns}\"]").text).gsub(/\n|\r|\t/,' ').strip
           sections[current_section] ||= Array.new
         else
           if current_section
             sections[current_section] << (1..columns).map { |i|
                ReverseMarkdown.convert(
                  htmee.decode(row.at_xpath("td[#{i}]")&.children&.to_s.strip || row.at_xpath("td[#{i}]")&.to_s.strip), unknown_tags: :bypass
                ).strip
             }.join(': ')
           end
         end
       }
    end
  }
end

#crawler_table_to_dl(noko, table_path, columns, excluded_terms: [], fussion_terms: {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tzispa/helpers/crawler.rb', line 42

def crawler_table_to_dl(noko, table_path, columns, excluded_terms: [], fussion_terms:{})
  String.new.tap { |content|
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new)
    sections = crawler_table(noko, table_path, columns)
    hash_fussion! sections, fussion_terms
    unless sections.empty?
      content << '<dl>'
      sections.sort.each { |key, value|
        unless key.empty? || value.empty? || excluded_terms.include?(UnicodeUtils.downcase key)
          content << "<dt>#{key}</dt>"
          if value.is_a?(Array) && value.count > 1
            content << '<ul>' << value.map { |item| "<li>#{markdown.render item}</li>"}.join("\n") << '</ul>'
          elsif value.is_a?(Array) && value.count == 1
            content << "<dd>#{markdown.render value.first}</dd>"
          else
            content << "<dd>#{markdown.render value}</dd>"
          end
        end
      }
      content << "</dl>"
    end
  }
end

#crawler_table_to_list(noko, table_path, excluded_terms: []) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/tzispa/helpers/crawler.rb', line 103

def crawler_table_to_list(noko, table_path, excluded_terms: [])
  htmee = HTMLEntities.new(:expanded)
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new)
  String.new.tap { |list|
    list << '<ul>'
    list << Array.new.tap { |lines|
      noko.xpath(table_path).collect { |td|
        line = if td.xpath('table/tr/td').count > 0
          crawler_table(td, 'table/tr', 2)
        else
          raw_ln = ReverseMarkdown.convert(td&.children&.to_s.strip, unknown_tags: :bypass)
          raw_ln unless raw_ln.empty? || excluded_terms.include?(raw_ln)
        end
        if line&.is_a? String
          lines << "<li>#{htmee.decode(markdown.render line)}</li>"
        elsif line.is_a? Hash
          line&.map { |key, value|
            lines << "<li><strong>#{key}</strong>: #{value} </li>" unless excluded_terms.include?(key) || excluded_terms.include?(value)
          }.join("\n")
        else
          line&.map { |v|
            lines << "<li>#{v}</li>" unless excluded_terms.include?(v)
          }.join("\n")
        end
      }
    }.join("\n")
    list << '</ul>'
  }
end

#crawler_to_markdown(source) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/tzispa/helpers/crawler.rb', line 33

def crawler_to_markdown(source)
  begin
    source = source.read if source.respond_to? :read
    htmee = HTMLEntities.new
    ReverseMarkdown.convert(htmee.decode(source).strip, unknown_tags: :bypass)
  rescue Encoding::UndefinedConversionError
  end
end