Class: ImageGetter

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

Class Method Summary collapse

Class Method Details

.images(url:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/getf.rb', line 10

def self.images(url:)
  images = []
  begin
    doc = Nokogiri::HTML(open(url), nil, "UTF-8")
    doc.css("img").each do |img|
      images << img.attr("src")
    end
    doc.css("IMG").each do |img|
      images << img.attr("src")
    end
  rescue Exception => e
    puts "Exception: #{e.to_s}"
  end
  images
end

.largest(url) ⇒ Object

++ normally, the biggest size of image should be the featured image of a article ++



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/getf.rb', line 30

def self.largest(url)
  images = images(url:url)
  max_size = 0
  max_image = nil;
  images.each do |img|
    size = FastImage.size(img)
    if size != nil && size.length == 2

      # ++
      # when a image's height (or width) is way large than width (height)
      # it should not be the feature image of a article 
      # ++
      rate = size[0] * 1.0 / size[1]
      if rate > 2 || rate < 0.5
        next
      end

      # ++
      # when the height or width of image is two small
      # it should not be the feature image of a article also
      # ++
      if size[0] < 20 || size[1] < 20
        next
      end

      size = size[0] * size[1]
      if size > max_size
        max_size = size
        max_image = img
      end
    end
  end
  max_image
end

.on(url:) ⇒ Object



6
7
8
# File 'lib/getf.rb', line 6

def self.on(url:)
  largest(url)
end