kuport 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +216 -0
- data/Rakefile +2 -0
- data/bin/kuport +133 -0
- data/kuport.gemspec +29 -0
- data/lib/kuport/helper.rb +58 -0
- data/lib/kuport/message.rb +70 -0
- data/lib/kuport/timetable.rb +88 -0
- data/lib/kuport/version.rb +3 -0
- data/lib/kuport/view.rb +20 -0
- data/lib/kuport.rb +152 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b534c2fd903d20621f711e90bc526ecd2239d749
|
4
|
+
data.tar.gz: c8907e3fe41caefd6608c5c43e7702041944770f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 183b78a27136094580d1e9a00460ea5d2c9b95a9a5fded5284485428ea3e91697fdbf5003810968d57550802192dbcb196c408ba6b99f19c8bf0d583f6079816
|
7
|
+
data.tar.gz: 0360777db15d8ff7610cf11b98a40e3cd92438ed30d8ef473fa333d956bff9f53dba2c50dbd72d705b91d5a4014c7b7080f425766d49e6f0e432745d53593fab
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 u+
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
# Kuport
|
2
|
+
|
3
|
+
キューポートからデータを取ってきてJSONにするライブラリとコマンド
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```shell
|
8
|
+
$ gem install kuport
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
###Command
|
14
|
+
|
15
|
+
端末でjsonを読むには`jid`がおすすめ。
|
16
|
+
|
17
|
+
```
|
18
|
+
$ go get github.com/simeji/jid/cmd/jid
|
19
|
+
$ cat sample.json | jid
|
20
|
+
```
|
21
|
+
|
22
|
+
決め打ちで処理するなら`jq`などを使用。
|
23
|
+
|
24
|
+
一度ログインすればキャッシュが効くので、暫くは`--id`とパスワード入力は不要。
|
25
|
+
|
26
|
+
|
27
|
+
```shell
|
28
|
+
# 個人宛メッセージ取得
|
29
|
+
$ kuport --id jx91234 -m
|
30
|
+
|
31
|
+
|
32
|
+
# 個人宛メッセージ(既読)取得
|
33
|
+
$ kuport --id jx91234 -m read
|
34
|
+
|
35
|
+
|
36
|
+
# ログインのみ(一度ログインするとCookieがキャッシュされる)
|
37
|
+
$ kuport --id jx91234
|
38
|
+
|
39
|
+
|
40
|
+
# キューポートからファイルをダウンロード
|
41
|
+
$ kuport --download URL --output-file FILE
|
42
|
+
|
43
|
+
|
44
|
+
# メッセージの添付ファイルをまとめてダウンロード(jqでjsonパース)
|
45
|
+
$ json="$(kuport --id jx91234 -m | jq '.[0].links')"
|
46
|
+
$ kuport --download "$json"
|
47
|
+
|
48
|
+
|
49
|
+
# 動的にダウンロードするファイルを選択
|
50
|
+
$ kuport --download "$(kuport --id jx91234 -m | jid)"
|
51
|
+
|
52
|
+
|
53
|
+
# 時間割取得
|
54
|
+
$ kuport -t
|
55
|
+
```
|
56
|
+
|
57
|
+
###Library
|
58
|
+
```ruby
|
59
|
+
require 'kuport'
|
60
|
+
kp = Kuport.new
|
61
|
+
kp.login('jx91234')
|
62
|
+
|
63
|
+
messages = kp.messages.to_json
|
64
|
+
timetable = kp.messages.to_json
|
65
|
+
|
66
|
+
kp.download(url, name)
|
67
|
+
kp.download([{name: 'File.pdf', path: 'https://example.com/file.pdf'}, ])
|
68
|
+
|
69
|
+
kp.cookies_clear
|
70
|
+
```
|
71
|
+
|
72
|
+
##Formats
|
73
|
+
|
74
|
+
###message
|
75
|
+
|
76
|
+
```json
|
77
|
+
[
|
78
|
+
{
|
79
|
+
"title": "おしらせ その1",
|
80
|
+
"body": "内容",
|
81
|
+
"links": [
|
82
|
+
{
|
83
|
+
"name": "詳細.pdf",
|
84
|
+
"path": "https://example.com/file.pdf"
|
85
|
+
},
|
86
|
+
{
|
87
|
+
"name": "画像.png",
|
88
|
+
"path": "https://example.com/img/image1.pdf"
|
89
|
+
}
|
90
|
+
]
|
91
|
+
},
|
92
|
+
{
|
93
|
+
"title": "おしらせ その2",
|
94
|
+
"body": "内容",
|
95
|
+
"links": []
|
96
|
+
}
|
97
|
+
]
|
98
|
+
|
99
|
+
```
|
100
|
+
|
101
|
+
|
102
|
+
###timetable
|
103
|
+
|
104
|
+
| 要素 | 中身 |
|
105
|
+
|---------|--------------------------|
|
106
|
+
| year | 時間割の年 |
|
107
|
+
| dates | その週の日付けや祝日情報 |
|
108
|
+
| table | 月曜から土曜の時間割 |
|
109
|
+
| special | 集中講義など |
|
110
|
+
|
111
|
+
|
112
|
+
```json
|
113
|
+
{
|
114
|
+
"year": "2022年",
|
115
|
+
"dates": [
|
116
|
+
{
|
117
|
+
"date": "12月19日 月",
|
118
|
+
"special": null
|
119
|
+
},
|
120
|
+
{
|
121
|
+
"date": "12月20日 火",
|
122
|
+
"special": null
|
123
|
+
},
|
124
|
+
{
|
125
|
+
"date": "12月21日 水",
|
126
|
+
"special": null
|
127
|
+
},
|
128
|
+
{
|
129
|
+
"date": "12月22日 木",
|
130
|
+
"special": null
|
131
|
+
},
|
132
|
+
{
|
133
|
+
"date": "12月23日 金",
|
134
|
+
"special": "天皇誕生日"
|
135
|
+
},
|
136
|
+
{
|
137
|
+
"date": "12月24日 土",
|
138
|
+
"special": null
|
139
|
+
}
|
140
|
+
],
|
141
|
+
"table": {
|
142
|
+
"mon": [
|
143
|
+
{
|
144
|
+
"name": "Math",
|
145
|
+
"room": "11-2",
|
146
|
+
"period": "QC",
|
147
|
+
"status": []
|
148
|
+
},
|
149
|
+
{
|
150
|
+
"name": null,
|
151
|
+
"room": null,
|
152
|
+
"period": null,
|
153
|
+
"status": []
|
154
|
+
},
|
155
|
+
{
|
156
|
+
"name": "English",
|
157
|
+
"room": "13-8",
|
158
|
+
"period": "Q2",
|
159
|
+
"status": []
|
160
|
+
},
|
161
|
+
{
|
162
|
+
"name": null,
|
163
|
+
"room": null,
|
164
|
+
"period": null,
|
165
|
+
"status": []
|
166
|
+
},
|
167
|
+
{
|
168
|
+
"name": "IT",
|
169
|
+
"room": "QA",
|
170
|
+
"period": null,
|
171
|
+
"status": []
|
172
|
+
},
|
173
|
+
{
|
174
|
+
"name": null,
|
175
|
+
"room": null,
|
176
|
+
"period": null,
|
177
|
+
"status": []
|
178
|
+
},
|
179
|
+
{
|
180
|
+
"name": null,
|
181
|
+
"room": null,
|
182
|
+
"period": null,
|
183
|
+
"status": []
|
184
|
+
}
|
185
|
+
],
|
186
|
+
"tue"
|
187
|
+
.
|
188
|
+
.
|
189
|
+
.
|
190
|
+
"sat"
|
191
|
+
},
|
192
|
+
"special": ""
|
193
|
+
}
|
194
|
+
|
195
|
+
```
|
196
|
+
|
197
|
+
###download
|
198
|
+
|
199
|
+
| 形式 | 説明 | 例 |
|
200
|
+
|------------|---------------|------------------------------------------------------------------|
|
201
|
+
| URL | ファイルのURL | "http://example.com/file.pdf" |
|
202
|
+
| JSON | 単一の要素 | {"name": "file.pdf", "path": "http://example.com/abc.pdf"} |
|
203
|
+
| JSON(配列) | 複数の要素 | [{"name": "img.png", "path": "http://example.com/efg.png"}, ...] |
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
## Contributing
|
208
|
+
|
209
|
+
バグがあったらお気軽にどうぞ。
|
210
|
+
コントリビューター募集中。
|
211
|
+
|
212
|
+
|
213
|
+
## License
|
214
|
+
|
215
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
216
|
+
|
data/Rakefile
ADDED
data/bin/kuport
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'optparse'
|
3
|
+
require 'kuport'
|
4
|
+
|
5
|
+
# ruby2.3 <<~DOC
|
6
|
+
example_usage = <<USAGE
|
7
|
+
example usage
|
8
|
+
|
9
|
+
# Get messages
|
10
|
+
$ kuport --id jx91234 -m
|
11
|
+
|
12
|
+
|
13
|
+
# Get already read messages
|
14
|
+
$ kuport --id jx91234 -m read
|
15
|
+
|
16
|
+
|
17
|
+
# Login only(cache cookies)
|
18
|
+
$ kuport --id jx91234
|
19
|
+
|
20
|
+
|
21
|
+
# Download file manually
|
22
|
+
$ kuport --download URL --output-file FILE
|
23
|
+
|
24
|
+
|
25
|
+
# Download message attachment
|
26
|
+
$ json="$(kuport --id jx91234 -m | jq '.[0].links')"
|
27
|
+
$ kuport --download "$json"
|
28
|
+
|
29
|
+
|
30
|
+
# Interactively download message attachment
|
31
|
+
$ go get github.com/simeji/jid/cmd/jid
|
32
|
+
$ kuport --download "$(kuport --id jx91234 -m | jid)"
|
33
|
+
USAGE
|
34
|
+
|
35
|
+
def debug?
|
36
|
+
nil != ENV['DEBUG_KUPORT']
|
37
|
+
end
|
38
|
+
|
39
|
+
require 'pry' if debug?
|
40
|
+
|
41
|
+
parser = OptionParser.new do |o|
|
42
|
+
def o.options
|
43
|
+
@options ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def o.on_setopt(symbol, short, long, *desc)
|
47
|
+
on(short, long, *desc){|arg| options[symbol] = block_given? ? yield(arg) : arg}
|
48
|
+
end
|
49
|
+
|
50
|
+
# 候補からデフォルト値をセットする
|
51
|
+
def o.on_select(symbol, short, long, cands, *desc)
|
52
|
+
on_setopt(symbol, short, long, cands, cands.join(' | '), *desc){|arg| arg ? arg : cands[0]}
|
53
|
+
end
|
54
|
+
|
55
|
+
o.program_name= 'kuport'
|
56
|
+
o.version = Kuport::VERSION
|
57
|
+
o.banner = 'KUPORT - Get data as a JSON from Kuport'
|
58
|
+
o.on_head('Usage: kuport [options]')
|
59
|
+
# sumarry_width=32, summary_indent=' '*4,
|
60
|
+
|
61
|
+
|
62
|
+
o.on_setopt(:id, '-I', '--id ID', 'Student number')
|
63
|
+
o.on_setopt(:download, '-D', '--download URL|JSON', 'Download file. URL needs --output-file')
|
64
|
+
o.on_setopt(:output_file, '-O', '--output-file FILE')
|
65
|
+
o.on_setopt(:clear_cookies, '-C', '--clear-cookies')
|
66
|
+
o.separator('')
|
67
|
+
|
68
|
+
o.on_select(:messages, '-m', '--messages [TYPE]', %i[default read backno],
|
69
|
+
"read\tAlready read messages",
|
70
|
+
"backno\tBack number messages(Unimplemented)")
|
71
|
+
o.on_select(:timetable, '-t', '--timetable [TYPE]', %i[default compact])
|
72
|
+
o.separator('')
|
73
|
+
|
74
|
+
o.separator('')
|
75
|
+
o.on_tail(example_usage)
|
76
|
+
end
|
77
|
+
|
78
|
+
ARGV << '--help' if ARGV.empty?
|
79
|
+
parser.permute!(ARGV)
|
80
|
+
opts= parser.options
|
81
|
+
|
82
|
+
if debug?
|
83
|
+
warn "opts #{opts}"
|
84
|
+
warn "ARGV #{ARGV}"
|
85
|
+
end
|
86
|
+
|
87
|
+
# kp = KuportView.new
|
88
|
+
kp = Kuport.new
|
89
|
+
|
90
|
+
if opts[:clear_cookies]
|
91
|
+
kp.cookies_clear
|
92
|
+
exit unless opts[:id]
|
93
|
+
end
|
94
|
+
|
95
|
+
if opts[:id]
|
96
|
+
kp.login(opts[:id])
|
97
|
+
end
|
98
|
+
|
99
|
+
if opts[:download]
|
100
|
+
Kuport.quit('Need --output-file', 20) if Kuport.url?(opts[:download]) && opts[:output_file].nil?
|
101
|
+
kp.download(opts[:download], opts[:output_file])
|
102
|
+
end
|
103
|
+
|
104
|
+
# get data
|
105
|
+
Kuport.quit('Please login', 32) unless kp.loggedin?
|
106
|
+
|
107
|
+
|
108
|
+
# messages
|
109
|
+
if opts[:messages]
|
110
|
+
mes = case opts[:messages]
|
111
|
+
when :default
|
112
|
+
kp.messages
|
113
|
+
when :raed
|
114
|
+
kp.messages_read
|
115
|
+
when :backno
|
116
|
+
kp.messages_backno
|
117
|
+
end
|
118
|
+
|
119
|
+
puts mes.to_json
|
120
|
+
end
|
121
|
+
|
122
|
+
# timetable
|
123
|
+
if opts[:timetable]
|
124
|
+
table = kp.timetable
|
125
|
+
table.compact if opts[:timetable] == :compact
|
126
|
+
puts table.to_json
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
if debug?
|
131
|
+
require 'pry'
|
132
|
+
binding.pry
|
133
|
+
end
|
data/kuport.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'kuport/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'kuport'
|
8
|
+
spec.version = Kuport::VERSION
|
9
|
+
spec.authors = ['u+']
|
10
|
+
spec.email = ['uplus.e10@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Kuport scraping library and command}
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = 'https://github.com/u10e10/kuport'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'bin'
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}){|f| File.basename(f)}
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'nokogiri'
|
25
|
+
spec.add_runtime_dependency 'mechanize'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
class Kuport
|
2
|
+
module Helper
|
3
|
+
def basename_noext(path)
|
4
|
+
File.basename(path, File.extname(path))
|
5
|
+
end
|
6
|
+
|
7
|
+
def br_to_return(element)
|
8
|
+
element.search('br').each{|br| br.replace("\n")}
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_abs_url(base, part)
|
12
|
+
uri = URI.parse(part)
|
13
|
+
uri = URI.join(base, uri) if URI::Generic === uri
|
14
|
+
uri.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def color_str(color_num, str)
|
18
|
+
"\e[38;5;#{color_num}m#{str}\e[00m"
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_page_doc(page)
|
22
|
+
Nokogiri::HTML.parse(page.content.toutf8)
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_half_str(str)
|
26
|
+
NKF.nkf('-m0Z1 -w -W', str)
|
27
|
+
end
|
28
|
+
|
29
|
+
def url?(str)
|
30
|
+
str =~ /\A#{URI::regexp}\z/
|
31
|
+
end
|
32
|
+
|
33
|
+
def input_num(str)
|
34
|
+
print str
|
35
|
+
gets.to_i rescue nil
|
36
|
+
rescue Interrupt
|
37
|
+
exit 1
|
38
|
+
end
|
39
|
+
|
40
|
+
def input_passwd
|
41
|
+
$stderr.print 'Password> '
|
42
|
+
pass = STDIN.noecho(&:gets).chomp rescue nil # &.chomp
|
43
|
+
puts
|
44
|
+
return pass
|
45
|
+
rescue Interrupt
|
46
|
+
exit 2
|
47
|
+
end
|
48
|
+
|
49
|
+
def quit(mes, ret=false)
|
50
|
+
warn mes
|
51
|
+
exit ret
|
52
|
+
end
|
53
|
+
|
54
|
+
def blank?(var)
|
55
|
+
var.nil? || var.empty?
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class Kuport::Message
|
2
|
+
attr_reader :agent, :base_url, :title, :body, :links
|
3
|
+
|
4
|
+
# return [Message,...]
|
5
|
+
def self.parse_page(agent, page)
|
6
|
+
doc = Kuport.get_page_doc(page)
|
7
|
+
doc.css('.message > li').map{|mes| self.new(agent, mes, page.uri)}
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(agent, doc, base_url)
|
11
|
+
@agent = agent
|
12
|
+
@base_url = base_url
|
13
|
+
@title = parse_title(doc)
|
14
|
+
|
15
|
+
doc_mes = get_message_doc(doc)
|
16
|
+
|
17
|
+
if doc_mes
|
18
|
+
@body = get_body_text(doc_mes)
|
19
|
+
else
|
20
|
+
# When message is link
|
21
|
+
doc_mes = get_linked_page_doc(doc)
|
22
|
+
@body = get_body_text(get_message_doc(doc_mes))
|
23
|
+
end
|
24
|
+
|
25
|
+
@links = parse_links(doc_mes)
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_title(doc)
|
29
|
+
doc.children.select(&:text?).join.strip + doc.at_css('a').text.freeze
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_message_doc(doc)
|
33
|
+
doc.at_xpath(".//*[contains(@class, 'message')]")
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_body_text(doc)
|
37
|
+
doc.text.strip.gsub(/\r\n/, "\n").freeze rescue "Error in #{title}"
|
38
|
+
end
|
39
|
+
|
40
|
+
# Call if message is link
|
41
|
+
def get_linked_page_doc(doc)
|
42
|
+
link = parse_link(doc.at_css('a'))[:path]
|
43
|
+
doc_new = Kuport.get_page_doc(agent.get(link))
|
44
|
+
doc_new.at_css('.portlet_module')
|
45
|
+
end
|
46
|
+
|
47
|
+
def parse_links(doc_mes)
|
48
|
+
doc_mes.css('a').map{|elem| parse_link(elem)}.freeze rescue [{name: "Error in #{title}", path: ''}]
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_link(elem)
|
52
|
+
{name: elem.text, path: Kuport.to_abs_url(base_url, elem[:href])}.freeze
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_h
|
56
|
+
@data_hash ||= {title: title, body: body, links: links}.freeze
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_s
|
60
|
+
@data_str ||= ("#{title}\n\n#{body}\n\n" + links.map{|l| l[:name]}.join("\n")).freeze
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_json(*a)
|
64
|
+
@data_json ||= to_h.to_json(*a)
|
65
|
+
end
|
66
|
+
|
67
|
+
def inspect
|
68
|
+
"title: #{title}\nbody: #{body}\nlinks: #{links}".freeze
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
class Kuport::Timetable
|
2
|
+
attr_reader :year, :dates, :table, :special
|
3
|
+
|
4
|
+
def initialize(doc)
|
5
|
+
period_line = doc.at_css('.portlet_module > .ac > table').css('tr')
|
6
|
+
parse_dates(period_line.shift)
|
7
|
+
parse_special(period_line.pop)
|
8
|
+
parse_table(period_line)
|
9
|
+
end
|
10
|
+
|
11
|
+
# 集中講義(1週間共通)
|
12
|
+
def parse_special(special_doc)
|
13
|
+
@special = special_doc.css('td')[1].text
|
14
|
+
@special = '' if @special == '-'
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse_dates(dates_doc)
|
18
|
+
Kuport.br_to_return(dates_doc)
|
19
|
+
tds = dates_doc.css('td')
|
20
|
+
@year = tds.shift.text
|
21
|
+
|
22
|
+
@dates = tds.map do |td|
|
23
|
+
# 月日曜, 祝
|
24
|
+
date,special = td.text.sub("\n", ' ').split("\n")
|
25
|
+
{date: date, special: special}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse_table(period_line)
|
30
|
+
@table = {mon: [], tue: [], wed: [], thurs: [], fri: [], sat: []}
|
31
|
+
|
32
|
+
period_line.each do |tr|
|
33
|
+
tds = tr.css('td')
|
34
|
+
tds.shift # 横枠破棄(1~7時限 集中講義等)
|
35
|
+
|
36
|
+
tds.zip(@table).each do |(td, day)| # 各曜日のn限
|
37
|
+
Kuport.br_to_return(td)
|
38
|
+
name,room,period = parse_class_text(td.text)
|
39
|
+
|
40
|
+
# 休講とか (kyuko)
|
41
|
+
status = td.css('img').map{|img| Kuport.basename_noext(img['src'])}
|
42
|
+
day[1] << {name: name, room: room, period: period, status: status}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def parse_class_text(text)
|
48
|
+
text.strip!
|
49
|
+
text = '' if text == '-'
|
50
|
+
text = Kuport.to_half_str(text)
|
51
|
+
|
52
|
+
name,room,period = text.split("\n")
|
53
|
+
name.sub!(/\[(.+)\]/, '\1') if name
|
54
|
+
period.sub!(/\((\w+)\)/, '\1') if period
|
55
|
+
[name,room,period]
|
56
|
+
end
|
57
|
+
|
58
|
+
def compact
|
59
|
+
# 土曜日から連続して授業の無い日を消す
|
60
|
+
table.reverse_each do |key, val|
|
61
|
+
break unless val.all?{|elem| elem[:name].nil?}
|
62
|
+
table.delete(key)
|
63
|
+
end
|
64
|
+
|
65
|
+
# 週を通してn時限目が無ければ消す
|
66
|
+
6.downto(0).each do |i|
|
67
|
+
break unless table.all?{|key, val| val[i][:name].nil?}
|
68
|
+
table.each{|key, val| val.pop}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_h
|
73
|
+
@data_hash ||= {
|
74
|
+
year: year,
|
75
|
+
dates: dates,
|
76
|
+
table: table,
|
77
|
+
special: special,
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
def to_s
|
82
|
+
@data_str = to_h.to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
def to_json
|
86
|
+
@data_json ||= to_h.to_json
|
87
|
+
end
|
88
|
+
end
|
data/lib/kuport/view.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# TODO
|
2
|
+
class KuportView < Kuport
|
3
|
+
def show_menu
|
4
|
+
@@menu_items.each_with_index do |(key,value), i|
|
5
|
+
print "%1d %s\n" % [i, value]
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def select_menu
|
10
|
+
show_menu
|
11
|
+
num = input_num("[0..#{@@menu_items.size-1}]> ")
|
12
|
+
send(menu[num]) rescue nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def show_messages
|
16
|
+
messages.each_with_index do |mes, i|
|
17
|
+
print "%3d %s\n" % [i, mes]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/kuport.rb
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'mechanize'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'json'
|
6
|
+
require 'io/console'
|
7
|
+
require 'nkf'
|
8
|
+
|
9
|
+
require 'kuport/helper'
|
10
|
+
|
11
|
+
# TODO make test
|
12
|
+
|
13
|
+
class Kuport
|
14
|
+
extend Kuport::Helper
|
15
|
+
|
16
|
+
# Need last slash in url
|
17
|
+
@@base_url = 'https://kuport.sc.kogakuin.ac.jp/ActiveCampus/'.freeze
|
18
|
+
@@base_module = 'module/'.freeze
|
19
|
+
@@modules = { login: 'Login.php', menu: 'Menu.php', messages: 'Message.php',
|
20
|
+
messages_read: 'Message.php?mode=read',
|
21
|
+
messages_backno: 'Message.php?mode=backno',
|
22
|
+
timetable: 'Jikanwari.php',
|
23
|
+
materials: 'Kyozai.php',
|
24
|
+
}
|
25
|
+
@@menu_items = {messages: '個人宛お知らせ',
|
26
|
+
messages_read: '個人宛お知らせ(既読)',
|
27
|
+
timetable: '時間割',
|
28
|
+
materials: '電子教材',
|
29
|
+
}
|
30
|
+
|
31
|
+
@@cache_dir = File.join(Dir.home, '.cache', 'kuport').freeze
|
32
|
+
@@cookies_file = File.join(@@cache_dir, 'cookies.jar')
|
33
|
+
FileUtils.mkdir_p(@@cache_dir)
|
34
|
+
|
35
|
+
attr_reader :agent
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
@agent = Mechanize.new
|
39
|
+
@agent.html_parser = self.class
|
40
|
+
cookies_load
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.module_url(*parts)
|
44
|
+
URI.join(@@base_url, @@base_module, *parts)
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_module(symb)
|
48
|
+
agent.get(self.class.module_url(@@modules[symb]))
|
49
|
+
end
|
50
|
+
|
51
|
+
def menu_links
|
52
|
+
@menu_links ||= get_module(:menu).links.reject{|l| l.text.empty? }
|
53
|
+
end
|
54
|
+
|
55
|
+
def cookies_save
|
56
|
+
File.open(@@cookies_file, 'w'){|f| agent.cookie_jar.save(f, {session: true})}
|
57
|
+
end
|
58
|
+
|
59
|
+
def cookies_load
|
60
|
+
File.open(@@cookies_file, 'r'){|f| agent.cookie_jar.load(f) } rescue nil
|
61
|
+
end
|
62
|
+
|
63
|
+
def cookies_clear
|
64
|
+
agent.cookie_jar.clear!
|
65
|
+
FileUtils.rm(@@cookies_file) if File.exist?(@@cookies_file)
|
66
|
+
end
|
67
|
+
|
68
|
+
def loggedin?
|
69
|
+
nil != get_module(:login).form.has_value?('Logout')
|
70
|
+
end
|
71
|
+
|
72
|
+
def login_cookie
|
73
|
+
return false unless cookies_load
|
74
|
+
return true if loggedin?
|
75
|
+
agent.cookie_jar.clear!
|
76
|
+
false
|
77
|
+
end
|
78
|
+
|
79
|
+
def login_passwd(id, passwd)
|
80
|
+
get_module(:login).form_with(name: 'login_form') do |f|
|
81
|
+
f.login = id
|
82
|
+
f.passwd = passwd
|
83
|
+
end.submit
|
84
|
+
|
85
|
+
return false unless loggedin?
|
86
|
+
cookies_save
|
87
|
+
true
|
88
|
+
end
|
89
|
+
|
90
|
+
def login(id)
|
91
|
+
return if login_cookie
|
92
|
+
Kuport.quit('Please student id', 4) if Kuport.blank?(id)
|
93
|
+
|
94
|
+
3.times do
|
95
|
+
return if login_passwd(id, Kuport.input_passwd)
|
96
|
+
warn 'Login failed'
|
97
|
+
end
|
98
|
+
|
99
|
+
Kuport.quit('Login error', 3)
|
100
|
+
end
|
101
|
+
|
102
|
+
def messages
|
103
|
+
@messages ||= Message.parse_page(agent, get_module(:messages))
|
104
|
+
end
|
105
|
+
|
106
|
+
def messages_read
|
107
|
+
@messages_read ||= Message.parse_page(agent, get_module(:messages_read))
|
108
|
+
end
|
109
|
+
|
110
|
+
def messages_backno
|
111
|
+
# TODO
|
112
|
+
end
|
113
|
+
|
114
|
+
def timetable
|
115
|
+
@timetable ||= Timetable.new(get_module(:timetable))
|
116
|
+
end
|
117
|
+
|
118
|
+
def materials
|
119
|
+
# TODO
|
120
|
+
end
|
121
|
+
|
122
|
+
def download_file(file_path, url)
|
123
|
+
File.write(file_path, agent.get(url).content)
|
124
|
+
end
|
125
|
+
|
126
|
+
# url_or_json is "http://....", {name:, path:}, or [{name:, path:}, ...]
|
127
|
+
# If url_or_json is URL, need file_path
|
128
|
+
def download(url_or_json, file_path=nil)
|
129
|
+
if Kuport.url?(url_or_json)
|
130
|
+
download_file(file_path, url_or_json)
|
131
|
+
return
|
132
|
+
end
|
133
|
+
|
134
|
+
json = JSON.parse(url_or_json, {symbolize_names: true}) rescue Kuport.quit("Download Error: #{url_or_json}", 7)
|
135
|
+
|
136
|
+
if Array === json
|
137
|
+
json.each{|link| download_file(link[:name], link[:path])}
|
138
|
+
else
|
139
|
+
download_file(json[:name], json[:path])
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
# html parser for Mechanize. force encode to UTF-8
|
144
|
+
def self.parse(text, url = nil, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_HTML, &block)
|
145
|
+
Nokogiri::HTML::Document.parse(text.toutf8, url, 'UTF-8', options, &block)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
require 'kuport/message'
|
150
|
+
require 'kuport/timetable'
|
151
|
+
require 'kuport/view'
|
152
|
+
require 'kuport/version'
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kuport
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- u+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mechanize
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Kuport scraping library and command
|
70
|
+
email:
|
71
|
+
- uplus.e10@gmail.com
|
72
|
+
executables:
|
73
|
+
- kuport
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/kuport
|
83
|
+
- kuport.gemspec
|
84
|
+
- lib/kuport.rb
|
85
|
+
- lib/kuport/helper.rb
|
86
|
+
- lib/kuport/message.rb
|
87
|
+
- lib/kuport/timetable.rb
|
88
|
+
- lib/kuport/version.rb
|
89
|
+
- lib/kuport/view.rb
|
90
|
+
homepage: https://github.com/u10e10/kuport
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.5.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Kuport scraping library and command
|
114
|
+
test_files: []
|