komic-cli 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbdc149c735015311df034e0f1c60605de5248aa
4
- data.tar.gz: 1d81e3e6f749dde7578d49c881e4bd40e8c114d8
3
+ metadata.gz: c1b54ef1bf644810e847909c33d2b347b9e4ccdf
4
+ data.tar.gz: 396f898e32192110cfeffa1948570d5497fd65b8
5
5
  SHA512:
6
- metadata.gz: 51ff0497fd3f1f643094e50ab0fb2298cfdc7c64bd5d20fd991c0e863e51b58662f6ad2eecfd1a1156298fcc99efe319c46c8348b8669344b5e710dfdd015389
7
- data.tar.gz: 6b10b60da03d9e1da1d58b09c6abcb3dd248d8af724c3851e7fb2193252a3cc0eaaea824441242bf1a4241394a1f547f46164b6f3445e1026cca39b41d44f174
6
+ metadata.gz: 8353a7b28ebc04d5d1cce7115cd42a7bc55d2ed9a7cd89c456bbcfafac842c4d6ddde2f51aef9393528a5b0bff7f0116a17e805dce715510ae96fd997347706b
7
+ data.tar.gz: a1af1a739be51e7d5d8bfe1b52a0677bac81da430a2b0ad4097172a4a4f37c177bce2fc534e71f5f3f8253ff42b8e6d9c3069e463c3ebbc0e23ada46324440d7
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ ## Requirements
2
+
3
+ ### 需要安装带有 rsvg 的 imagemagick 用来处理 SVG
4
+
5
+ MAC OS 下
6
+ ```bash
7
+ brew install imagemagick --with-librsvg
8
+ ```
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ $ gem install komic-cli
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```
19
+ Commands:
20
+ komic download URL # 从 url 下载画册数据 (* 目前只支持豆瓣相册)
21
+ komic mock # 生成虚拟的画册数据
22
+ komic help [COMMAND] # Describe available commands or one specific command
23
+
24
+ Usage:
25
+ komic mock
26
+
27
+ Options:
28
+ [--page-number=PAGE-NUMBER] # 设定页数
29
+ # Default: 6
30
+ [--size=SIZE] # 设定尺寸
31
+ # Default: 700-1024x900-1000
32
+ [--name=NAME] # 设定文件夹名
33
+ # Default: mock
34
+ ```
data/komic-cli.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |gem|
26
26
  gem.add_runtime_dependency 'mini_magick', '~> 4.2'
27
27
  gem.add_runtime_dependency 'jbuilder', '~> 2.3'
28
28
  gem.add_runtime_dependency 'json-schema', '~> 2.5'
29
+ gem.add_runtime_dependency 'ruby-progressbar', '~> 1.7'
29
30
 
30
31
  gem.add_development_dependency 'bundler', '~> 1.0'
31
32
  gem.add_development_dependency 'rake', '~> 0.8'
data/lib/komic/cli.rb CHANGED
@@ -22,6 +22,12 @@ module Komic
22
22
  generator.create_package({ images: images, meta: { name: title } }, options)
23
23
  end
24
24
 
25
+ desc "version", "显示版本"
26
+ def version
27
+ say "Komic #{Komic::VERSION}"
28
+ say "Komic's content.json version #{Komic::CONTENT_JSON_VERSION}"
29
+ end
30
+
25
31
  desc "mock", "生成虚拟的画册数据"
26
32
  option :'page-number', default: 6, desc: "设定页数"
27
33
  option :size, default: "700-1024x900-1000", desc: "设定尺寸"
@@ -2,6 +2,7 @@ require 'mechanize'
2
2
  require 'fileutils'
3
3
  require 'mime/types'
4
4
  require 'tmpdir'
5
+ require 'ruby-progressbar'
5
6
 
6
7
  module Komic
7
8
  module Crawler
@@ -10,6 +11,7 @@ module Komic
10
11
  @mechanize = Mechanize.new
11
12
  @file_index = 0
12
13
  @tmpdir = Dir.mktmpdir
14
+ @willbe_downloaded = []
13
15
  end
14
16
 
15
17
  def get_crawled_result(album_home_url)
@@ -36,8 +38,22 @@ module Komic
36
38
  next_link_url = next_link["href"]
37
39
  end
38
40
 
39
- image_pathes = (Dir.entries(@tmpdir) - %w[. ..]).map do |path|
40
- File.join(@tmpdir, path)
41
+ # green background
42
+ color_code = "\e[0m\e[32m\e[7m\e[1m"
43
+ reset_code = "\e[0m"
44
+ progress_status = "#{color_code} %p%% #{reset_code}"
45
+
46
+ bar = ProgressBar.create( :format => "%a %bᗧ%i #{progress_status} %t",
47
+ :title => 'Download image from douban',
48
+ :progress_mark => ' ',
49
+ :remainder_mark => '・',
50
+ :total => @willbe_downloaded.size,
51
+ :starting_at => 0 )
52
+
53
+ image_pathes = @willbe_downloaded.map do |url|
54
+ image_path = download_image url
55
+ bar.increment
56
+ image_path
41
57
  end
42
58
 
43
59
  return album_title, image_pathes
@@ -62,10 +78,10 @@ module Komic
62
78
 
63
79
  unless link_to_large.nil?
64
80
  @mechanize.get(link_to_large['href']) do |page|
65
- download_image(page.at('#pic-viewer img')["src"])
81
+ @willbe_downloaded.push(page.at('#pic-viewer img')["src"])
66
82
  end
67
83
  else
68
- download_image(thumb_photo_url)
84
+ @willbe_downloaded.push(thumb_photo_url)
69
85
  end
70
86
  end
71
87
 
@@ -73,11 +89,12 @@ module Komic
73
89
  resource = @mechanize.get(photo_url)
74
90
  content_type = resource["content-type"]
75
91
  mime_type = MIME::Types[resource["content-type"]].first
76
- resource.save(File.expand_path( \
77
- [@file_index, mime_type.extensions.first].join('.'), @tmpdir))
92
+ image_path = File.expand_path( \
93
+ [@file_index, mime_type.extensions.first].join('.'), @tmpdir)
94
+ resource.save(image_path)
78
95
  @file_index = @file_index + 1
79
- p 'current image is ' + @file_index.to_s
80
96
  sleep 2
97
+ image_path
81
98
  end
82
99
  end
83
100
  end
@@ -7,7 +7,7 @@ require 'mini_magick'
7
7
  require 'base64'
8
8
 
9
9
  require 'komic/version'
10
- require 'komic/generator/helpers'
10
+ require 'komic/utils'
11
11
 
12
12
  module Komic
13
13
  class ThumbnailsBuilder
@@ -101,7 +101,7 @@ module Komic
101
101
  end
102
102
 
103
103
  def create_fake_image(filename, size)
104
- size = Helpers.parse_size(size)
104
+ size = Utils.parse_size(size)
105
105
  file = Tempfile.new([filename, '.svg'])
106
106
  image_width = size[:width]
107
107
  image_height = size[:height]
@@ -141,7 +141,7 @@ module Komic
141
141
  end
142
142
 
143
143
  files.map do |image, index|
144
- image[:src] = Helpers.get_relativepath_as(image[:src], root_dir)
144
+ image[:src] = Utils.get_relative_path(image[:src], root_dir)
145
145
  image
146
146
  end
147
147
 
@@ -151,12 +151,13 @@ module Komic
151
151
  author: { name: 'TEST' },
152
152
  thumbnails: {
153
153
  height: 200,
154
- path: Helpers.get_relativepath_as(thumbnail_path, root_dir),
154
+ path: Utils.get_relative_path(thumbnail_path, root_dir),
155
155
  },
156
156
  }
157
157
 
158
- # TODO(yangqing): require deep_merge, dirty but work
159
- meta.merge!(data[:meta]) unless data[:meta].nil?
158
+ unless data[:meta].nil?
159
+ meta = Utils.deep_merge_hashes(meta, data[:meta])
160
+ end
160
161
 
161
162
  content_builder = ContentBuilder.new(meta, files)
162
163
  File.open(File.join(root_dir, './content.json'), 'w') do |file|
@@ -0,0 +1,49 @@
1
+ module Komic
2
+ module Utils extend self
3
+ # Merges a master hash with another hash, recursively.
4
+ #
5
+ # master_hash - the "parent" hash whose values will be overridden
6
+ # other_hash - the other hash whose values will be persisted after the merge
7
+ #
8
+ # This code was lovingly stolen from some random gem:
9
+ # http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
10
+ #
11
+ # Thanks to whoever made it.
12
+ def deep_merge_hashes(master_hash, other_hash)
13
+ target = master_hash.dup
14
+
15
+ other_hash.each_key do |key|
16
+ if other_hash[key].is_a? Hash and target[key].is_a? Hash
17
+ target[key] = Utils.deep_merge_hashes(target[key], other_hash[key])
18
+ next
19
+ end
20
+
21
+ target[key] = other_hash[key]
22
+ end
23
+
24
+ target
25
+ end
26
+
27
+ def parse_size(size)
28
+ width_range, height_range = size.split('x')
29
+ width, height = [width_range, height_range].map do |range|
30
+ unless range.nil?
31
+ min, max = range.split('-')
32
+ r = range.to_i
33
+ unless max.nil?
34
+ r = Random.rand(min.to_i...max.to_i)
35
+ end
36
+ r
37
+ end
38
+ end
39
+ return { width: width, height: height }
40
+ end
41
+
42
+ def get_relative_path(path, root)
43
+ File.join('./',
44
+ Pathname.new(path).relative_path_from(
45
+ Pathname.new(root)
46
+ ))
47
+ end
48
+ end
49
+ end
data/lib/komic/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Komic
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  CONTENT_JSON_VERSION = '0.1.0'
4
4
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require 'rspec'
2
- require 'komic/cli/version'
2
+ require 'komic/version'
3
3
 
