article_crux 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +10 -0
- data/article_crux.gemspec +41 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/article_crux.rb +117 -0
- data/lib/article_crux/version.rb +3 -0
- metadata +156 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4713c573646d3a81816234e8c17272a388dc5c1f
|
4
|
+
data.tar.gz: d982b33fc5efd04c7318f9307cfcdda0ea6b0927
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4e2b7ed4066f1339c256d54414ac17949ca0a9350c46c82dff244d2fe744778780321858dce11b83c6780ed5673d65f83e3528f40ffb03cc348dfdcf5b51823
|
7
|
+
data.tar.gz: cb82bf19afbca30dfd8b96b50c577e8f4e9998e54db04f55f8c4491da72106ed88adf6d9ef1fca92bff369fc6a7411ab451edac87ae86e4507fbdba7a99beb65
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 amitsaxena
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# ArticleCrux
|
2
|
+
|
3
|
+
The gem scrapes HTML of a URL and returns the title and cover image which most likey represents the article. It also returns an array of tags. The gem can be useful in scenarios where you want to display a short summary of the article, before the end-user lands on the actual article.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'article_crux'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install article_crux
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
<pre lang="ruby"><code>
|
23
|
+
require 'article_crux'
|
24
|
+
|
25
|
+
ArticleCrux.fetch("https://techcrunch.com/2017/04/18/facebook-announces-react-fiber-a-rewrite-of-its-react-framework/")
|
26
|
+
=> {:image=>"https://tctechcrunch2011.files.wordpress.com/2017/04/image-uploaded-from-ios-1.jpg?w=764&h=400&crop=1", :title=>"Facebook announces React Fiber, a rewrite of its React framework", :tags=>["developers", "F82017", "Facebook", "Javascript", "react", "React Fiber"]}
|
27
|
+
|
28
|
+
# In case you want to pass a custom user Agent (server can whitelist specific User agents, as well as you might me blocked, and at times a server might return a different for different user agent)
|
29
|
+
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0"
|
30
|
+
ArticleCrux.fetch("https://techcrunch.com/2017/04/18/facebook-announces-react-fiber-a-rewrite-of-its-react-framework/", user_agent)
|
31
|
+
=> {:image=>"https://tctechcrunch2011.files.wordpress.com/2017/04/image-uploaded-from-ios-1.jpg?w=764&h=400&crop=1", :title=>"Facebook announces React Fiber, a rewrite of its React framework", :tags=>["developers", "F82017", "Facebook", "Javascript", "react", "React Fiber"]}
|
32
|
+
</code></pre>
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/amitsaxena/article_crux.
|
37
|
+
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
42
|
+
|
43
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'article_crux/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "article_crux"
|
8
|
+
spec.version = ArticleCrux::VERSION
|
9
|
+
spec.authors = ["amitsaxena"]
|
10
|
+
spec.email = ["amit83.saxena@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Returns crux of an article, which includes title and the most likely image that represents the article}
|
13
|
+
spec.description = %q{The gem scrapes HTML of a URL to return the title and cover image which most likey represents the article. It also returns an array of tags.}
|
14
|
+
spec.homepage = "https://github.com/amitsaxena/article_crux"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
36
|
+
|
37
|
+
spec.add_dependency "fastimage", "~> 2.0"
|
38
|
+
spec.add_dependency "nokogiri", "~> 1.6"
|
39
|
+
spec.add_dependency "httparty", "~> 0.12"
|
40
|
+
spec.add_dependency 'addressable', "~> 2.3"
|
41
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "article_crux"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/article_crux.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require "article_crux/version"
|
2
|
+
require 'httparty'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'fastimage'
|
5
|
+
require 'addressable'
|
6
|
+
require 'open-uri'
|
7
|
+
|
8
|
+
module ArticleCrux
|
9
|
+
def self.fetch(url, user_agent="ArticleCrux(https://github.com/amitsaxena/article_crux)")
|
10
|
+
url = (url =~ /^(http|https):\/\/(.)*/i) ? url : "http://#{url}"
|
11
|
+
probe = HTTParty.head(url, headers: {"User-Agent" => user_agent})
|
12
|
+
if(probe.content_type && probe.content_type.split('/')[0] == "image")
|
13
|
+
return {:image => url, :title => nil, :tags => []}
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
res = HTTParty.get(url, headers: {"User-Agent" => user_agent})
|
18
|
+
raise "Unable to crawl URL" if res.code != 200
|
19
|
+
doc = Nokogiri::HTML(res)
|
20
|
+
rescue
|
21
|
+
doc = Nokogiri::HTML(open(url, "User-Agent" => user_agent))
|
22
|
+
end
|
23
|
+
|
24
|
+
og_url = doc.search("//meta[@property='og:url' or @name='og:url']")
|
25
|
+
if (!og_url.empty? && !str_blank?(og_url[0]["content"]) && (og_url[0]["content"] =~ /^(http|https):\/\/(.)*/i) && (url != og_url[0]["content"]))
|
26
|
+
begin
|
27
|
+
res = HTTParty.get(og_url[0]["content"], headers: {"User-Agent" => user_agent})
|
28
|
+
raise "Unable to crawl URL" if res.code != 200
|
29
|
+
doc = Nokogiri::HTML(res)
|
30
|
+
rescue
|
31
|
+
# If the og:url is faulty or doesn't return a 200 response, use the original url for meta data scraping
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
og_image = doc.search("//meta[@property='og:image' or @name='og:image']")
|
36
|
+
og_images = []
|
37
|
+
if !og_image.empty?
|
38
|
+
og_image.each do |ogi|
|
39
|
+
if !str_blank?(ogi["content"])
|
40
|
+
image = ogi["content"]
|
41
|
+
if (image =~ /^\/\/(.)*/)
|
42
|
+
uri = URI.parse(url)
|
43
|
+
image = "#{uri.scheme}:#{image}"
|
44
|
+
elsif (image =~ /^\/(.)*/)
|
45
|
+
uri = URI.parse(url)
|
46
|
+
image = File.join("#{uri.scheme}://#{uri.host}", image)
|
47
|
+
end
|
48
|
+
og_images << image
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Try to get the best image based on heuristics
|
54
|
+
image = get_best_image(og_images)
|
55
|
+
|
56
|
+
# If og:image is an invalid image (or extremely small), fall back to <img> tags
|
57
|
+
og_size = FastImage.size(Addressable::URI.escape(image)) if !str_blank?(image)
|
58
|
+
if (str_blank?(image) || og_size.nil? || (og_size[0] < 100 && og_size[1] < 100))
|
59
|
+
image = nil # reset image so that it doesn't show up in response
|
60
|
+
image_paths = []
|
61
|
+
page_images = doc.search("//img")
|
62
|
+
page_images.each do |page_image|
|
63
|
+
next if (str_blank?(page_image["src"]))
|
64
|
+
clip_image = page_image["src"]
|
65
|
+
if (clip_image && !(clip_image =~ /^(http|https):\/\/(.)*/i))
|
66
|
+
base = doc.search("//base")[0]
|
67
|
+
base_url = base["href"] if (!base.nil? && !str_blank?(base["href"]))
|
68
|
+
uri = URI.parse(url)
|
69
|
+
if (clip_image =~ /^\/(.)*/)
|
70
|
+
base_url = "#{uri.scheme}://#{uri.host}" if str_blank?(base_url)
|
71
|
+
else
|
72
|
+
base_url = "#{uri.scheme}://#{uri.host}#{uri.path[%r{^(.*[\/])}]}" if str_blank?(base_url)
|
73
|
+
end
|
74
|
+
clip_image = File.join(base_url, clip_image)
|
75
|
+
end
|
76
|
+
image_paths << clip_image
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
og_title = doc.search("//meta[@property='og:title' or @name='og:title']")
|
81
|
+
if (!og_title.empty? && !str_blank?(og_title[0]["content"]))
|
82
|
+
clip_title = og_title[0]["content"]
|
83
|
+
else
|
84
|
+
page_title = doc.search("//title")[0]
|
85
|
+
clip_title = page_title.text if !page_title.nil?
|
86
|
+
end
|
87
|
+
|
88
|
+
tags = []
|
89
|
+
possible_tags = doc.xpath('//meta[contains(@name, "tag") or contains(@name, "keyword") or contains(@property, "tag") or contains(@property, "keyword")]')
|
90
|
+
possible_tags.each{|e| tags << e["content"].split(',') if !str_blank?(e["content"])}
|
91
|
+
tags = tags.flatten.map(&:strip).uniq
|
92
|
+
|
93
|
+
res = {:image => image, :title => clip_title, :tags => tags}
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.get_best_image(images)
|
97
|
+
return nil if images.empty?
|
98
|
+
return images[0] if (images.size == 1)
|
99
|
+
# reject logo or similar images
|
100
|
+
refined_images = images.reject{|i| i =~ /logo|fallback/i}
|
101
|
+
return refined_images[0] if (refined_images.size == 1)
|
102
|
+
refined_images = images if refined_images.empty?
|
103
|
+
dimensions = []
|
104
|
+
refined_images.each do |i|
|
105
|
+
type = FastImage.type(Addressable::URI.escape(i))
|
106
|
+
size = FastImage.size(Addressable::URI.escape(i))
|
107
|
+
return i if((type == :gif) && (size && size[0] > 299 && size[1] > 199))
|
108
|
+
dimensions << {:x => size[0], :y => size[1], :image => i} if !size.nil?
|
109
|
+
end
|
110
|
+
image = dimensions.empty? ? nil : dimensions.max_by{|d| d[:x]}[:image]
|
111
|
+
return image
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.str_blank?(str)
|
115
|
+
str.nil? || str.empty?
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: article_crux
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- amitsaxena
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: fastimage
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: nokogiri
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.6'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.6'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: httparty
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.12'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.12'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: addressable
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.3'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.3'
|
111
|
+
description: The gem scrapes HTML of a URL to return the title and cover image which
|
112
|
+
most likey represents the article. It also returns an array of tags.
|
113
|
+
email:
|
114
|
+
- amit83.saxena@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- article_crux.gemspec
|
126
|
+
- bin/console
|
127
|
+
- bin/setup
|
128
|
+
- lib/article_crux.rb
|
129
|
+
- lib/article_crux/version.rb
|
130
|
+
homepage: https://github.com/amitsaxena/article_crux
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
metadata:
|
134
|
+
allowed_push_host: https://rubygems.org
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project:
|
151
|
+
rubygems_version: 2.5.1
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: Returns crux of an article, which includes title and the most likely image
|
155
|
+
that represents the article
|
156
|
+
test_files: []
|