-
Notifications
You must be signed in to change notification settings - Fork 0
/
kut_portal.rb
151 lines (123 loc) · 5.21 KB
/
kut_portal.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# coding: utf-8
require 'mechanize'
require 'openssl'
require './util'
class KUTPortal
attr_reader :agent, :ta_subjects
INDEX_URL = 'https://portal.kochi-tech.ac.jp/'
TA_SUBJECTS_PATH = '/Portal/StudentApp/TA/WorkSumList.aspx'
TA_SUBJECTS_TABLE = '#ctl00_phContents_TaWorkSumList1_gvWorkSum'
TA_WORKS_TABLE = '#ctl00_phContents_TaWorkList1_gvWork'
TA_WORK_RECORD_ADD_BTN = '#ctl00$phContents$TaWorkList1$btnNew'
TA_WORK_RECORD_REMOVE_BTN = '#ctl00_phContents_TaWorkList1_gvWork_ctl02_btnDelete'
TA_WORK_SUMMARIES = {
support: '①授業補助',
prepare: '②授業準備',
material: '③資料作成',
other: '④その他',
support_prepare: '①②授業補助と授業準備',
support_material: '①③授業補助と資料作成'
}
def initialize
@agent = Mechanize.new do |a|
a.user_agent_alias = 'Windows Mozilla'
# a.follow_meta_refresh = true
# a.keep_alive = false
end
# ポータルの証明書が原因でアクセスに失敗するため、証明書を確認しないようにする
@agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
def restore_session
return unless File.exist?('cookie.yml')
@agent.cookie_jar.load('cookie.yml', session: true)
end
def start
@agent.get INDEX_URL
end
def login(username, password)
form = @agent.page.forms[0]
form['j_username'] = username
form['j_password'] = password
form.click_button # submit
# ログイン失敗
return false if @agent.page.at('.form-element.form-error')
# Mechanize は JavaScript をサポートしないため、ポータルサイトが警告を発するが
# [Continue] ボタンをクリックして無視する
@agent.page.forms[0].click_button
@agent.cookie_jar.save('cookie.yml', session: true)
true
end
def logging_in?
@agent.get INDEX_URL
[email protected]('img[alt=ログアウト]')
end
def need_login?
!logging_in?
end
def ta_subjects(year = nil, month = nil)
@agent.get(TA_SUBJECTS_PATH)
form = @agent.page.forms[0]
if year
form['ctl00$phContents$TaWorkSumList1$ddlYear'] = year
form['__EVENTTARGET'] = 'ctl00$phContents$TaWorkSumList1$ddlYear'
form['__EVENTARGUMENT'] = ''
form.submit()
end
if month
form['ctl00$phContents$TaWorkSumList1$ctlMonths$ddlMonth'] = month
form['__EVENTTARGET'] = 'ctl00$phContents$TaWorkSumList1$ctlMonths$ddlMonth'
form['__EVENTARGUMENT'] = ''
form.submit()
end
tbl_rows = @agent.page.search("#{TA_SUBJECTS_TABLE} tr")
keys = %i(num term subject_id subject_name teacher pay_unit hours total_hours plan_hours overtime_hours)
Util.table_rows_to_records(tbl_rows, *keys)
end
def ta_works(subject_name, year = nil, month = nil)
subject = ta_subjects(year, month).find { |s| s[:subject_name].include?(subject_name) }
tr_query = "#{TA_SUBJECTS_TABLE} tr:nth-of-type(#{subject[:num].to_i + 1})"
tr = @agent.page.at(tr_query)
form = @agent.page.forms[0]
form.click_button(form.button_with(value: /登録/)) # 科目勤務記録一覧に遷移
tbl_rows = @agent.page.search("#{TA_WORKS_TABLE} tr")
keys = %i(num date summary started finished rest_hours total_hours)
Util.table_rows_to_records(tbl_rows, *keys)
end
def add_ta_work_record(subject_name, summary, date, started, finished, rest_hours)
ta_works(subject_name)
works_form = @agent.page.forms[0]
add_btn = works_form.button_with(value: /新規追加/)
works_form.click_button(add_btn) # 科目勤務記録登録に遷移
form = @agent.page.forms[0]
# 日付
form['ctl00$phContents$TaWorkEdit1$ctlWorkDate$txtDate'] = date.strftime('%Y/%m/%d')
# 開始時間
form['ctl00$phContents$TaWorkEdit1$ctlStartTime$txtHour'] = started.strftime('%H')
form['ctl00$phContents$TaWorkEdit1$ctlStartTime$txtMinute'] = started.strftime('%M')
# 終了時間
form['ctl00$phContents$TaWorkEdit1$ctlEndTime$txtHour'] = finished.strftime('%H')
form['ctl00$phContents$TaWorkEdit1$ctlEndTime$txtMinute'] = finished.strftime('%M')
# 休憩時間
form['ctl00$phContents$TaWorkEdit1$ctlRestTime$txtHour'] = rest_hours.strftime('%H')
form['ctl00$phContents$TaWorkEdit1$ctlRestTime$txtMinute'] = rest_hours.strftime('%M')
# 名目
form['ctl00$phContents$TaWorkEdit1$ctlWorkDetail$ddlWorkDetail'] = TA_WORK_SUMMARIES[summary]
form.click_button(form.button_with(value: /登録/))
error_text_ids =
%w(ctl00_phContents_TaWorkEdit1_ctlWorkDate_lblError
ctl00_phContents_TaWorkEdit1_ctlWorkDetail_lblError
ctl00_phContents_TaWorkEdit1_ctlStartTime_lblError
ctl00_phContents_TaWorkEdit1_ctlEndTime_lblError
ctl00_phContents_TaWorkEdit1_lblErr)
!error_text_ids.any? { |id| [email protected]("##{id}") }
end
def remove_ta_work_record(subject_name, date)
works = ta_works(subject_name)
works_form = @agent.page.forms[0]
rm_work = works.find { |w| w[:date].include?(date) }
tr_query = "#{TA_WORKS_TABLE} tr:nth-of-type(#{rm_work[:num].to_i + 1})"
tr = @agent.page.at(tr_query)
remove_btn = works_form.button_with(id: /#{tr.at('input[value=削除]').attribute('id')}/)
works_form.click_button(remove_btn) # 削除ボタンをクリック
end
end