4
- include Komic::Cli
4
+ include Komic
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ require 'komic/utils'
3
+
4
+ describe Komic::Utils do
5
+ it "get relative path" do
6
+ relative_path = subject.get_relative_path("/user/x/abc", "/user/x")
7
+ expect(relative_path).to be_eql("./abc")
8
+ end
9
+
10
+ before { allow(Random).to receive(:rand).and_return(700) }
11
+ it "parse `komic mock`'s size opt" do
12
+ expect(subject.parse_size("700-800x900")).to be_eql({ width: 700, height: 900 })
13
+ expect(subject.parse_size("800x700-900")).to be_eql({ width: 800, height: 700 })
14
+ expect(subject.parse_size("800x900")).to be_eql({ width: 800, height: 900 })
15
+ end
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: komic-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hxgdzyuyi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-03 00:00:00.000000000 Z
11
+ date: 2015-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '2.5'
97
+ - !ruby/object:Gem::Dependency
98
+ name: ruby-progressbar
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.7'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.7'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: bundler
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -177,7 +191,7 @@ files:
177
191
  - ChangeLog.rdoc
178
192
  - Gemfile
179
193
  - LICENSE.txt
180
- - README.rdoc
194
+ - README.md
181
195
  - Rakefile
182
196
  - bin/komic
183
197
  - komic-cli.gemspec
@@ -186,10 +200,10 @@ files:
186
200
  - lib/komic/crawler/douban.rb
187
201
  - lib/komic/generator/content.schema.json
188
202
  - lib/komic/generator/generator.rb
189
- - lib/komic/generator/helpers.rb
203
+ - lib/komic/utils.rb
190
204
  - lib/komic/version.rb
191
- - spec/cli_spec.rb
192
205
  - spec/spec_helper.rb
206
+ - spec/utils_spec.rb
193
207
  homepage: https://rubygems.org/gems/komic-cli
194
208
  licenses:
195
209
  - MIT
@@ -215,5 +229,5 @@ signing_key:
215
229
  specification_version: 4
216
230
  summary: komic-cli
217
231
  test_files:
218
- - spec/cli_spec.rb
219
232
  - spec/spec_helper.rb
233
+ - spec/utils_spec.rb
data/README.rdoc DELETED
@@ -1,27 +0,0 @@
1
- = komic-cli
2
-
3
- * {Homepage}[https://rubygems.org/gems/komic-cli]
4
- * {Documentation}[http://rubydoc.info/gems/komic-cli/frames]
5
- * {Email}[mailto:hxgdzyuyi at gmail.com]
6
-
7
- == Description
8
-
9
- TODO: Description
10
-
11
- == Features
12
-
13
- == Examples
14
-
15
- require 'komic/cli'
16
-
17
- == Requirements
18
-
19
- == Install
20
-
21
- $ gem install komic-cli
22
-
23
- == Copyright
24
-
25
- Copyright (c) 2015 hxgdzyuyi
26
-
27
- See LICENSE.txt for details.
@@ -1,29 +0,0 @@
1
- require 'pathname'
2
-
3
- module Komic
4
- class Generator
5
- module Helpers
6
- def self.parse_size(size)
7
- width_range, height_range = size.split('x')
8
- width, height = [width_range, height_range].map do |range|
9
- unless range.nil?
10
- min, max = range.split('-')
11
- r = range.to_i
12
- unless max.nil?
13
- r = Random.rand(min.to_i...max.to_i)
14
- end
15
- r
16
- end
17
- end
18
- return { width: width, height: height }
19
- end
20
-
21
- def self.get_relativepath_as(path, root)
22
- File.join('./',
23
- Pathname.new(path).relative_path_from(
24
- Pathname.new(root)
25
- ))
26
- end
27
- end
28
- end
29
- end
data/spec/cli_spec.rb DELETED
@@ -1,8 +0,0 @@
1
- require 'spec_helper'
2
- require 'komic/cli'
3
-
4
- describe Komic::Cli do
5
- it "should have a VERSION constant" do
6
- subject.const_get('VERSION').should_not be_empty
7
- end
8
- end