-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_calendar.rb
100 lines (89 loc) · 3.21 KB
/
get_calendar.rb
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'yaml'
require './lib/request.rb'
require './lib/schedule.rb'
require './lib/email.rb'
require './lib/icalendar.rb'
START_TIME_OF_CLASS = [8 * 3600,
8 * 3600 + 50 * 60,
9 * 3600 + 50 * 60,
10 * 3600 + 40 * 60,
11 * 3600 + 30 * 60,
14 * 3600,
14 * 3600 + 50 * 60,
15 * 3600 + 50 * 60,
16 * 3600 + 40 * 60,
18 * 3600 + 30 * 60,
19 * 3600 + 20 * 60,
20 * 3600 + 10 * 60]
END_TIME_OF_CLASS = [8 * 3600 + 45 * 60,
9 * 3600 + 35 * 60,
10 * 3600 + 35 * 60,
11 * 3600 + 25 * 60,
12 * 3600 + 15 * 60,
14 * 3600 + 45 * 60,
15 * 3600 + 35 * 60,
16 * 3600 + 35 * 60,
17 * 3600 + 25 * 60,
19 * 3600 + 15 * 60,
20 * 3600 + 05 * 60,
20 * 3600 + 55 * 60]
begin
# Load accounts and password.
profiles = YAML.load_file('public/account.yml')
rescue
puts '未找到账号密码,请新建account.yml并存入账号密码'
exit
end
default_start_day_string = '20170220'
begin
puts "请输入开学上课日期,格式:#{default_start_day_string},直接回车以使用默认值:"
start_day_string = gets.chomp.strip
start_day_string = default_start_day_string if start_day_string.empty?
start_day = Time.parse(start_day_string)
rescue ArgumentError
puts "无法解析出正确的日期"
retry
end
start_weekday = start_day.wday
mailer = Email.new
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
req = Request.new(account, password)
req.download_schedule
schedule = Schedule.new
courses = schedule.courses
icalendar = ICalendar.new
courses.each do |f|
# I found that some courses have two apart phases in a semester
f[:week].each do |w|
class_start_week = w.split('-').first.to_i
class_end_week = w.split('-').last.to_i
repeat = class_end_week - class_start_week + 1
desc = ["任课教师:#{f[:teacher].join('/')}",
"课程类型:#{f[:prop]}",
"学分:#{f[:credit]}",
"考试类型:#{f[:exam]}"].join('\n')
# The value is the time at 0 o'clock on that day
that_day = start_day + ((class_start_week - 1) * 7 + f[:weekday] - start_weekday) * 24 * 3600
s_time = that_day + START_TIME_OF_CLASS[f[:order] - 1]
e_time = that_day + END_TIME_OF_CLASS[f[:order] + f[:count] - 2]
event = {
summary: f[:name],
location: f[:place] + f[:classroom],
rrule: "FREQ=WEEKLY;COUNT=#{repeat}",
description: desc,
start_time: s_time.strftime("%Y%m%dT%H%M%S"),
end_time: e_time.strftime("%Y%m%dT%H%M%S")
}
icalendar.add_event event
end
end
ics_path = "result/schedule#{account}.ics"
File.open(ics_path, 'w') do |f|
f.syswrite(icalendar.publish)
end
mailer.send_calendar(subs_email, ics_path, subs_name)
end