arb-xmu-course 2.1.1 → 2.3.0

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
  SHA1:
3
- metadata.gz: b6eda3309e782a73522b07b1da16a94f540e8db3
4
- data.tar.gz: 5f838bf91a12911f9bb51767d58fd7203d7b2b14
3
+ metadata.gz: 1531ba75e5a898b024b5003718ff236d3bb3da34
4
+ data.tar.gz: 5fd20229501e38a2b358e019cde76bbdac95495e
5
5
  SHA512:
6
- metadata.gz: 9b4b660bf377c3f5a9750866277bfe40c8f307045ce392871413068ef9b01ce54ec9dbde216c716a11de4c43ac87ec392d4a1b6b57c686adf5eccb191dd278c3
7
- data.tar.gz: 2b486e12a2e89959153df2f8c7a85f315f16934d606f6ba688d23b58c3361a98f8522c56308e9f3f0a6880ba7c7cf84ccc316ebca0c8689ec282468e7d105d62
6
+ metadata.gz: 7e0454e279c13ee976735df9f6f065b3718d48bcbd45d2d84952e2cc809a17cc44a1aed13ea532ded10ea0c956b36968c9aef8972b53bd5012c9adcc2d2c128d
7
+ data.tar.gz: abc3b0b9fe0ebf9ee2960f9366f3b4438bfaa6078fce3f897adc3ffcd113fee6f79bf6c022fba901e9f80de4956b65015eeb7c1e7d5798fbaf8063017768781f
data/bin/arb-xmu-course CHANGED
@@ -9,44 +9,6 @@ require 'arb/xmu/course'
9
9
  include Arb::Util
10
10
  include Arb::Xmu::Course
11
11
 
12
- TOKEN_FILE_NAME = 'encrypted_token'
13
- TOKEN_SEPARATOR = ':'
14
- CLIENT = HTTPClient.new
15
-
16
- opts = Slop.parse do |o|
17
- o.bool *%w{-h --help}, '显示帮助'
18
- o.bool *%w{-c --clear}, '清除本地账户信息'
19
- o.bool *%w{-r --reset}, '重置账户信息'
20
-
21
- #院选课程 xxlx=4
22
- o.bool *%w{-s --school}, '院选选课模式'
23
- #校选课程 xxlx=3
24
- o.bool *%w{-g --general}, '校选选课模式'
25
- #公共课程 xxlx=5
26
- o.bool *%w{-p --public}, '公共课选课模式'
27
-
28
- o.bool *%w{-v --version}, '显示当前版本'
29
- end
30
-
31
- case true
32
- when opts.version?
33
- puts Arb::Xmu::Course::VERSION
34
- when opts.help?
35
- puts(opts) && exit
36
- when opts.clear?
37
- File.delete(TOKEN_FILE_NAME) && exit
38
- when opts.reset?
39
- set_up_account
40
- when opts.school?
41
- select_school_elective(CLIENT)
42
- when opts.general?
43
- select_general_elective(CLIENT)
44
- when opts.public?
45
- select_public(CLIENT)
46
- else
47
- puts(opts) && exit
48
- end
49
-
50
12
  def select_school_elective(client)
51
13
  select_course_common client, 'http://bkxk.xmu.edu.cn/xsxk/yxxx.html?pagination=5000', 4
52
14
  end
@@ -55,30 +17,42 @@ def select_general_elective(client)
55
17
  select_course_common client, 'http://bkxk.xmu.edu.cn/xsxk/qxxxx.html?pagination=5000', 3
56
18
  end
57
19
 
20
+ def select_general_required(client)
21
+ select_course_common client, 'http://bkxk.xmu.edu.cn/xsxk/qxxbxk.html?pagination=5000', 1
22
+ end
23
+
58
24
  def select_public(client)
59
25
  select_course_common client, 'http://bkxk.xmu.edu.cn/xsxk/ggk.html?pagination=5000', 5
60
26
  end
61
27
 
28
+ def select_school_required(client)
29
+ select_course_common client, 'http://bkxk.xmu.edu.cn/xsxk/yxbxk.html?pagination=5000', 2
30
+ end
31
+
32
+ def select_across(client)
33
+ select_course_common client, 'http://bkxk.xmu.edu.cn/xsxk/kxk.html?pagination=5000', 3
34
+ end
35
+
36
+
62
37
  def select_course_common(client, url, xxlx)
63
38
  prepare_select_course(client)
