docapurl 0.1.2 → 0.2.3
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 +4 -4
- data/CH_README.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +7 -0
- data/lib/docapurl/browser.rb +32 -10
- data/lib/docapurl/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86b590bab2ec4319cb20309eb6a8aada80cb75f06518d3a1dc187f975a586c22
|
4
|
+
data.tar.gz: 991021e9e09e7fee0bfd2bb2c4c2de68740a6b5f21b271022b223b57876efa49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad15cf9f5f9456b163d1ca8fd6a2cab22eef9d710ad6c5acbd93609c6cd7d2f7a63e256c3875dad1312a352d0d60be5d240612e9c52e7213145ab13bae708b6d
|
7
|
+
data.tar.gz: 15c44caa1302a481166441ac3bfb43894248681c8b357752398bedaac55ab293b761d9c1ed75fe8f0c377e782b0db59e4c7f5f2373deff65e96f91e2b6957b0e
|
data/CH_README.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -3,6 +3,13 @@
|
|
3
3
|
A tool to screenshot the webpage on terminal.
|
4
4
|
|
5
5
|
[chinese_readme 中文说明](https://github.com/jicheng1014/docapurl/blob/master/CH_README.md)
|
6
|
+
|
7
|
+
## Capture url as a Service
|
8
|
+
|
9
|
+
[https://www.urlprint.com](https://www.urlprint.com) provides the capture url service, REST API supported.
|
10
|
+
|
11
|
+
|
12
|
+
|
6
13
|
## Installation
|
7
14
|
|
8
15
|
Add this line to your application's Gemfile:
|
data/lib/docapurl/browser.rb
CHANGED
@@ -4,7 +4,7 @@ require 'logger'
|
|
4
4
|
module Docapurl
|
5
5
|
class Browser
|
6
6
|
SYSTEM_MAX_PAGE_DOWN_TO_BOTTOM = 50
|
7
|
-
attr_accessor :browser, :save_path, :logger
|
7
|
+
attr_accessor :browser, :save_path, :logger, :context, :page
|
8
8
|
|
9
9
|
def initialize(options)
|
10
10
|
@save_path = options[:save_path]
|
@@ -13,10 +13,12 @@ module Docapurl
|
|
13
13
|
options[:slowmo] = 0.5
|
14
14
|
@logger = options[:logger] || Logger.new(STDOUT)
|
15
15
|
@browser = Ferrum::Browser.new options
|
16
|
+
@context = browser.contexts.create
|
17
|
+
@page = @context.create_page
|
18
|
+
yield(self) if block_given?
|
16
19
|
end
|
17
20
|
|
18
21
|
def cap(url, options)
|
19
|
-
|
20
22
|
options[:quality] ||= 90
|
21
23
|
options[:full] = true if options[:full].nil?
|
22
24
|
options[:path] ||= @save_path
|
@@ -24,29 +26,37 @@ module Docapurl
|
|
24
26
|
options[:path] ||= "screenshot-#{host.to_s == '' ? '' : "#{host}-"}#{Time.now.strftime('%F-%T')}.jpg"
|
25
27
|
logger.info "browser begin to visit url #{url}"
|
26
28
|
|
27
|
-
|
29
|
+
set_callback("before_visit_func", options)
|
30
|
+
page.go_to(url)
|
31
|
+
set_callback("after_visit_func", options)
|
32
|
+
|
33
|
+
|
28
34
|
logger.info 'visited'
|
29
35
|
max_pagedown = options[:max_pagedown] || 5
|
30
36
|
pagedown_to_bottom = options.delete :pagedown_to_bottom
|
31
|
-
visit_whole_page(
|
37
|
+
visit_whole_page(page: page, max_pagedown: max_pagedown, pagedown_to_bottom: pagedown_to_bottom)
|
32
38
|
|
33
39
|
sleep_before_screen = options.delete :sleep_before_screen
|
34
|
-
logger.info "sleep #{sleep_before_screen.to_i} second before
|
40
|
+
logger.info "sleep #{sleep_before_screen.to_i} second before screenshot"
|
35
41
|
sleep(sleep_before_screen.to_i)
|
36
42
|
|
37
|
-
|
43
|
+
|
44
|
+
set_callback("before_screenshot_func", options)
|
45
|
+
page.screenshot(**options)
|
46
|
+
set_callback("after_screenshot_func", options)
|
47
|
+
|
38
48
|
logger.info "screenshot ended, path = #{options[:path]}"
|
39
49
|
end
|
40
50
|
|
41
51
|
def close
|
42
52
|
return if browser.nil?
|
53
|
+
context.dispose unless context.nil?
|
43
54
|
|
44
55
|
logger.info 'close browser'
|
45
56
|
browser.quit
|
46
57
|
end
|
47
58
|
|
48
|
-
def visit_whole_page(
|
49
|
-
page ||= browser.page
|
59
|
+
def visit_whole_page( page: nil, max_pagedown: nil, pagedown_to_bottom: false)
|
50
60
|
viewport_height = page.viewport_size.last.to_i
|
51
61
|
document_height = page.document_size.last.to_i
|
52
62
|
return if document_height<= viewport_height
|
@@ -61,12 +71,24 @@ module Docapurl
|
|
61
71
|
|
62
72
|
page_down_count.times do
|
63
73
|
logger.info "press PageDown .."
|
64
|
-
|
74
|
+
page.keyboard.type(:PageDown)
|
65
75
|
end
|
66
76
|
logger.info "press HOME .."
|
67
|
-
|
77
|
+
page.keyboard.type(:Home)
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def set_callback(name, options)
|
83
|
+
the_function = options.delete name
|
84
|
+
if the_function.nil?
|
85
|
+
the_function = options.delete name.to_sym
|
86
|
+
end
|
87
|
+
|
88
|
+
the_function.call(self) unless the_function.nil?
|
68
89
|
end
|
69
90
|
|
91
|
+
|
70
92
|
class << self
|
71
93
|
def cap(url, path = nil, browser_options = {}, cap_options = {})
|
72
94
|
browser = new(browser_options)
|
data/lib/docapurl/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docapurl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- atpking
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|