docapurl 0.1.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c10bf394b6481f9fc5c761b00e66c07962528af9f7f06126af16a50c775341a
4
- data.tar.gz: f17dd6558580511af00c722be7fe435d605aa2a967bf3e0bba54b4fc72f7f47d
3
+ metadata.gz: 86b590bab2ec4319cb20309eb6a8aada80cb75f06518d3a1dc187f975a586c22
4
+ data.tar.gz: 991021e9e09e7fee0bfd2bb2c4c2de68740a6b5f21b271022b223b57876efa49
5
5
  SHA512:
6
- metadata.gz: d96e596ec9a1efcb84d2ebb6f76d3cb0058983ce5aa4c7056e645f436b36dad0eaca13784a245f13f548b62fdcf6473eb6a9dacad234970dfc10168798596664
7
- data.tar.gz: 0247bfe4cd74ddcbd56d8f3b20c74096cc994c2b67de559dae8126bb2ba7451acbb8fc6337a6b7e769dbce5f55a5465196b94a174af90a75975e35edb40adcb1
6
+ metadata.gz: ad15cf9f5f9456b163d1ca8fd6a2cab22eef9d710ad6c5acbd93609c6cd7d2f7a63e256c3875dad1312a352d0d60be5d240612e9c52e7213145ab13bae708b6d
7
+ data.tar.gz: 15c44caa1302a481166441ac3bfb43894248681c8b357752398bedaac55ab293b761d9c1ed75fe8f0c377e782b0db59e4c7f5f2373deff65e96f91e2b6957b0e
data/CH_README.md CHANGED
@@ -1,10 +1,15 @@
1
1
  # Docapurl
2
2
  一个ruby的命令行截图网页的工具
3
3
 
4
+ ## 抓取网页线上服务
5
+
6
+ [https://www.urlprint.com](https://www.urlprint.com) 提供在线抓取网页生成图片的服务, 支持 REST API 调用, 欢迎各位大佬试用
7
+
4
8
  ## 安装
5
9
 
6
10
  gem install docapurl
7
11
 
12
+
8
13
  ## 前置条件
9
14
 
10
15
  - ruby 环境
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- docapurl (0.1.2)
4
+ docapurl (0.2.3)
5
5
  ferrum (~> 0.11)
6
6
  thor
7
7
 
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:
@@ -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
- browser.go_to(url)
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(browser, max_pagedown: max_pagedown, pagedown_to_bottom: pagedown_to_bottom)
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 screen"
40
+ logger.info "sleep #{sleep_before_screen.to_i} second before screenshot"
35
41
  sleep(sleep_before_screen.to_i)
36
42
 
37
- browser.screenshot(**options)
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(browser, page: nil, max_pagedown: nil, pagedown_to_bottom: false)
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
- browser.keyboard.type(:PageDown)
74
+ page.keyboard.type(:PageDown)
65
75
  end
66
76
  logger.info "press HOME .."
67
- browser.keyboard.type(:Home)
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)
@@ -1,3 +1,3 @@
1
1
  module Docapurl
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.3"
3
3
  end
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.1.2
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-04-25 00:00:00.000000000 Z
11
+ date: 2021-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler