images_from_link 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: d7a96ebdb6430e22f1f2566655793a7579b5e1c4
4
+ data.tar.gz: c80e576e30626c7d2967589c9a51817cf93c5bc5
5
+ SHA512:
6
+ metadata.gz: 4120466fde93136b6a0859c87b78e06e1ad25a3c9dcac1ab70b62c166221c8f29816d64fd61a31228bb68e1d9934cf0042eece7a941678f670c073ac8a5805c7
7
+ data.tar.gz: a2c0e307c68209d197c54ab46b0b66136235ed42898c7dd7ae63c1a461728709b5f45b594b3bfc9d78a128daa870a95faa18f2a6baa8998a70fc99cb4d5e3e8b
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at exwarvlad@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'httparty'
4
+ gem 'nokogiri'
5
+
6
+ # Specify your gem's dependencies in images_from_link.gemspec
7
+ gemspec
8
+
9
+ gem 'rspec'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 zerocool
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,39 @@
1
+ # ImagesFromLink
2
+
3
+ Extracts images by link.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'images_from_link'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install images_from_link
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ require 'images_from_link'
25
+
26
+ ImagesFromLink.get_images('https://www.google.com')
27
+
28
+ =>
29
+ [
30
+ "https://www.google.com/textinputassistant/tia.png",
31
+ "https://www.google.com/images/nav_logo229.png",
32
+ "https://www.google.com/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png"
33
+ ]
34
+ ```
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "images_from_link"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'images_from_link/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "images_from_link"
8
+ spec.version = ImagesFromLink::VERSION
9
+ spec.authors = ["zerocool"]
10
+ spec.email = ["exwarvlad@gmail.com"]
11
+
12
+ spec.summary = "Get images from link"
13
+ spec.description = "Get images from link"
14
+ spec.homepage = "https://github.com/exwarvlad/images_from_link"
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'] = "TODO: Set to 'http://mygemserver.com'"
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{^(spec|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 "rspec", "~> 3.5"
36
+
37
+ spec.add_dependency "httparty"
38
+ spec.add_dependency "nokogiri", "~> 1.7.0"
39
+ end
@@ -0,0 +1,70 @@
1
+ class HandlerLink
2
+ FORMAT_IMG = ["jpg", "jpeg", "png", "gif"]
3
+
4
+ # получает урл - отдает домен
5
+ def get_host_link(link)
6
+ uri = URI.parse(link)
7
+ "#{uri.scheme}://" + uri.host
8
+ end
9
+
10
+ def handler_links(arra_links, link)
11
+ arra_links.each do |url|
12
+ if url.include?("(/")
13
+ uri = get_host_link(link)
14
+
15
+ position = url.index("(")
16
+ url[position] += uri.to_s
17
+ end
18
+ end
19
+ end
20
+
21
+ # добавляет scheme, если это href
22
+ def handler_prefix_link(host_link, link)
23
+ abort 'в качестве аргументов передайте строки' unless host_link.is_a?(String) || link.is_a?(String)
24
+
25
+ if link[0] == '/' && link[1] != '/'
26
+ host_link + link
27
+ elsif link[0..1] == '//'
28
+ uri = URI.parse(host_link)
29
+ "#{uri.scheme}:#{link}"
30
+ else
31
+ link
32
+ end
33
+ end
34
+
35
+ def remove_unless_symbols(arra_images_links)
36
+ arra_images_links.each do |image_url|
37
+ if (image_url[0..3] != "http" || image_url[0..3] != "www.") && image_url.include?("(")
38
+ position = image_url.index("(")
39
+ image_url.reverse!
40
+ position.times { image_url.chop! }
41
+ image_url.reverse!
42
+ image_url.delete!("(,;'')")
43
+ end
44
+ end
45
+ end
46
+
47
+ def remove_global_unless_symbols(arra_images_links)
48
+ arra_images_links.each { |link| link.delete!("(,;'')") }
49
+ end
50
+
51
+ def remove_unless_link(arra_link)
52
+
53
+ arra_link.each_with_index do |link, index|
54
+ arra_link[index] = "" if link[0..3] != "http"
55
+
56
+ index_ending = nil
57
+
58
+ FORMAT_IMG.each do |i|
59
+ index_ending = i if link.include?(i)
60
+ end
61
+
62
+ unless index_ending == nil
63
+ position = link.index(index_ending)
64
+ arra_link[index] = "" if (link[position + index_ending.size] =~ /[a-z]/)
65
+ end
66
+
67
+ end
68
+ arra_link.delete("")
69
+ end
70
+ end
data/lib/ImagesLink.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'httparty'
2
+ require 'nokogiri'
3
+ require 'uri'
4
+ require_relative 'HandlerLink'
5
+
6
+ class ImagesLink
7
+
8
+ def initialize(link)
9
+ @link = link
10
+
11
+ begin
12
+ @response = HTTParty.get(@link)
13
+ rescue Errno::ECONNREFUSED => e
14
+ puts "Неправильный урл"
15
+ abort e.message
16
+ end
17
+
18
+ @doc = Nokogiri::HTML(@response.body)
19
+ @doc.search('//noscript').each { |node| node.remove } # убираю мешающие ноды
20
+ @arra_links = [] # сдесь будут храниться все урлы картинок
21
+ @handler_link = HandlerLink.new # обработчик урлов
22
+ @link_host_name = @handler_link.get_host_link(@link) # беру имя домена
23
+ end
24
+
25
+ # возвращает, все найденные, урлы картинок
26
+ def get_images_from_url
27
+ @arra_links = (get_url_with_attr_img_link + get_url_with_other_attr).uniq
28
+ @handler_link.remove_global_unless_symbols(@arra_links)
29
+ @arra_links
30
+ end
31
+
32
+ def get_url_with_attr_img_link
33
+ img_arra = []
34
+ # пробегаю по тегам img (meta og:images...), хватаю урл и закидываю в @arra_links
35
+ @doc.xpath('//img').each do |img|
36
+ if img['src'] != nil
37
+ arra = [img['src'].to_s]
38
+ src = @handler_link.remove_unless_symbols(arra)
39
+ got_link = @handler_link.handler_prefix_link(@link_host_name, src.to_s.delete!("[\"]"))
40
+
41
+ img_arra << got_link
42
+ end
43
+ end
44
+
45
+ @doc.xpath('//link').each do |link|
46
+ if link['href'] != nil && link['type'] != nil
47
+ if link['type'].include?("image")
48
+ got_link = @handler_link.handler_prefix_link(@link_host_name, link['href'])
49
+ img_arra << got_link
50
+ end
51
+ end
52
+ end
53
+
54
+ @arra_links.uniq!
55
+ @handler_link.remove_unless_link(@arra_links)
56
+ img_arra
57
+ end
58
+
59
+ def get_url_with_other_attr
60
+ # нахожу все урлы с jpg, png, gif... и закидываю в @arra_links
61
+ @images_links = URI.extract(@doc.to_s.encode("UTF-16be", :invalid => :replace, :replace => "?").encode('UTF-8')).select { |l| l[/\.(?:gif|png|jpe?g)\b/] }
62
+ @handler_link.handler_links(@images_links, @link) # обрабатываю урлы
63
+ @handler_link.remove_unless_symbols(@images_links)
64
+ @handler_link.remove_unless_link(@images_links)
65
+ @images_links
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module ImagesFromLink
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "images_from_link/version"
2
+ require 'HandlerLink'
3
+ require 'ImagesLink'
4
+
5
+ module ImagesFromLink
6
+ # выводит каритнки по переданному урлу
7
+ def self.get_images(link)
8
+ images_link = ImagesLink.new(link)
9
+ images_link.get_images_from_url
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: images_from_link
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - zerocool
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-18 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '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.7.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.7.0
83
+ description: Get images from link
84
+ email:
85
+ - exwarvlad@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - CODE_OF_CONDUCT.md
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - images_from_link.gemspec
99
+ - lib/HandlerLink.rb
100
+ - lib/ImagesLink.rb
101
+ - lib/images_from_link.rb
102
+ - lib/images_from_link/version.rb
103
+ homepage: https://github.com/exwarvlad/images_from_link
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.8
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Get images from link
127
+ test_files: []