hertools 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hertools.gemspec +30 -0
- data/lib/hertools.rb +9 -0
- data/lib/hertools/version.rb +5 -0
- data/lib/hertools/website_parser.rb +172 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 20f15df456f2d19d57142b6e67310c78cf4d7a80a08d4f3e17ca07764e7bb38f
|
4
|
+
data.tar.gz: 0e0ac2aff27cc5303d8a3ed93e44c075c6bad37cba541f00565eeb00f4f3cb85
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 14a9a96095ad1a146abb92cb5c230366639cb2a4e490822ca8bedd9e6311c9ded6932d500c6b8b6025916d9c874dcef7fb2330c6b3b615cdd122c50990c83b0f
|
7
|
+
data.tar.gz: ab279755ab77ff157d39a9d6ae5317837906c8bf81ecd3d95ee7d92580fe1cafec49f96198ba28f4728082ac76f9526854a0c838a9cf0f47e3e33a29ea526034
|
data/.gitignore
ADDED
data/.rspec
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) 2020 WuDi
|
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,48 @@
|
|
1
|
+
# Hertools
|
2
|
+
|
3
|
+
A tool box made for her. Provides more and more tools, like the WebsiteParser and so on.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'hertools'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hertools
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Hertools::WebsiteParser
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'hertools'
|
27
|
+
|
28
|
+
wp = Hertools::WebsiteParser.new
|
29
|
+
|
30
|
+
res = wp.crawl_title_and_favicon_file("https://www.baidu.com/")
|
31
|
+
res = wp.crawl_title_and_favicon_file("https://www.baidu.com/", root_path: "/Users/test/study/", html_parser: "httparty")
|
32
|
+
res = wp.crawl_title_and_favicon_file("https://www.baidu.com", root_path: "/Users/test/study/", html_parser: "nokogiri")
|
33
|
+
```
|
34
|
+
|
35
|
+
## Development
|
36
|
+
|
37
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
38
|
+
|
39
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Amberwudi/hertools.
|
44
|
+
|
45
|
+
|
46
|
+
## License
|
47
|
+
|
48
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hertools"
|
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/hertools.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'lib/hertools/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "hertools"
|
5
|
+
spec.version = Hertools::VERSION
|
6
|
+
spec.authors = ["WuDi"]
|
7
|
+
spec.email = ["wudlvq@qq.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{A tool box made for her.}
|
10
|
+
spec.description = %q{A tool box made for her. Provides more and more tools, like the WebsiteParser and so on.}
|
11
|
+
spec.homepage = "https://github.com/Amberwudi/hertools"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.add_dependency 'httparty', '~> 0.18', '>= 0.18.1'
|
16
|
+
spec.add_dependency 'nokogiri', '~> 1.10', '>= 1.10.10'
|
17
|
+
spec.add_dependency 'htmlentities', '~> 4.3', '>= 4.3.4'
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/Amberwudi/hertools"
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
end
|
data/lib/hertools.rb
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'base64'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'httparty'
|
6
|
+
require 'htmlentities'
|
7
|
+
require 'nokogiri'
|
8
|
+
|
9
|
+
module Hertools
|
10
|
+
# Summary: help to get the website info by a url.
|
11
|
+
class WebsiteParser
|
12
|
+
# Summary: get the title and favicon file by the url of one webpage.
|
13
|
+
# Arguments
|
14
|
+
# url: the url of a webpage
|
15
|
+
# options:
|
16
|
+
# html_parser: %w[httparty nokogiri]
|
17
|
+
# root_path: existing file directory
|
18
|
+
def crawl_title_and_favicon_file(url, options = {})
|
19
|
+
puts '>>> Parsing the arguments <<<'
|
20
|
+
unless parse_url(url)
|
21
|
+
puts 'Failed because the bad url!'
|
22
|
+
return false
|
23
|
+
end
|
24
|
+
unless parse_options(options)
|
25
|
+
puts 'Failed because the bad options!'
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
|
29
|
+
puts '>>> Analysing the http response <<<'
|
30
|
+
if httparty?
|
31
|
+
response = HTTParty.get(@url)
|
32
|
+
res = response.body
|
33
|
+
else
|
34
|
+
response = HTTParty.head(@url)
|
35
|
+
res = begin
|
36
|
+
Nokogiri::HTML(URI.open(url), nil, 'UTF-8')
|
37
|
+
rescue StandardError => e
|
38
|
+
puts e
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
puts "HttpCode: #{response.code}"
|
43
|
+
|
44
|
+
if res.nil? || res.to_s.empty?
|
45
|
+
puts 'No content!'
|
46
|
+
@title = @domain_name
|
47
|
+
@favicon_url = "#{@index_url}/favicon.ico"
|
48
|
+
puts "Use the default favicon url: #{@favicon_url}."
|
49
|
+
else
|
50
|
+
@title = if httparty?
|
51
|
+
res[%r{<title>\n*(.*)\n*</title>}, 1].to_s
|
52
|
+
else
|
53
|
+
res.xpath('//head/title')[0]&.content.to_s
|
54
|
+
end
|
55
|
+
if @title.empty?
|
56
|
+
puts 'Not found the title!'
|
57
|
+
puts 'Use the domain name as the title.'
|
58
|
+
@title = @domain_name
|
59
|
+
end
|
60
|
+
if httparty?
|
61
|
+
coder = HTMLEntities.new
|
62
|
+
@title = coder.decode(@title)
|
63
|
+
end
|
64
|
+
puts "Title: #{@title}"
|
65
|
+
|
66
|
+
@favicon_url = if httparty?
|
67
|
+
res[/<link rel="icon".*href="([^"]+)/, 1].to_s
|
68
|
+
else
|
69
|
+
favicon_links = res.xpath('//head/link[@rel="icon"]')
|
70
|
+
favicon_links.empty? ? '' : favicon_links[0][:href].to_s
|
71
|
+
end
|
72
|
+
if @favicon_url.empty?
|
73
|
+
puts 'Not found the favicon url!'
|
74
|
+
@favicon_url = "#{@index_url}/favicon.ico"
|
75
|
+
puts "Use the default favicon url: #{@favicon_url}."
|
76
|
+
else
|
77
|
+
puts "FaviconUrl: #{@favicon_url}"
|
78
|
+
unless @favicon_url.include?('http')
|
79
|
+
if @favicon_url.include?('//')
|
80
|
+
@favicon_url = "#{@protocol}:#{@favicon_url}"
|
81
|
+
puts "Fixed favicon url: #{@favicon_url}"
|
82
|
+
elsif @favicon_url.include?('/')
|
83
|
+
@favicon_url = "#{@index_url}#{@favicon_url}"
|
84
|
+
puts "Fixed favicon url: #{@favicon_url}"
|
85
|
+
else
|
86
|
+
@favicon_url = "#{@index_url}/favicon.ico"
|
87
|
+
puts "Use the default favicon url: #{@favicon_url}."
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
if @title.empty? && @favicon_url.empty?
|
94
|
+
puts 'Failed because not found the title and favicon url!'
|
95
|
+
return false
|
96
|
+
end
|
97
|
+
|
98
|
+
identifier = Digest::MD5.hexdigest(@url)
|
99
|
+
file_directory_path = "#{@root_path}/#{@domain_name}_#{identifier}"
|
100
|
+
puts "FileDirectory: #{file_directory_path}"
|
101
|
+
Dir.mkdir(file_directory_path) unless File.directory?(file_directory_path)
|
102
|
+
if File.directory?(file_directory_path)
|
103
|
+
unless @title.empty?
|
104
|
+
info_file_path = "#{file_directory_path}/website_info.txt"
|
105
|
+
puts "InfoFilePath: #{info_file_path}"
|
106
|
+
open(info_file_path, 'wb') { |f| f << "Title: #{@title}" }
|
107
|
+
end
|
108
|
+
|
109
|
+
unless @favicon_url.empty?
|
110
|
+
favicon_file_suffix = @favicon_url.split('.').last
|
111
|
+
favicon_file_name = Digest::MD5.hexdigest(@favicon_url) + '.' + favicon_file_suffix
|
112
|
+
favicon_file_path = "#{file_directory_path}/#{favicon_file_name}"
|
113
|
+
puts "FaviconFilePath: #{favicon_file_path}"
|
114
|
+
open(favicon_file_path, 'wb') { |f| f << URI.open(@favicon_url).read }
|
115
|
+
end
|
116
|
+
puts 'Finished!'
|
117
|
+
true
|
118
|
+
else
|
119
|
+
puts 'Failed to create the directory!'
|
120
|
+
false
|
121
|
+
end
|
122
|
+
rescue StandardError => e
|
123
|
+
puts e
|
124
|
+
puts 'Failed because the unexpected exception!'
|
125
|
+
false
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
def parse_url(url)
|
131
|
+
@url = String(url)
|
132
|
+
puts "Url: #{@url}"
|
133
|
+
@url_match = @url.match(%r{(https?)://([^/]+)})
|
134
|
+
return false if @url.empty? || @url_match.nil?
|
135
|
+
|
136
|
+
@index_url = @url_match[0]
|
137
|
+
puts "IndexUrl: #{@index_url}"
|
138
|
+
@protocol = @url_match[1]
|
139
|
+
puts "Protocol: #{@protocol}"
|
140
|
+
@domain_name = @url_match[2]
|
141
|
+
puts "DomainName: #{@domain_name}"
|
142
|
+
true
|
143
|
+
rescue StandardError => e
|
144
|
+
puts e
|
145
|
+
false
|
146
|
+
end
|
147
|
+
|
148
|
+
def parse_options(options)
|
149
|
+
options = Hash(options)
|
150
|
+
html_parser = options.fetch(:html_parser) { 'httparty' }
|
151
|
+
@old_html_parser = @html_parser
|
152
|
+
@html_parser = %w[httparty nokogiri].include?(html_parser) ? html_parser : 'httparty'
|
153
|
+
reset_httparty_judge_result if @old_html_parser != @html_parser
|
154
|
+
puts "HtmlParser: #{@html_parser}"
|
155
|
+
root_path = options.fetch(:root_path) { Dir.pwd }
|
156
|
+
@root_path = (File.directory?(root_path) ? root_path : Dir.pwd).chomp('/')
|
157
|
+
puts "RootPath: #{@root_path}"
|
158
|
+
true
|
159
|
+
rescue StandardError => e
|
160
|
+
puts e
|
161
|
+
false
|
162
|
+
end
|
163
|
+
|
164
|
+
def httparty?
|
165
|
+
@httparty_judge_result ||= @html_parser == 'httparty'
|
166
|
+
end
|
167
|
+
|
168
|
+
def reset_httparty_judge_result
|
169
|
+
@httparty_judge_result = nil
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hertools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- WuDi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.18'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.18.1
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.18'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.18.1
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: nokogiri
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.10'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 1.10.10
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '1.10'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 1.10.10
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: htmlentities
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '4.3'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 4.3.4
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '4.3'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 4.3.4
|
73
|
+
description: A tool box made for her. Provides more and more tools, like the WebsiteParser
|
74
|
+
and so on.
|
75
|
+
email:
|
76
|
+
- wudlvq@qq.com
|
77
|
+
executables: []
|
78
|
+
extensions: []
|
79
|
+
extra_rdoc_files: []
|
80
|
+
files:
|
81
|
+
- ".gitignore"
|
82
|
+
- ".rspec"
|
83
|
+
- ".travis.yml"
|
84
|
+
- Gemfile
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.md
|
87
|
+
- Rakefile
|
88
|
+
- bin/console
|
89
|
+
- bin/setup
|
90
|
+
- hertools.gemspec
|
91
|
+
- lib/hertools.rb
|
92
|
+
- lib/hertools/version.rb
|
93
|
+
- lib/hertools/website_parser.rb
|
94
|
+
homepage: https://github.com/Amberwudi/hertools
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata:
|
98
|
+
homepage_uri: https://github.com/Amberwudi/hertools
|
99
|
+
source_code_uri: https://github.com/Amberwudi/hertools
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: 2.3.0
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubygems_version: 3.1.2
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: A tool box made for her.
|
119
|
+
test_files: []
|