seiya 0.0.6 → 0.0.7

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: 0d5ca500e310f4b98f85a00120047c72d64cf611
4
- data.tar.gz: 63db6b3c6d4258678fb808a3b1c2e765af3592f2
3
+ metadata.gz: b46494f0bfaec4f0a78fd9cf6cc9ac06c306b9f7
4
+ data.tar.gz: 4fbc8f4febaddf6f20f7f04ac4ae7dd43d86c20e
5
5
  SHA512:
6
- metadata.gz: cdfcef0a38a291788696f3b220b0700e0546e3169ac2c2853b47317bd8af4129d8f3b33e30c2b5b051bab4e6e4c5dfb926dafe1dc2488273abe09be2ff115ea7
7
- data.tar.gz: 24cdc21f897340a4e51854f79c4a36a069cb42284fa1a40fca260371933ae85dda20d7051d9c68953f4de4384dafe7a567c59d5d786153db36cb74a52c9fb11c
6
+ metadata.gz: 43afa2ca3d3c85cf28b8ac0985f37cd56b2c0001a1d69ba108dd4b7679410ebbbeb8cf2d62a543c19d2f29aa38ec0ef9248ac18cf2942d703fc143b09b447dee
7
+ data.tar.gz: 797a0a441cf7d7b129e5958d09fc20902ed75040b320a5b555cd2cf174ef5b58c62c2540b2ca8f65b31409e73bae3a54987b029191a88d2762674f13717e95a1
data/bin/seiya CHANGED
@@ -3,71 +3,14 @@
3
3
  $:.unshift File.expand_path '.'
4
4
  $:.unshift File.expand_path '../../lib', __FILE__
5
5
  require 'seiya'
6
- require 'optparse'
7
6
 
8
- options = {}
7
+ command = ARGV.shift
9
8
 
10
- OptionParser.new do |opts|
11
- opts.banner = 'Usage: seiya [options]'
12
-
13
- opts.on '-v', '--version', 'seiya version' do |v|
14
- options[:version] = v
15
- end
16
-
17
- opts.on '-tTask', '--task=Task', 'run a seiya task' do |t|
18
- options[:task] = t
19
- end
20
-
21
- opts.on '-aArg', '--argument=Arg', 'send argument to seiya task' do |a|
22
- options[:args] = [] unless options[:args]
23
- options[:args] << a
24
- end
25
-
26
- opts.on '-cTask', '--create=Task', 'create a seiya task' do |t|
27
- options[:create] = t
28
- end
29
- end.parse!
30
-
31
- def extend_load_path(path)
32
- Dir.foreach path do |f|
33
- unless %w(. ..).include? f
34
- if File.directory? File.join path, f
35
- new_path = File.join path, f
36
- extend_load_path new_path
37
- elsif f == 'tasks.rb'
38
- $:.unshift path
39
- break
40
- end
41
- end
42
- end
43
- end
44
-
45
- if options.key? :task
46
- extend_load_path Dir.pwd
47
-
48
- begin
49
- require 'tasks'
50
- rescue
51
- puts 'No tasks!'
52
- exit!
53
- end
54
-
55
- Seiya.setup
56
- task_name = options[:task]
57
- begin
58
- task = Seiya.get_task task_name
59
- rescue NameError
60
- puts "No task named: #{task_name}"
61
- exit!
62
- end
63
- task.run
64
- end
65
-
66
- if options.key? :version
67
- puts Seiya::VERSION
68
- end
69
-
70
- if options.key? :create
71
- task_name = options[:create]
72
- Seiya.gen_task_file task_name
9
+ case command
10
+ when '-v', '--version'
11
+ puts Seiya::VERSION
12
+ when '-h', '--help'
13
+ Seiya.usage
14
+ else
15
+ Seiya.run_command command, *ARGV
73
16
  end
