rinkusukurepa 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5ed57cee41ec46988d39ebdfe08c1e9baa1a4478
4
+ data.tar.gz: 1607ef131fc9aa0baba9a89cf655cb00e783e5ba
5
+ SHA512:
6
+ metadata.gz: 481fa1ccdae83c4fb40d1314b97095166bffae554a3bb0eb77dc6c9f5e3d6b04dbaa727f1ac1122d8a3d5ba8926801664582680bd77b035266a84ece784ccc9b
7
+ data.tar.gz: d39f067f5b389aec2b2e3421d0a2f0b98ca8339df66bda75aa9a9db7a8804c34160278eb7fff6132724fa653da28c9fa6d2e3136aa09e5c962fc0ab3867d79ec
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ before_install: gem install bundler -v 1.10.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rinkusukurepa.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 rewin0087
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,67 @@
1
+ # Rinkusukurepa
2
+
3
+ A library for Scraping a webpage by it's url and return the web page title, description, site name, images, favicon and video (if there's a video). Inspired by facebook url sharer.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rinkusukurepa'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rinkusukurepa
20
+
21
+ ## Usage
22
+ web_page = Rinkusukurepa.parse_url!('https://github.com/rewin0087/rinkusukurepa')
23
+ web_page.title # return the web page title
24
+ web_page.description # return the web page description
25
+ web_page.images # return all the web page images
26
+ web_page.site_name # return site name
27
+ web_page.video # return video if present
28
+ web_page.page_type # return page type
29
+ web_page.page_document # return raw nokigiri document object
30
+ web_page.attributes # return a hash with icon, title, description, images, site_name, video and page_type
31
+
32
+ Scrape and get specific attribute
33
+
34
+ web_page = Rinkusukurepa.new('https://github.com/rewin0087/rinkusukurepa')
35
+ web_page.parse_url # it will scrape and return the document of the url
36
+ web_page.get_icon # it will find the icon from the document and return the icon
37
+ web_page.get_title # it will find the title from the document and return the title
38
+ web_page.get_description # it will find the description from the document and return the description
39
+ web_page.get_images # it will find the images from the document and return the images
40
+ web_page.get_site_name # it will find the site name from the document and return the site name
41
+ web_page.get_video # it will find the video from the document and return the video
42
+ web_page.get_page_type # it will find the page type from the document and return the page type
43
+
44
+ To customize some of the configurations
45
+ create a file in the config/initializers/rinkusukurepa.rb
46
+ and put:
47
+ Rinkusukurepa.configure do |config|
48
+ config.image_min_width = 200 # default 150
49
+ config.image_min_height = 200 # default 100
50
+ config.max_image = 20 # default 20
51
+ config.page_types = ['article', 'post'] # default ['website', 'video', 'sound']
52
+ config.image_extensions = /(.png|.jpg)/ # default /(.png|PNG|.jpg|JPG|.jpeg|JPEG|BMP|.bmp|.gif|GIF)/
53
+ end
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (git checkout -b my-new-feature)
59
+ 3. Commit your changes (git commit -am 'Add some feature')
60
+ 4. Push to the branch (git push origin my-new-feature)
61
+ 5. Create new Pull Request
62
+
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
67
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rinkusukurepa"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ module Rinkusukurepa
2
+ module Configuration
3
+ attr_accessor :image_min_width,
4
+ :image_min_height,
5
+ :max_image,
6
+ :page_types,
7
+ :image_extensions
8
+
9
+ def configure
10
+ yield self
11
+ end
12
+
13
+ def image_min_width
14
+ @image_min_width || 150
15
+ end
16
+
17
+ def image_min_height
18
+ @image_min_height || 100
19
+ end
20
+
21
+ def max_image
22
+ @max_image || 10
23
+ end
24
+
25
+ def page_types
26
+ @page_types || %w(website video sound)
27
+ end
28
+
29
+ def image_extensions
30
+ @image_extensions || /(.png|PNG|.jpg|JPG|.jpeg|JPEG|BMP|.bmp|.gif|GIF)/
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,239 @@
1
+ require 'uri'
2
+ require 'open-uri'
3
+ require 'open_uri_redirections'
4
+ require 'nokogiri'
5
+ require 'fastimage'
6
+
7
+ module Rinkusukurepa
8
+ extend Rinkusukurepa::Configuration
9
+
10
+ class Scraper
11
+ attr_accessor :page_url,
12
+ :icon,
13
+ :title,
14
+ :description,
15
+ :images,
16
+ :site_name,
17
+ :video,
18
+ :page_type,
19
+ :page_document
20
+
21
+ Rinkusukurepa.page_types.each do |type|
22
+ define_method "#{type.downcase.strip.gsub(/[^0-9A-Za-z]/, '')}?" do
23
+ self.type == type
24
+ end
25
+
26
+ define_singleton_method "#{type.downcase.strip.gsub(/[^0-9A-Za-z]/, '')}" do
27
+ type
28
+ end
29
+ end
30
+
31
+ def self.parse_url!(url)
32
+ scraper = Rinkusukurepa::Scraper.new(url)
33
+ scraper.parse_url!
34
+ scraper
35
+ end
36
+
37
+ def initialize(url)
38
+ @page_url = URI.parse(URI.encode(url))
39
+ @images = []
40
+ end
41
+
42
+ def parse_url
43
+ @page_document = Nokogiri::HTML(open(@page_url.to_s, :allow_redirections => :all))
44
+ self
45
+ end
46
+
47
+ def parse_url!
48
+ parse_url
49
+ get_icon
50
+ get_title
51
+ get_description
52
+ get_images
53
+ get_site_name
54
+ get_video
55
+ get_page_type
56
+ end
57
+
58
+ def attributes
59
+ {
60
+ icon: @icon,
61
+ title: @title,
62
+ description: @description,
63
+ images: @images || [],
64
+ site_name: @site_name,
65
+ video: @video,
66
+ page_type: @page_type
67
+ }
68
+ end
69
+
70
+ def get_icon
71
+ icon = @page_document.css('link[rel="icon"]')
72
+ icon = @page_document.css('link[rel="shortcut icon"]') if icon.nil? || icon.empty?
73
+
74
+ if !icon.nil? && !icon.empty?
75
+ href = icon.first.attributes['href'].value
76
+ uri = URI.parse(URI.encode(href))
77
+ if uri.host.nil?
78
+ @icon = "http://#{@page_url.host}#{uri.path}"
79
+ else
80
+ @icon = "http://#{uri.host}#{uri.path}"
81
+ end
82
+ end
83
+
84
+ @icon
85
+ end
86
+
87
+ def get_title
88
+ # title
89
+ title = @page_document.css('meta[property="og:title"]')
90
+ if title.nil? || title.empty?
91
+ @title = @page_document.css('title').text
92
+ else
93
+ # parse from meta
94
+ @title = title.first.attributes['content'].value
95
+ end
96
+
97
+ @title
98
+ end
99
+
100
+ def get_description
101
+ # Description
102
+ description = @page_document.css('meta[property="og:description"]')
103
+ if description.nil? || description.empty?
104
+ description = @page_document.css('meta[name="description"]')
105
+ if !description.nil? && !description.empty?
106
+ @description = description.first.attributes['content'].value
107
+ end
108
+ else
109
+ # parse from meta
110
+ @description = description.first.attributes['content'].value
111
+ end
112
+
113
+ @description
114
+ end
115
+
116
+ def get_images
117
+ # check if url is an image
118
+ if File.extname(@page_url.to_s).match(Rinkusukurepa.image_extensions)
119
+ @images << @page_url.to_s
120
+ return @images
121
+ end
122
+
123
+ # Image Preview
124
+ preview = @page_document.css('meta[property="og:image"]')
125
+ if preview.nil? || preview.empty?
126
+ retrieve_all_images_from_document
127
+ else
128
+ # parse from meta
129
+ @images = preview.map do |p|
130
+ url = p.attributes['content'].value
131
+ uri = URI.parse(URI.encode(url.strip! || url))
132
+ if uri.to_s.match(/http/)
133
+ if uri.host.nil?
134
+ "http://#{@page_url.host}#{uri.to_s}"
135
+ else
136
+ uri.to_s
137
+ end
138
+ if http_image = FastImage.size(uri.to_s)
139
+ if http_image[0] > Rinkusukurepa.image_min_width && http_image[1] > Rinkusukurepa.image_min_height
140
+ uri.to_s
141
+ end
142
+ end
143
+ end
144
+ end.compact.uniq
145
+
146
+ if @images.nil? || @images.empty?
147
+ retrieve_all_images_from_document
148
+ end
149
+ end
150
+
151
+ @images
152
+ end
153
+
154
+ def get_site_name
155
+ # Site Name
156
+ site_name = @page_document.css('meta[property="og:site_name"]')
157
+ if site_name.nil? || site_name.empty?
158
+ @site_name = @page_url.host
159
+ else
160
+ # parse from meta
161
+ @site_name = site_name.first.attributes['content'].value
162
+ end
163
+
164
+ @site_name
165
+ end
166
+
167
+ def get_video
168
+ # Video
169
+ videos = @page_document.css('meta[property="og:video:secure_url"]')
170
+ if !videos.nil? && !videos.empty?
171
+ # parse from meta
172
+ @video = videos.find { |v| v.attributes['content'].value.include?('embed') }.attributes['content'].value
173
+ end
174
+
175
+ @video
176
+ end
177
+
178
+ def get_page_type
179
+ # Type
180
+ type = @page_document.css('meta[property="og:type"]')
181
+ if type.nil? || type.empty?
182
+ @page_type = Rinkusukurepa::Scraper.website
183
+ else
184
+ # parse from meta
185
+ page_type = type.first.attributes['content'].value
186
+ @page_type = Rinkusukurepa.page_types.include?(page_type) ? page_type : Rinkusukurepa::Scraper.website
187
+ end
188
+
189
+ @page_type
190
+ end
191
+
192
+ protected
193
+
194
+ def retrieve_all_images_from_document
195
+ # get all img
196
+ preview = @page_document.css('img')
197
+ if !preview.nil? && !preview.empty?
198
+ previews = preview[0..Rinkusukurepa.max_image].map do |p|
199
+ image = p.attributes['src']
200
+ unless image.blank?
201
+ url = nil
202
+ if image.value.match(Rinkusukurepa.image_extensions) && image.value.match(/http/)
203
+ uri = URI.parse(URI.encode(image.value.strip! || image.value))
204
+ if uri.host.nil?
205
+ url = "http://#{@page_url.host}/#{uri.to_s}"
206
+ else
207
+ url = uri.to_s
208
+ end
209
+ end
210
+
211
+ unless url.nil?
212
+ # NOTE: fallback mini magick if fast image return's nil
213
+ # fast image
214
+ if http_image = ::FastImage.size(url)
215
+ if http_image[0] > Rinkusukurepa.image_min_width && http_image[1] > Rinkusukurepa.image_min_height
216
+ url
217
+ end
218
+ # mini magick
219
+ else
220
+ begin
221
+ http_image = ::MiniMagick::Image.open(url)
222
+ if http_image.present?
223
+ if http_image.width > Rinkusukurepa.image_min_width && http_image.height > Rinkusukurepa.image_min_height
224
+ url
225
+ end
226
+ end
227
+ rescue => e
228
+ nil
229
+ end
230
+ end
231
+ end
232
+ end
233
+ end.compact.uniq
234
+ # set scraped images
235
+ @images = previews
236
+ end
237
+ end
238
+ end
239
+ end
@@ -0,0 +1,3 @@
1
+ module Rinkusukurepa
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "rinkusukurepa/version"
2
+ require "rinkusukurepa/configuration"
3
+ require "rinkusukurepa/scraper"
4
+
5
+ module Rinkusukurepa
6
+ class << self
7
+ def new(url)
8
+ scraper.new(url)
9
+ end
10
+
11
+ def scraper
12
+ Rinkusukurepa::Scraper
13
+ end
14
+
15
+ def parse_url!(url)
16
+ Rinkusukurepa::Scraper.parse_url!(url)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rinkusukurepa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rinkusukurepa"
8
+ spec.version = Rinkusukurepa::VERSION
9
+ spec.authors = ["rewin0087"]
10
+ spec.email = ["erwin_rulezzz87@yahoo.com"]
11
+
12
+ spec.summary = %q{A library for Scraping a webpage by it's url and return the web page title, description, site name, images, favicon and video (if there's a video). Inspired by facebook url sharer.}
13
+ spec.description = %q{A library for Scraping a webpage by it's url and return the web page title, description, site name, images, favicon and video (if there's a video). Inspired by facebook url sharer. }
14
+ spec.homepage = "https://github.com/rewin0087/rinkusukurepa"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ spec.add_runtime_dependency "nokogiri", "~> 1.6.7"
27
+ spec.add_runtime_dependency "open_uri_redirections", "~> 0.2.1"
28
+ spec.add_runtime_dependency "fastimage", "~> 1.8.1"
29
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rinkusukurepa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rewin0087
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-09 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.6.7
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.6.7
69
+ - !ruby/object:Gem::Dependency
70
+ name: open_uri_redirections
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.2.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.2.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: fastimage
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.8.1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.8.1
97
+ description: 'A library for Scraping a webpage by it''s url and return the web page
98
+ title, description, site name, images, favicon and video (if there''s a video).
99
+ Inspired by facebook url sharer. '
100
+ email:
101
+ - erwin_rulezzz87@yahoo.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - lib/rinkusukurepa.rb
117
+ - lib/rinkusukurepa/configuration.rb
118
+ - lib/rinkusukurepa/scraper.rb
119
+ - lib/rinkusukurepa/version.rb
120
+ - rinkusukurepa.gemspec
121
+ homepage: https://github.com/rewin0087/rinkusukurepa
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.4.8
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A library for Scraping a webpage by it's url and return the web page title,
145
+ description, site name, images, favicon and video (if there's a video). Inspired
146
+ by facebook url sharer.
147
+ test_files: []