64
39
  courses = parse_courses((client.get url).body)
65
40
  show_courses_in_table courses
66
41
  try_to_select_courses(client, courses, xxlx, get_main_select_round(client))
67
- $stdout.puts '已经开始选课,输入字符q退出。'
68
42
  loop do
69
- exit if 'q'==$stdin.gets.chomp
43
+ global_gets
70
44
  end
71
45
  end
72
46
 
73
47
  def set_up_account
74
- $stdout.puts '请输入学号:'
75
- stu_id= $stdin.gets.chomp
76
- $stdout.puts '请输入密码:'
77
- passwd= $stdin.gets.chomp
48
+ global_puts '请输入学号:'
49
+ stu_id= global_gets
50
+ global_puts '请输入密码:'
51
+ passwd= global_gets
78
52
  File.open TOKEN_FILE_NAME, 'w' do |file|
79
53
  file.write (SimpleDes.encrypt(stu_id)+TOKEN_SEPARATOR+SimpleDes.encrypt(passwd))
80
54
  end
81
- $stdout.puts '账号信息已经加密并保存在当前目录,请妥善保管。'
55
+ global_puts '账号信息已经加密并保存在当前目录,请妥善保管。'
82
56
  exit
83
57
  end
84
58
 
@@ -86,8 +60,8 @@ def retrieve_check_code(client)
86
60
  File.open 'check_code.jpeg', 'wb' do |file|
87
61
  file.write(client.get('http://bkxk.xmu.edu.cn/xsxk/getCheckCode').body)
88
62
  end
89
- $stdout.puts '验证码图片已在当前目录生成,请查看后输入:'
90
- $stdin.gets.chomp
63
+ global_puts '验证码图片已在当前目录生成,请查看后输入:'
64
+ global_gets
91
65
  end
92
66
 
93
67
  def authorize_session(client, username, password)
@@ -123,7 +97,7 @@ def show_courses_in_table(courses)
123
97
  table.add_row course_entity.to_a
124
98
  end
125
99
  end
126
- $stdout.puts table
100
+ global_puts table
127
101
  end
128
102
 
129
103
  def parse_courses(raw_html)
@@ -153,13 +127,15 @@ def create_attempt_thread(max_interval, client, course, xxlx, lcid, &block)
153
127
  sleep interval
154
128
  res = request_select_course(client, course, xxlx, lcid)
155
129
  if res['success']
156
- $stdout.puts '', "《#{course.name}》(#{course.class_name}班)已经成功选课!", ''
130
+ global_puts '', "《#{course.name}》(#{course.class_name}班)已经成功选课!", ''
157
131
  break
158
132
  else
159
- $stdout.puts '', "《#{course.name}》(#{course.class_name}班)第#{count}次尝试选课失败,#{interval}秒后将重试。(#{filter_text(res['message'])})", ''
133
+ global_puts '', "《#{course.name}》(#{course.class_name}班)第#{count}次尝试选课失败,#{interval}秒后将重试。(#{filter_text(res['message'])})", ''
160
134
  end
161
135
  end
162
- exit if block.binding.local_variable_get(:opened_thread_count)-1==0
136
+ opened_thread_count=block.binding.local_variable_get(:opened_thread_count)
137
+ exit unless opened_thread_count > 0
138
+ block.binding.local_variable_set :opened_thread_count, opened_thread_count-1
163
139
  end
164
140
  end
165
141
 
@@ -188,10 +164,10 @@ def process_ids(str_ids)
188
164
  end
189
165
 
190
166
  def try_to_select_courses(client, courses, xxlx, lcid)
191
- $stdout.puts '', '输入要选的课程ID(重复无效,支持语法糖:x-y,会尝试选择ID从x到y的所有课程), 以空格分割:'
192
- ids = process_ids($stdin.gets.chomp.split(' '))
193
- $stdout.puts '输入最大重试间隔时间,要求不得低于3秒:'
194
- max_interval = $stdin.gets.chomp.to_i
167
+ global_puts '', '输入要选的课程ID(重复无效,支持语法糖:x-y,会尝试选择ID从x到y的所有课程), 以空格分割:'
168
+ ids = process_ids(global_gets.split(' '))
169
+ global_puts '输入最大重试间隔时间,要求不得低于3秒:'
170
+ max_interval = global_gets.to_i
195
171
  max_interval = 3 if max_interval<3
196
172
  opened_thread_count = ids.size
197
173
  ids.each do |id|