@@ -0,0 +1,22 @@
1
+ module Seiya
2
+ class Command
3
+ def summary
4
+ 'I am a seiya command'
5
+ end
6
+
7
+ def usage
8
+ 'no usage'
9
+ end
10
+
11
+ def run
12
+ end
13
+
14
+ def run!(*args)
15
+ if args.include? '-h' or args.include? '--help'
16
+ puts usage
17
+ return
18
+ end
19
+ run *args
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ require 'seiya/command'
2
+ require 'optparse'
3
+
4
+ module Contrib
5
+ module Commands
6
+ class Crawl < Seiya::Command
7
+ def summary
8
+ 'Crawl a task'
9
+ end
10
+
11
+ def usage
12
+ 'Usage
13
+ =====
14
+ seiya crawl <task_name> [options]
15
+
16
+ Run a task
17
+
18
+ Options
19
+ =======
20
+ --help, -h show this help message and exit
21
+ -a NAME=VALUE set task argument (may be repeated)'
22
+ end
23
+
24
+ def run(*args)
25
+ require 'tasks'
26
+
27
+ task_name = args.shift
28
+ if task_name.nil?
29
+ puts 'Need a task_name'
30
+ exit!
31
+ end
32
+ task_class = Seiya.get_task_class task_name
33
+
34
+ options = {}
35
+ OptionParser.new do |opts|
36
+ opts.banner = 'Usage: seiya [options]'
37
+
38
+ opts.on '-aArg', '--argument=Arg', 'send argument to seiya task' do |a|
39
+ options[:args] = [] unless options[:args]
40
+ options[:args] << a
41
+ end
42
+ end.parse!
43
+
44
+ _args = options[:args] ? options[:args] : []
45
+
46
+ task = task_class.new *_args
47
+
48
+ task.run
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,31 @@
1
+ require 'seiya/command'
2
+ module Contrib
3
+ module Commands
4
+ class Create < Seiya::Command
5
+ def summary
6
+ 'Create a new project'
7
+ end
8
+
9
+ def usage
10
+ 'Usage
11
+ =====
12
+ seiya create <project_name>
13
+
14
+ Create new project
15
+
16
+ Options
17
+ =======
18
+ --help, -h show this help message and exit'
19
+ end
20
+
21
+ def run(*args)
22
+ project_name = args.shift
23
+ if project_name.nil?
24
+ puts 'Need a project_name!'
25
+ exit!
26
+ end
27
+ Seiya.gen_project_file project_name
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ require_relative './commands/create'
2
+ require_relative './commands/crawl'
data/lib/seiya/contrib.rb CHANGED
@@ -1 +1,2 @@
1
1
  require 'seiya/contrib/pipelines'
2
+ require 'seiya/contrib/commands'
@@ -3,5 +3,6 @@ module Seiya
3
3
  PIPELINES = {
4
4
  'seiya/contrib|Contrib::Pipelines::BasePipeline' => 2
5
5
  }
6
+ COMMANDS = 'seiya/contrib|Contrib::Commands'
6
7
  end
7
8
  end
@@ -0,0 +1,9 @@
1
+ class String
2
+ def underscore
3
+ self.gsub(/::/, '/').
4
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
+ tr('-', '_').
7
+ downcase
8
+ end
9
+ end
data/lib/seiya/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Seiya
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
data/lib/seiya.rb CHANGED
@@ -5,6 +5,8 @@ require 'seiya/task'
5
5
  require 'seiya/item'
6
6
  require 'seiya/pipeline'
7
7
  require 'seiya/settings'
8
+ require 'seiya/command'
9
+ require 'seiya/support'
8
10
 
9
11
  module Seiya
10
12
  extend self
@@ -15,7 +17,40 @@ module Seiya
15
17
  end
16
18
  end
17
19
 
18
- def setup(conf_file: 'seiya.ini')
20
+ def get_const(require_str, const_str)
21
+ begin
22
+ require require_str
23
+ rescue LoadError => e
24
+ puts e
25
+ puts "Cannot load #{require_str}"
26
+ exit!
27
+ end
28
+
29
+ begin
30
+ Util.get_const const_str
31
+ rescue NameError
32
+ puts "Cannot get #{const_str}"
33
+ exit!
34
+ end
35
+ end
36
+
37
+ def extend_load_path(path)
38
+ Dir.foreach path do |f|
39
+ unless %w(. ..).include? f
40
+ if File.directory? File.join path, f
41
+ new_path = File.join path, f
42
+ extend_load_path new_path
43
+ elsif f == 'tasks.rb'
44
+ $:.unshift path
45
+ break
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def setup(conf_file: 'seiya.ini', load_path: Dir.pwd)
52
+ extend_load_path load_path
53
+
19
54
  require 'inifile'
20
55
  require 'seiya/util'
21
56
  conf = IniFile.load conf_file
@@ -23,49 +58,101 @@ module Seiya
23
58
  settings_require_str, settings_const_str = settings_file.split '|'
24
59
  require settings_require_str
25
60
 
61
+ pipelines = Settings::PIPELINES
26
62
  begin
27
- pipelines = Util.get_const "#{settings_const_str.empty? ? 'Settings' : settings_const_str}::PIPELINES"
63
+ pipelines.merge! Util.get_const "#{settings_const_str}::PIPELINES"
28
64
  rescue NameError
29
- pipelines = {}
65
+ # ignored
30
66
  end
31
67
 
32
- pipelines.merge! Settings::PIPELINES
33
-
34
68
  pipelines = pipelines.sort_by { |_, v| v }.to_h
35
69
 
36
70
  @pipelines = pipelines.keys.map do |k|
37
- require_str, class_name = k.split('|')
38
- begin
39
- require require_str
40
- clazz = Util.get_const class_name
41
- clazz.new
42
- rescue LoadError
43
- puts 'current directory is not a spider directory!'
44
- exit!
71
+ require_str, const_str = k.split '|'
72
+ clazz = get_const require_str, const_str
73
+ clazz.new
74
+ end
75
+
76
+ commands = [Settings::COMMANDS]
77
+ begin
78
+ commands << Util.get_const("#{settings_const_str}::COMMANDS")
79
+ rescue NameError
80
+ # ignored
81
+ end
82
+
83
+ @commands = commands.map do |dir|
84
+ require_str, const_str = dir.split '|'
85
+ _module = get_const require_str, const_str
86
+ clazz_symbols = _module.constants.select do |c|
87
+ const = _module.const_get(c)
88
+ const.is_a? Class and const < Command
89
+ end
90
+ clazz_symbols.map do |c|
91
+ clazz = _module.const_get c
92
+ [clazz.name.underscore.split('/').last.to_sym, clazz.new]
45
93
  end
94
+ end.flatten(1).to_h
95
+ end
96
+
97
+ def run_command(command, *args)
98
+ if @commands.nil?
99
+ setup
100
+ end
101
+
102
+ command_sym = command.to_sym
103
+ unless @commands.key? command_sym
104
+ puts "No command: #{command}"
105
+ exit!
106
+ end
107
+
108
+ @commands[command_sym].run! *args
109
+ end
110
+
111
+ def usage
112
+ if @commands.nil?
113
+ setup
46
114
  end
115
+ available_commands = @commands.map do |k, v|
116
+ ' %-14s%-30s' % [k.to_s, v.summary]
117
+ end.join("\n")
118
+
119
+ puts %(Seiya #{VERSION}
120
+
121
+ Usage:
122
+ seiya <command> [options] [args]
123
+
124
+ Available commands:
125
+ #{available_commands}
126
+
127
+ Use "seiya <command> -h" to see more info about a command
128
+ )
47
129
  end
48
130
 
49
- def get_task(task_name)
131
+ def get_task_class(task_name)
50
132
  task_name = 'Tasks::' << task_name unless task_name.include? '::'
51
- clazz = Util::get_const task_name
52
- clazz.new
133
+ begin
134
+ Util::get_const task_name
135
+ rescue NameError
136
+ p $:
137
+ puts "Cannot get task: #{task_name}"
138
+ exit!
139
+ end
53
140
  end
54
141
 
55
- def gen_task_file(task_name)
56
- base_path = "#{task_name}/#{task_name}"
142
+ def gen_project_file(project_name)
143
+ base_path = "#{project_name}/#{project_name}"
57
144
  FileUtils.mkpath "#{base_path}"
58
145
  FileUtils.mkpath "#{base_path}/tasks"
59
146
  FileUtils.mkpath "#{base_path}/items"
60
147
  FileUtils.mkpath "#{base_path}/pipelines"
61
- File.write("#{task_name}/seiya.ini",
148
+ File.write("#{project_name}/seiya.ini",
62
149
  %([global]
63
- settings = #{task_name}/settings|Settings
150
+ settings = #{project_name}/settings|Settings
64
151
  ))
65
152
  File.write("#{base_path}/settings.rb",
66
153
  %(module Settings
67
154
  PIPELINES = {
68
- '#{task_name}/pipelines|Pipelines::Test' => 10,
155
+ '#{project_name}/pipelines|Pipelines::Test' => 10,
69
156
  }
70
157
  end))
71
158
  File.write("#{base_path}/items.rb", %(require 'items/test'))
@@ -0,0 +1,8 @@
1
+ require 'seiya'
2
+ module Commands
3
+ class Sing <Seiya::Command
4
+ def run
5
+ puts 'I am a singer!'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1 @@
1
+ require_relative './commands/sing'
@@ -3,4 +3,5 @@ module Settings
3
3
  'aa/pipelines/t|Pipelines::A' => 1,
4
4
  'aa/pipelines/t|Pipelines::B' => 2,
5
5
  }
6
+ COMMANDS = 'aa/commands|Commands'
6
7
  end
@@ -3,7 +3,8 @@ require_relative '../items'
3
3
 
4
4
  module Tasks
5
5
  class Test < Seiya::Task
6
- def initialize
6
+ def initialize *args
7
+ puts args
7
8
  @start_urls = ('a'..'d').map do |w|
8
9
  'http://www.baidu.com/?key=' << w
9
10
  end
@@ -13,7 +14,7 @@ module Tasks
13
14
  item = Items::Test.new
14
15
  item[:url] = response.url
15
16
  enum.yield item
16
- request = Seiya::Request.new 'http://www.collipa.com'
17
+ request = Seiya::Request.new 'http://www.weibo.com'
17
18
  request.register &method(:other_parse)
18
19
  enum.yield request
19
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seiya
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - yetone
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-05 00:00:00.000000000 Z
11
+ date: 2015-02-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: i@yetone.net
@@ -20,7 +20,11 @@ files:
20
20
  - README.md
21
21
  - bin/seiya
22
22
  - lib/seiya.rb
23
+ - lib/seiya/command.rb
23
24
  - lib/seiya/contrib.rb
25
+ - lib/seiya/contrib/commands.rb
26
+ - lib/seiya/contrib/commands/crawl.rb
27
+ - lib/seiya/contrib/commands/create.rb
24
28
  - lib/seiya/contrib/pipelines.rb
25
29
  - lib/seiya/item.rb
26
30
  - lib/seiya/pipeline.rb
@@ -28,9 +32,12 @@ files:
28
32
  - lib/seiya/response.rb
29
33
  - lib/seiya/scheduler.rb
30
34
  - lib/seiya/settings.rb
35
+ - lib/seiya/support.rb
31
36
  - lib/seiya/task.rb
32
37
  - lib/seiya/util.rb
33
38
  - lib/seiya/version.rb
39
+ - sample/test/aa/commands.rb
40
+ - sample/test/aa/commands/sing.rb
34
41
  - sample/test/aa/items.rb
35
42
  - sample/test/aa/pipelines/t.rb
36
43
  - sample/test/aa/settings.rb