getf 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/getf +9 -0
  3. data/lib/getf.rb +64 -0
  4. metadata +46 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f272f314fafb7cd7a0ab3fdae40c16c9bc488e47
4
+ data.tar.gz: 221fcbec8ac2bec7514ea9840dfe9034b41aecea
5
+ SHA512:
6
+ metadata.gz: 3dcbe9cfbe6c754cc0a30b1f7a55c856b473ac82414bc80315dd62b54929ae0e3cfb26fa2a33853efa8b88bb5bf749f4a34221176156f0b107ae53235e1dc62a
7
+ data.tar.gz: 52de8fde1fac39ee61576e7a9dbda39d9599812fcf578bb1ed241a42e696c8f767d60e8bc98fc41a8f86b3b0779b08fa1bcf12c7e0d984d85c14ef10f7fbcd1e
@@ -0,0 +1,9 @@
1
+ require 'getf'
2
+
3
+ if ARGV[0] && ARGV[0] != "-h" && ARGV[0] != "--help"
4
+ puts ImageGetter.on(url:ARGV[0])
5
+ else
6
+ puts "*"
7
+ puts "* usage: getf <url_of_article>"
8
+ puts "*"
9
+ end
@@ -0,0 +1,64 @@
1
+ class ImageGetter
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require 'fastimage'
5
+
6
+ def self.on(url:)
7
+ largest(url)
8
+ end
9
+
10
+ def self.images(url:)
11
+ images = []
12
+ begin
13
+ doc = Nokogiri::HTML(open(url), nil, "UTF-8")
14
+ doc.css("img").each do |img|
15
+ images << img.attr("src")
16
+ end
17
+ doc.css("IMG").each do |img|
18
+ images << img.attr("src")
19
+ end
20
+ rescue Exception => e
21
+ puts "Exception: #{e.to_s}"
22
+ end
23
+ images
24
+ end
25
+
26
+ # ++
27
+ # normally, the biggest size of image
28
+ # should be the featured image of a article
29
+ # ++
30
+ def self.largest(url)
31
+ images = images(url:url)
32
+ max_size = 0
33
+ max_image = nil;
34
+ images.each do |img|
35
+ size = FastImage.size(img)
36
+ if size != nil && size.length == 2
37
+
38
+ # ++
39
+ # when a image's height (or width) is way large than width (height)
40
+ # it should not be the feature image of a article
41
+ # ++
42
+ rate = size[0] * 1.0 / size[1]
43
+ if rate > 2 || rate < 0.5
44
+ next
45
+ end
46
+
47
+ # ++
48
+ # when the height or width of image is two small
49
+ # it should not be the feature image of a article also
50
+ # ++
51
+ if size[0] < 20 || size[1] < 20
52
+ next
53
+ end
54
+
55
+ size = size[0] * size[1]
56
+ if size > max_size
57
+ max_size = size
58
+ max_image = img
59
+ end
60
+ end
61
+ end
62
+ max_image
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: getf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Minghe Huang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: it helps you easily get feature image from a url of article
14
+ email: h.minghe@gmail.com
15
+ executables:
16
+ - getf
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/getf
21
+ - lib/getf.rb
22
+ homepage: http://minghe.me
23
+ licenses: []
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.5
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: get feature image of a post like boss
45
+ test_files: []
46
+ has_rdoc: