treerful_scanner 1.0.1 → 1.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 +4 -4
- data/bin/treerful_scanner +43 -8
- data/lib/treerful_scanner/client.rb +2 -2
- data/lib/treerful_scanner/html_printer.rb +39 -0
- data/lib/treerful_scanner/png_printer.rb +42 -0
- data/lib/treerful_scanner/printer.rb +11 -8
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a365a19a0eb1375fc78e84d6a2bea533452548b4bb56ee6843bd052ab3ac469
|
4
|
+
data.tar.gz: '08c18615a4d44f9d257010f40aace6ec423d2f4b61c59e43777bf7723aee7563'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff7a389844442ea8422095fb1f13b6e94d45b6429a0c331b8d1559173c1788509cfe6f8f7f02a231c077b1f8cb161a5cf98cf996e571a6909b0f191b81c053a9
|
7
|
+
data.tar.gz: 2f6cfbde0105d61b20929625c9e1a5319d4302bd0b28a9d8cc70aa45d3558e9124eff7134492c3d39e177d45c40d8f85020b23e647c211059b5b73daa8a324e4
|
data/bin/treerful_scanner
CHANGED
@@ -6,22 +6,57 @@ require 'date'
|
|
6
6
|
require 'optparse'
|
7
7
|
|
8
8
|
options = {
|
9
|
-
date: (Date.today + 1).to_s
|
9
|
+
date: (Date.today + 1).to_s,
|
10
|
+
places: [],
|
11
|
+
place_names: [],
|
12
|
+
format: 'terminal'
|
10
13
|
}
|
11
14
|
|
12
15
|
OptionParser.new do |opts|
|
13
16
|
opts.banner = "Usage: #{File.basename __FILE__} [options]"
|
14
|
-
opts.on('-d DATE', '--date
|
17
|
+
opts.on('-d DATE', '--date DATE', 'yyyy-mm-dd') do |date|
|
15
18
|
options[:date] = date
|
16
19
|
end
|
20
|
+
|
21
|
+
opts.on('-n x,y,z', '--place-names x,y,z', Array, 'Filter by place names') do |place_names|
|
22
|
+
options[:place_names] = place_names
|
23
|
+
end
|
24
|
+
|
25
|
+
opts.on('-p x,y,z', '--places x,y,z', Array, 'Filter by place IDs') do |ids|
|
26
|
+
options[:places] = ids
|
27
|
+
end
|
28
|
+
|
29
|
+
opts.on('-f FORMAT', '--format FORMAT', '"terminal" (default), "html", "png"') do |format|
|
30
|
+
options[:format] = format
|
31
|
+
end
|
17
32
|
end.parse!
|
18
33
|
|
19
34
|
client = TreerfulScanner::Client.new
|
20
|
-
printer = TreerfulScanner::Printer.new([], options[:date])
|
21
35
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
36
|
+
place_filter = -> (place) do
|
37
|
+
return true if options[:place_names].empty? && options[:places].empty?
|
38
|
+
return options[:place_names].any?{ |name| place.name.include?(name) } unless options[:place_names].empty?
|
39
|
+
return options[:places].include?(place.id) unless options[:places].empty?
|
40
|
+
false
|
41
|
+
end
|
42
|
+
|
43
|
+
case options[:format]
|
44
|
+
when 'terminal'
|
45
|
+
printer = TreerfulScanner::Printer.new([], options[:date])
|
46
|
+
count = 0
|
47
|
+
client.search_by_date(options[:date], place_filter: place_filter) do |table|
|
48
|
+
print printer.header if count % 10 == 0
|
49
|
+
print printer.row(table)
|
50
|
+
count += 1
|
51
|
+
end
|
52
|
+
when 'html'
|
53
|
+
time_tables = client.search_by_date(options[:date], place_filter: place_filter)
|
54
|
+
printer = TreerfulScanner::HtmlPrinter.new(time_tables, options[:date])
|
55
|
+
print printer.print
|
56
|
+
when 'png'
|
57
|
+
time_tables = client.search_by_date(options[:date], place_filter: place_filter)
|
58
|
+
printer = TreerfulScanner::PngPrinter.new(time_tables, options[:date])
|
59
|
+
print printer.print
|
60
|
+
else
|
61
|
+
raise "unknown format: #{options[:format]}"
|
27
62
|
end
|
@@ -10,7 +10,7 @@ module TreerfulScanner
|
|
10
10
|
@parser = Parser.new
|
11
11
|
end
|
12
12
|
|
13
|
-
def search_by_date(date)
|
13
|
+
def search_by_date(date, place_filter: -> { true })
|
14
14
|
date = date.to_s
|
15
15
|
result = []
|
16
16
|
EventMachine.run do
|
@@ -18,7 +18,7 @@ module TreerfulScanner
|
|
18
18
|
client = conn.get path: '/space/result', keepalive: true
|
19
19
|
client.callback do
|
20
20
|
multi = EventMachine::MultiRequest.new
|
21
|
-
places = @parser.parse_places(client.response)
|
21
|
+
places = @parser.parse_places(client.response).select(&place_filter)
|
22
22
|
places.each do |place|
|
23
23
|
client2 = conn.get path: "/space/allowTimes?id=#{place.id}&date=#{date}", keepalive: true
|
24
24
|
client2.callback do
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'treerful_scanner'
|
4
|
+
require 'erb'
|
5
|
+
|
6
|
+
module TreerfulScanner
|
7
|
+
class HtmlPrinter < Printer
|
8
|
+
attr_reader :date
|
9
|
+
|
10
|
+
def initialize(*)
|
11
|
+
super
|
12
|
+
@template = IO.read("#{__dir__}/html_printer/template.erb")
|
13
|
+
end
|
14
|
+
|
15
|
+
def print
|
16
|
+
erb = ERB.new(@template)
|
17
|
+
erb.result(binding)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def tbody
|
23
|
+
"<tbody>#{@time_tables.map(&method(:to_tr)).join}</tbody>"
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_tr(time_table)
|
27
|
+
result = String.new
|
28
|
+
result << "<tr><td><a href=\"https://www.treerful.com/space/#{time_table.place.id}\" rel=\"noopener noreferrer\" target=\"_blank\">#{time_table.place.name}</a></td><td>#{time_table.place.volume}</td>"
|
29
|
+
result <<
|
30
|
+
binary_form(time_table).each_char.map do |c|
|
31
|
+
case c
|
32
|
+
when ' ' then '<td></td>'
|
33
|
+
when 'O' then '<td class="available"></td>'
|
34
|
+
end
|
35
|
+
end.join
|
36
|
+
result << '</tr>'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'treerful_scanner'
|
4
|
+
require 'tmpdir'
|
5
|
+
require 'English'
|
6
|
+
|
7
|
+
module TreerfulScanner
|
8
|
+
class PngPrinter < Printer
|
9
|
+
CHROME_BINS = [
|
10
|
+
'chromium-browser',
|
11
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
|
12
|
+
].freeze
|
13
|
+
WINDOW_HEIGHT_OFFSET = 200
|
14
|
+
WINDOW_CELL_HEIGHT = 24.5
|
15
|
+
|
16
|
+
def print
|
17
|
+
html_printer = HtmlPrinter.new(@time_tables, @date)
|
18
|
+
Dir.mktmpdir do |dir|
|
19
|
+
html_path = "#{dir}/output.html"
|
20
|
+
IO.write(html_path, html_printer.print)
|
21
|
+
chrome_bin = CHROME_BINS.find(&File.method(:exist?)) || ENV['CHROME_BIN']
|
22
|
+
raise 'ENV["CHROME_BIN"] is nil' unless chrome_bin
|
23
|
+
window_height = WINDOW_HEIGHT_OFFSET + WINDOW_CELL_HEIGHT * @time_tables.length
|
24
|
+
Dir.chdir(dir) do
|
25
|
+
raise "chrome exited with status #{$CHILD_STATUS}" unless system(
|
26
|
+
chrome_bin,
|
27
|
+
'--headless',
|
28
|
+
'--disable-gpu',
|
29
|
+
'--disable-software-rasterizer',
|
30
|
+
'--disable-dev-shm-usage',
|
31
|
+
'--no-sandbox',
|
32
|
+
'--screenshot',
|
33
|
+
'--hide-scrollbars',
|
34
|
+
"--window-size=1280,#{window_height}",
|
35
|
+
html_path
|
36
|
+
)
|
37
|
+
IO.read('screenshot.png', mode: 'rb')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -17,18 +17,21 @@ module TreerfulScanner
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def row(time_table)
|
20
|
-
|
21
|
-
|
20
|
+
fixed_string(time_table.place.name, TITLE_WIDTH) +
|
21
|
+
binary_form(time_table).each_char.map { |i| format('%-2s', i) }.join +
|
22
|
+
"\n"
|
23
|
+
end
|
24
|
+
|
25
|
+
def binary_form(time_table)
|
26
|
+
result = String.new
|
27
|
+
result << ' ' * (BEGIN_HOUR..END_HOUR).size * 2
|
22
28
|
offset = BEGIN_HOUR * 60
|
23
29
|
time_table.durations.each do |duration|
|
24
30
|
start = (duration.from - offset) / 30
|
25
31
|
finish = (duration.to - offset) / 30
|
26
|
-
|
32
|
+
result[start...finish] = 'O' * (finish - start)
|
27
33
|
end
|
28
|
-
|
29
|
-
fixied_string(time_table.place.name, TITLE_WIDTH) +
|
30
|
-
tmp.each_char.map { |i| format('%-2s', i) }.join +
|
31
|
-
"\n"
|
34
|
+
result
|
32
35
|
end
|
33
36
|
|
34
37
|
def print
|
@@ -42,7 +45,7 @@ module TreerfulScanner
|
|
42
45
|
|
43
46
|
private
|
44
47
|
|
45
|
-
def
|
48
|
+
def fixed_string(string, width)
|
46
49
|
length = string.length + string.scan(/\p{Han}|\p{Katakana}|\p{Hiragana}\p{Hangul}/).length
|
47
50
|
diff = width - length
|
48
51
|
return string + (' ' * diff) if diff > 0
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: treerful_scanner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jian Weihang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -91,8 +91,10 @@ files:
|
|
91
91
|
- lib/treerful_scanner.rb
|
92
92
|
- lib/treerful_scanner/client.rb
|
93
93
|
- lib/treerful_scanner/duration.rb
|
94
|
+
- lib/treerful_scanner/html_printer.rb
|
94
95
|
- lib/treerful_scanner/parser.rb
|
95
96
|
- lib/treerful_scanner/place.rb
|
97
|
+
- lib/treerful_scanner/png_printer.rb
|
96
98
|
- lib/treerful_scanner/printer.rb
|
97
99
|
- lib/treerful_scanner/time_table.rb
|
98
100
|
homepage: https://github.com/tonytonyjan/treerful_scanner
|