-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.rb
More file actions
68 lines (63 loc) · 1.91 KB
/
Copy pathindex.rb
File metadata and controls
68 lines (63 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'yaml'
require './lib/request.rb'
require './lib/score.rb'
require './lib/email.rb'
SLEEP_TIME = 7 * 60
begin
# Load accounts and password.
profiles = YAML.load_file('public/account.yml')
rescue
puts '未找到账号密码,请新建account.yml并存入账号密码'
exit
end
mailer = Email.new
loop do
profiles.each do |pro|
account = pro['account'].to_s
password = pro['password'].to_s
subs_email = pro['subs_email'].to_s
subs_name = pro['subs_name'].to_s
begin
req = Request.new(account, password)
req.download_score
score = Score.new
rescue => e
# Just print the error message for running constanly.
p e
next
end
current_score = score.score_list
gpas = score.gpas
unless File.exist?("result/#{account}.yml")
# Create a file to record scores if it doesn't exist.
puts "第一次查询 #{account} 成绩,初始化中..."
File.open("result/#{account}.yml", 'wb') do |f|
YAML.dump(current_score, f)
end
end
# Load the scores queried last time.
last_score = YAML.load_file("result/#{account}.yml")
# Update it if (current_score & last_score) != current_score
# Judge by method update? in order to avoid scores being withdrawed.
diff = score.diff(last_score)
if diff.any?
puts "\033[032;1m有新的成绩!!!\033[0m"
unless subs_email.nil?
begin
mailer.send_score(subs_email, current_score.last, gpas.last, subs_name, diff)
rescue
puts "\033[31;1m邮件发送失败,请检查邮箱是否正确!\033[0m"
next
end
end
File.open("result/#{account}.yml", 'wb') do |f|
YAML.dump(current_score, f)
end
else
puts '未查询到新成绩'
end
end
puts "开始睡觉,睡上#{SLEEP_TIME / 60}分钟"
sleep SLEEP_TIME
puts "\n睡醒了接着干 #{Time.new.strftime('%Y-%m-%d %H:%M')}"
end