@@ -206,13 +182,13 @@ def get_account_info
206
182
  end
207
183
  [stu_id, passwd]
208
184
  rescue
209
- puts '账号信息受损,请重新设置:'
185
+ global_puts '账号信息受损,请重新设置:'
210
186
  set_up_account
211
187
  end
212
188
 
213
189
  def ensure_local_account
214
190
  unless File.exists? TOKEN_FILE_NAME
215
- puts '请先设置账号信息(账号信息会先加密然后保存在本地,用于授权选课系统)'
191
+ global_puts '请先设置账号信息(账号信息会先加密然后保存在本地,用于授权选课系统)'
216
192
  set_up_account && exit
217
193
  end
218
194
  end
@@ -231,6 +207,7 @@ def transform_bracket(raw)
231
207
  end
232
208
  raw
233
209
  end
210
+
234
211
  def filter_text(raw)
235
212
  if Enumerable===raw
236
213
  return [].tap do |arr|
@@ -244,4 +221,79 @@ def filter_text(raw)
244
221
  raw.gsub! tmp, ''
245
222
  end
246
223
  raw
247
- end
224
+ end
225
+
226
+ def global_puts(*args)
227
+ $stdout.puts *args
228
+ end
229
+
230
+ def global_gets(*args)
231
+ quit_flags=%w{q quit e exit}
232
+ tmp = $stdin.gets.chomp
233
+ exit if quit_flags.include? tmp
234
+ tmp
235
+ end
236
+
237
+
238
+ TOKEN_FILE_NAME = 'encrypted_token'
239
+ TOKEN_SEPARATOR = ':'
240
+ CLIENT = HTTPClient.new
241
+
242
+ begin
243
+ opts = Slop.parse do |o|
244
+ o.banner="为缓解选课服务器压力,请酌情选择较大的“最大重试间隔”,此外,在任何需要输入的地方输入“q”或“quit”可以结束本程序。\n
245
+ 用法:命令行输入arb-xmu-course[空格][选项],选项在以下列出。 -Powered By AryBin"
246
+
247
+ o.bool *%w{-h --help}, '显示帮助'
248
+ o.bool *%w{-c --clear}, '清除本地账户信息'
249
+ o.bool *%w{-r --reset}, '重置账户信息'
250
+
251
+ #院选课程 xxlx=4
252
+ o.bool *%w{-s --school}, '院选选课模式'
253
+ #院必修课程 xxlx=2
254
+ o.bool *%w{-S --School}, '院必修选课模式'
255
+ #校选课程 xxlx=3
256
+ o.bool *%w{-g --general}, '校选选课模式'
257
+ #校必修课程 xxlx=1
258
+ o.bool *%w{-G --General}, '校必修选课模式'
259
+ #公共课程 xxlx=5
260
+ o.bool *%w{-p --public}, '公共课选课模式'
261
+ #跨选课程
262
+ o.bool *%w{-a --across}, '跨选课选课模式'
263
+
264
+ o.bool *%w{-v --version}, '显示当前版本'
265
+ end
266
+ rescue Slop::MissingArgument
267
+ global_puts '参数不足,请确认。'
268
+ rescue Slop::UnknownOption
269
+ global_puts '未知选项,请确认。'
270
+ end
271
+
272
+ #error
273
+ exit unless opts
274
+
275
+ case true
276
+ when opts.version?
277
+ global_puts Arb::Xmu::Course::VERSION
278
+ when opts.help?
279
+ global_puts(opts)
280
+ when opts.clear?
281
+ File.delete(TOKEN_FILE_NAME)
282
+ exit
283
+ when opts.reset?
284
+ set_up_account
285
+ when opts.school?
286
+ select_school_elective(CLIENT)
287
+ when opts.general?
288
+ select_general_elective(CLIENT)
289
+ when opts.public?
290
+ select_public(CLIENT)
291
+ when opts.General?
292
+ select_general_required(CLIENT)
293
+ when opts.School?
294
+ select_school_required(CLIENT)
295
+ when opts.across?
296
+ select_across(CLIENT)
297
+ else
298
+ puts opts
299
+ end
@@ -1,7 +1,7 @@
1
1
  module Arb
2
2
  module Xmu
3
3
  module Course
4
- VERSION = '2.1.1'
4
+ VERSION = '2.3.0'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arb-xmu-course
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - arybin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-23 00:00:00.000000000 Z
11
+ date: 2016-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler