mooc-data-parser 0.0.3 → 0.0.4
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 +4 -4
- data/Readme.md +9 -7
- data/lib/mooc_data_parser.rb +4 -6
- data/lib/mooc_data_parser/app.rb +220 -0
- data/lib/mooc_data_parser/auth_coordinator.rb +15 -0
- data/lib/mooc_data_parser/data_downloader.rb +49 -0
- data/lib/mooc_data_parser/options_parser_logic.rb +45 -0
- data/lib/mooc_data_parser/version.rb +1 -1
- metadata +6 -3
- data/lib/mooc_data_parser/App.rb +0 -257
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b10527539a617caf47b8b4e10a441fa82473d6cb
|
4
|
+
data.tar.gz: 4ea29ecdefc4f79259d488e0ad49af5964a8eedf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5b3ab28a7cc01efb5b2e6330c56a856babaf4f904ffa058f6b0cd9f55676919e165b458193c92ddfdeb95cf2c8d60b83629660e2158435bdd5f866e97248b50
|
7
|
+
data.tar.gz: 828f9ab0f05d60202b576f2c559ff01b0e826fc8cc0e8b20e017c30ebe3e418cca19d2450dc4a8c8cc20e8eb2944c144a3f49a38b68acfe2f6674c431b9d75ac
|
data/Readme.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Mooc Data Parser
|
2
2
|
|
3
|
+
[](https://codeclimate.com/github/jamox/mooc-data-parser)
|
4
|
+
|
3
5
|
## Installation
|
4
6
|
|
5
7
|
Or install it yourself as:
|
@@ -10,7 +12,7 @@ Scripts made to process some data for our Massive Open Online Courses.
|
|
10
12
|
## Usage
|
11
13
|
|
12
14
|
```bash
|
13
|
-
Usage: show-mooc-details
|
15
|
+
Usage: show-mooc-details [options]
|
14
16
|
-f, --force Reload data from server
|
15
17
|
-u, --user username Show details for user
|
16
18
|
-m, --missing-points Show missing compulsary points
|
@@ -30,33 +32,33 @@ To get fresh data, use `-f` command line parameter.
|
|
30
32
|
|
31
33
|
Show basic info for applicants
|
32
34
|
```bash
|
33
|
-
|
35
|
+
mooc-data-parser -l
|
34
36
|
```
|
35
37
|
|
36
38
|
Show basic info for applicants and percents for each week
|
37
39
|
```bash
|
38
|
-
|
40
|
+
mooc-data-parser -l -c
|
39
41
|
```
|
40
42
|
|
41
43
|
Show basic info for applicants and missing points
|
42
44
|
```bash
|
43
|
-
|
45
|
+
mooc-data-parser -l -m
|
44
46
|
```
|
45
47
|
|
46
48
|
Show basic info for applicants and percents for each week and missing points
|
47
49
|
```bash
|
48
|
-
|
50
|
+
mooc-data-parser -l -c -m
|
49
51
|
```
|
50
52
|
|
51
53
|
Find info about a user
|
52
54
|
* by email:
|
53
55
|
```bash
|
54
|
-
|
56
|
+
mooc-data-parser -e <email>
|
55
57
|
```
|
56
58
|
|
57
59
|
* by username:
|
58
60
|
```bash
|
59
|
-
|
61
|
+
mooc-data-parser -u <username>
|
60
62
|
```
|
61
63
|
|
62
64
|
Flags `-c` and `-l` can be used with these too.
|
data/lib/mooc_data_parser.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
require "mooc_data_parser/app"
|
2
1
|
require "mooc_data_parser/dummy_cacher"
|
2
|
+
require "mooc_data_parser/app"
|
3
|
+
require "mooc_data_parser/auth_coordinator"
|
4
|
+
require "mooc_data_parser/options_parser_logic"
|
5
|
+
require "mooc_data_parser/data_downloader"
|
3
6
|
require "mooc_data_parser/version"
|
4
7
|
module MoocDataParser
|
5
|
-
require 'optparse'
|
6
|
-
require 'ostruct'
|
7
|
-
require 'httparty'
|
8
|
-
require 'json'
|
9
|
-
require 'io/console'
|
10
8
|
end
|
11
9
|
|
@@ -0,0 +1,220 @@
|
|
1
|
+
module MoocDataParser
|
2
|
+
require 'json'
|
3
|
+
class App
|
4
|
+
|
5
|
+
def run(args)
|
6
|
+
init_variables()
|
7
|
+
parse_options(args)
|
8
|
+
decide_what_to_do(maybe_fetch_json())
|
9
|
+
$cache.write_file_to_cache('data.json', @notes.to_json)
|
10
|
+
end
|
11
|
+
|
12
|
+
def decide_what_to_do(json)
|
13
|
+
if @options.user
|
14
|
+
show_info_about(@options.user, 'username', json)
|
15
|
+
elsif @options.user_email
|
16
|
+
show_info_about(@options.user_email, 'email', json)
|
17
|
+
elsif @options.user_tmc_username
|
18
|
+
show_info_about(@options.user_tmc_username, 'username', json)
|
19
|
+
elsif @options.list
|
20
|
+
list_and_filter_participants(json)
|
21
|
+
else
|
22
|
+
$cache.write_file_to_cache('data.json', @notes.to_json)
|
23
|
+
puts @opt
|
24
|
+
abort
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def init_variables
|
29
|
+
$cache ||= MoocDataParser::DummyCacher.new
|
30
|
+
@notes = begin JSON.parse($cache.read_file_from_cache('data.json')) rescue {} end
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse_options(args)
|
34
|
+
@options, @opt = MoocDataParser::OptionsParserLogic.new(args).parse
|
35
|
+
end
|
36
|
+
|
37
|
+
def maybe_fetch_json()
|
38
|
+
if @options.reload or @notes['user_info'].nil? or @notes['week_data'].nil?
|
39
|
+
download_data()
|
40
|
+
else
|
41
|
+
{participants: @notes['user_info'].clone, week_data: @notes['week_data'].clone}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def download_data
|
46
|
+
MoocDataParser::DataDownloader.new(@notes).download!
|
47
|
+
end
|
48
|
+
|
49
|
+
def show_info_about(user, user_field = 'username', json)
|
50
|
+
participants = json[:participants]
|
51
|
+
week_data = json[:week_data]
|
52
|
+
my_user = participants.find{|a| a[user_field] == user }
|
53
|
+
|
54
|
+
abort "User not found" if my_user.nil?
|
55
|
+
|
56
|
+
show_user_print_basic_info(my_user)
|
57
|
+
show_user_print_completion_percentage(my_user, week_data, participants) if @options.show_completion_percentige
|
58
|
+
show_user_print_missing_points(my_user, week_data) if @options.show_missing_compulsory_points
|
59
|
+
end
|
60
|
+
|
61
|
+
def show_user_print_basic_info(my_user)
|
62
|
+
formatted_print_user_details ["Username", my_user['username']]
|
63
|
+
formatted_print_user_details ["Email", my_user['email']]
|
64
|
+
formatted_print_user_details ["Hakee yliopistoon", my_user['hakee_yliopistoon_2014']]
|
65
|
+
formatted_print_user_details ["Koko nimi", my_user['koko_nimi']]
|
66
|
+
end
|
67
|
+
|
68
|
+
def show_user_print_missing_points(my_user, week_data)
|
69
|
+
formatted_print_user_details ["Compulsory points"]
|
70
|
+
get_points_info_for_user(my_user, week_data).each do |k,v|
|
71
|
+
formatted_print_user_details [k, v.join(", ")]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def show_user_print_completion_percentage(my_user, week_data, participants)
|
76
|
+
formatted_print_user_details ["Points per week"]
|
77
|
+
done_exercise_percents(my_user, participants).each do |k|
|
78
|
+
begin
|
79
|
+
k = k.first
|
80
|
+
formatted_print_user_details [k[0], k[1]]
|
81
|
+
rescue
|
82
|
+
nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def formatted_print_user_details(details)
|
88
|
+
case details.size
|
89
|
+
when 1
|
90
|
+
puts "%18s" % details
|
91
|
+
when 2
|
92
|
+
puts "%18s: %-20s" % details
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
def wanted_fields
|
98
|
+
%w(username email koko_nimi)
|
99
|
+
end
|
100
|
+
|
101
|
+
def list_and_filter_participants(json)
|
102
|
+
participants = json[:participants]
|
103
|
+
week_data = json[:week_data]
|
104
|
+
everyone_in_course = participants.size
|
105
|
+
only_applying!(participants)
|
106
|
+
hakee_yliopistoon = participants.size
|
107
|
+
|
108
|
+
print_headers()
|
109
|
+
process_participants(participants, week_data)
|
110
|
+
print_list_stats(everyone_in_course, hakee_yliopistoon)
|
111
|
+
end
|
112
|
+
|
113
|
+
def print_headers
|
114
|
+
puts "%-20s %-35s %-25s %-120s" % ["Username", "Email", "Real name", "Missing points"]
|
115
|
+
puts '-'*200
|
116
|
+
end
|
117
|
+
|
118
|
+
def print_list_stats(everyone_in_course, hakee_yliopistoon)
|
119
|
+
puts "\n"
|
120
|
+
puts "Stats: "
|
121
|
+
puts "%25s: %4d" % ["Kaikenkaikkiaan kurssilla", everyone_in_course]
|
122
|
+
puts "%25s: %4d" % ["Hakee yliopistoon", hakee_yliopistoon]
|
123
|
+
end
|
124
|
+
|
125
|
+
def process_participants(participants, week_data)
|
126
|
+
participants.each do |participant|
|
127
|
+
nice_string_in_array = wanted_fields.map do |key|
|
128
|
+
participant[key]
|
129
|
+
end
|
130
|
+
|
131
|
+
to_be_printed = "%-20s %-35s %-25s "
|
132
|
+
|
133
|
+
maybe_add_extra_fields(nice_string_in_array, to_be_printed, participants, participant, week_data)
|
134
|
+
|
135
|
+
puts to_be_printed % nice_string_in_array
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def maybe_add_extra_fields(nice_string_in_array, to_be_printed, participants, participant, week_data)
|
140
|
+
if @options.show_completion_percentige
|
141
|
+
nice_string_in_array << format_done_exercises_percents(done_exercise_percents(participant, participants))
|
142
|
+
to_be_printed << "%-180s "
|
143
|
+
end
|
144
|
+
if @options.show_missing_compulsory_points
|
145
|
+
nice_string_in_array << missing_points_to_list_string(get_points_info_for_user(participant, week_data))
|
146
|
+
to_be_printed << "%-120s"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def format_done_exercises_percents(hash)
|
151
|
+
hash.map do |k|
|
152
|
+
begin
|
153
|
+
k = k.first
|
154
|
+
"#{k[0].scan(/\d+/).first}: #{k[1]}"
|
155
|
+
rescue
|
156
|
+
nil
|
157
|
+
end
|
158
|
+
end.compact.join(", ")
|
159
|
+
end
|
160
|
+
|
161
|
+
def done_exercise_percents(participant, participants_data)
|
162
|
+
user_info = participants_data.find{ |p| p['username'] == participant['username'] }
|
163
|
+
map_week_keys(user_info)
|
164
|
+
end
|
165
|
+
|
166
|
+
def week_keys
|
167
|
+
(1..12).map{|i| "viikko#{i}"}
|
168
|
+
end
|
169
|
+
|
170
|
+
def map_week_keys(user_info)
|
171
|
+
exercise_weeks = user_info['groups']
|
172
|
+
week_keys.map do |week|
|
173
|
+
details = exercise_weeks[week]
|
174
|
+
unless details.nil?
|
175
|
+
{week => ("%3.1f%" % [(details['points'].to_f / details['total'].to_f) * 100])}
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def missing_points_to_list_string(missing_by_week)
|
181
|
+
str = ""
|
182
|
+
missing_by_week.keys.each do |week|
|
183
|
+
missing = missing_by_week[week]
|
184
|
+
unless missing.nil? or missing.length == 0
|
185
|
+
str << week
|
186
|
+
str << ": "
|
187
|
+
str << missing.join(",")
|
188
|
+
str << " "
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
str
|
193
|
+
end
|
194
|
+
|
195
|
+
def compulsory_exercises
|
196
|
+
# TODO: täydennä data viikolle 12
|
197
|
+
{'6' => %w(102.1 102.2 102.3 103.1 103.2 103.3), '7' => %w(116.1 116.2 116.3), '8' => %w(124.1 124.2 124.3 124.4),
|
198
|
+
'9' => %w(134.1 134.2 134.3 134.4 134.5), '10' => %w(141.1 141.2 141.3 141.4), '11' => %w(151.1 151.2 151.3 151.4), '12' => %w()}
|
199
|
+
end
|
200
|
+
|
201
|
+
def get_points_info_for_user(participant, week_data)
|
202
|
+
points_by_week = week_data.keys.each_with_object({}) do |week, points_by_week|
|
203
|
+
points_by_week[week] = week_data[week][participant['username']]
|
204
|
+
end
|
205
|
+
|
206
|
+
points_by_week.keys.each_with_object({}) do |week, missing_by_week|
|
207
|
+
weeks_points = points_by_week[week] || [] #palauttaa arrayn
|
208
|
+
weeks_compulsory_points = compulsory_exercises[week] || []
|
209
|
+
missing_by_week[week] = weeks_compulsory_points - weeks_points
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def only_applying!(participants)
|
214
|
+
participants.select! do |participant|
|
215
|
+
participant['hakee_yliopistoon_2014']
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module MoocDataParser
|
2
|
+
require 'io/console'
|
3
|
+
class AuthCoordinator
|
4
|
+
|
5
|
+
def auth
|
6
|
+
print 'username: '
|
7
|
+
username = $stdin.gets.strip
|
8
|
+
print 'password: '
|
9
|
+
password = $stdin.noecho(&:gets).strip
|
10
|
+
puts
|
11
|
+
{username: username, password: password}
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
|
2
|
+
module MoocDataParser
|
3
|
+
require 'httparty'
|
4
|
+
require 'json'
|
5
|
+
class DataDownloader
|
6
|
+
|
7
|
+
def initialize(notes)
|
8
|
+
@notes = notes
|
9
|
+
end
|
10
|
+
|
11
|
+
def download!
|
12
|
+
auth = get_auth()
|
13
|
+
thread = get_process_thread()
|
14
|
+
url = "http://tmc.mooc.fi/mooc/participants.json?api_version=7&utf8=%E2%9C%93&filter_koko_nimi=&column_username=1&column_email=1&column_koko_nimi=1&column_hakee_yliopistoon_2014=1&group_completion_course_id=18"
|
15
|
+
user_info = JSON.parse(HTTParty.get(url, basic_auth: auth).body)['participants']
|
16
|
+
week_data = fetch_week_datas(auth)
|
17
|
+
@notes['user_info'] = user_info.clone
|
18
|
+
@notes['week_data'] = week_data.clone
|
19
|
+
thread.kill
|
20
|
+
puts
|
21
|
+
{participants: user_info, week_data: week_data}
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_auth
|
25
|
+
AuthCoordinator.new.auth
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_process_thread
|
29
|
+
t = -> do
|
30
|
+
loop do
|
31
|
+
print '.'
|
32
|
+
sleep 0.5
|
33
|
+
end
|
34
|
+
end
|
35
|
+
Thread.new(&t)
|
36
|
+
end
|
37
|
+
|
38
|
+
def fetch_week_datas(auth)
|
39
|
+
base_url = "http://tmc.mooc.fi/mooc/courses/18/points/"
|
40
|
+
weeks = %w(1 2 3 4 5 6 7 8 9 10 11 12)
|
41
|
+
rest = ".json?api_version=7"
|
42
|
+
week_data = {}
|
43
|
+
weeks.each do |week|
|
44
|
+
week_data[week] = JSON.parse(HTTParty.get(base_url + week + rest, basic_auth: auth).body)['users_to_points']
|
45
|
+
end
|
46
|
+
week_data
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module MoocDataParser
|
2
|
+
require 'optparse'
|
3
|
+
require 'ostruct'
|
4
|
+
class OptionsParserLogic
|
5
|
+
|
6
|
+
def initialize(args)
|
7
|
+
@args = args
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse
|
11
|
+
options = OpenStruct.new
|
12
|
+
opt = OptionParser.new do |opts|
|
13
|
+
opts.banner = "Usage: show-mooc-details.rb [options]"
|
14
|
+
|
15
|
+
opts.on("-f", "--force", "Reload data from server") do |v|
|
16
|
+
options.reload = true
|
17
|
+
end
|
18
|
+
opts.on("-u", "--user username", "Show details for user") do |v|
|
19
|
+
options.user = v
|
20
|
+
end
|
21
|
+
opts.on("-m", "--missing-points", "Show missing compulsary points") do |v|
|
22
|
+
options.show_missing_compulsory_points = true
|
23
|
+
end
|
24
|
+
opts.on("-c", "--completion-precentige", "Show completition percentige") do |v|
|
25
|
+
options.show_completion_percentige = true
|
26
|
+
end
|
27
|
+
opts.on("-e", "--email emailaddress", "Show details for user") do |v|
|
28
|
+
options.user_email = v
|
29
|
+
end
|
30
|
+
opts.on("-t", "--tmc-account tmc-account", "Show details for user") do |v|
|
31
|
+
options.user_tmc_username = v
|
32
|
+
end
|
33
|
+
opts.on("-l", "--list", "Show the basic list") do |v|
|
34
|
+
options.list = true
|
35
|
+
end
|
36
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
37
|
+
puts opts
|
38
|
+
exit
|
39
|
+
end
|
40
|
+
end
|
41
|
+
opt.parse!(@args)
|
42
|
+
[options, opt]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mooc-data-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jarmo Isotalo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -67,8 +67,11 @@ files:
|
|
67
67
|
- Readme.md
|
68
68
|
- bin/mooc-data-parser
|
69
69
|
- lib/mooc_data_parser.rb
|
70
|
-
- lib/mooc_data_parser/
|
70
|
+
- lib/mooc_data_parser/app.rb
|
71
|
+
- lib/mooc_data_parser/auth_coordinator.rb
|
72
|
+
- lib/mooc_data_parser/data_downloader.rb
|
71
73
|
- lib/mooc_data_parser/dummy_cacher.rb
|
74
|
+
- lib/mooc_data_parser/options_parser_logic.rb
|
72
75
|
- lib/mooc_data_parser/version.rb
|
73
76
|
- mooc-data-parser.gemspec
|
74
77
|
homepage: https://github.com/jamox/mooc-data-parser
|
data/lib/mooc_data_parser/App.rb
DELETED
@@ -1,257 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby -w
|
2
|
-
module MoocDataParser
|
3
|
-
require 'optparse'
|
4
|
-
require 'ostruct'
|
5
|
-
require 'httparty'
|
6
|
-
require 'json'
|
7
|
-
require 'io/console'
|
8
|
-
class App
|
9
|
-
def run(args)
|
10
|
-
$cache ||= MoocDataParser::DummyCacher.new
|
11
|
-
@notes = begin JSON.parse($cache.read_file_from_cache('data.json')) rescue {} end
|
12
|
-
|
13
|
-
@options = OpenStruct.new
|
14
|
-
opt = OptionParser.new do |opts|
|
15
|
-
opts.banner = "Usage: show-mooc-details.rb [options]"
|
16
|
-
|
17
|
-
opts.on("-f", "--force", "Reload data from server") do |v|
|
18
|
-
@options.reload = true
|
19
|
-
end
|
20
|
-
opts.on("-u", "--user username", "Show details for user") do |v|
|
21
|
-
@options.user = v
|
22
|
-
end
|
23
|
-
opts.on("-m", "--missing-points", "Show missing compulsary points") do |v|
|
24
|
-
@options.show_missing_compulsory_points = true
|
25
|
-
end
|
26
|
-
opts.on("-c", "--completion-precentige", "Show completition percentige") do |v|
|
27
|
-
@options.show_completion_percentige = true
|
28
|
-
end
|
29
|
-
opts.on("-e", "--email emailaddress", "Show details for user") do |v|
|
30
|
-
@options.user_email = v
|
31
|
-
end
|
32
|
-
opts.on("-t", "--tmc-account tmc-account", "Show details for user") do |v|
|
33
|
-
@options.user_tmc_username = v
|
34
|
-
end
|
35
|
-
opts.on("-l", "--list", "Show the basic list") do |v|
|
36
|
-
@options.list = true
|
37
|
-
end
|
38
|
-
opts.on_tail("-h", "--help", "Show this message") do
|
39
|
-
puts opts
|
40
|
-
exit
|
41
|
-
end
|
42
|
-
end
|
43
|
-
opt.parse!(args)
|
44
|
-
|
45
|
-
json = maybe_fetch_json()
|
46
|
-
if @options.user
|
47
|
-
show_info_about(@options.user, 'username', json)
|
48
|
-
elsif @options.user_email
|
49
|
-
show_info_about(@options.user_email, 'email', json)
|
50
|
-
elsif @options.user_tmc_username
|
51
|
-
show_info_about(@options.user_tmc_username, 'username', json)
|
52
|
-
elsif @options.list
|
53
|
-
list_and_filter_participants(json)
|
54
|
-
else
|
55
|
-
$cache.write_file_to_cache('data.json', @notes.to_json)
|
56
|
-
puts opt
|
57
|
-
abort
|
58
|
-
end
|
59
|
-
$cache.write_file_to_cache('data.json', @notes.to_json)
|
60
|
-
end
|
61
|
-
|
62
|
-
def get_auth
|
63
|
-
print 'username: '
|
64
|
-
username = $stdin.gets.strip
|
65
|
-
print 'password: '
|
66
|
-
password = $stdin.noecho(&:gets).strip
|
67
|
-
puts
|
68
|
-
{username: username, password: password}
|
69
|
-
end
|
70
|
-
|
71
|
-
def maybe_fetch_json()
|
72
|
-
if @options.reload or @notes['user_info'].nil? or @notes['week_data'].nil?
|
73
|
-
|
74
|
-
t = -> do
|
75
|
-
loop do
|
76
|
-
print '.'
|
77
|
-
sleep 0.5
|
78
|
-
end
|
79
|
-
puts
|
80
|
-
end
|
81
|
-
|
82
|
-
auth = get_auth()
|
83
|
-
th = Thread.new(&t)
|
84
|
-
|
85
|
-
url = "http://tmc.mooc.fi/mooc/participants.json?api_version=7&utf8=%E2%9C%93&filter_koko_nimi=&column_username=1&column_email=1&column_koko_nimi=1&column_hakee_yliopistoon_2014=1&group_completion_course_id=18"
|
86
|
-
user_info = JSON.parse(HTTParty.get(url, basic_auth: auth).body)['participants']
|
87
|
-
week_data = fetch_week_datas(auth)
|
88
|
-
@notes['user_info'] = user_info.clone
|
89
|
-
@notes['week_data'] = week_data.clone
|
90
|
-
th.kill
|
91
|
-
puts
|
92
|
-
{participants: user_info, week_data: week_data}
|
93
|
-
else
|
94
|
-
{participants: @notes['user_info'].clone, week_data: @notes['week_data'].clone}
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def show_info_about(user, user_field = 'username', json)
|
99
|
-
participants = json[:participants]
|
100
|
-
week_data = json[:week_data]
|
101
|
-
my_user = participants.find{|a| a[user_field] == user }
|
102
|
-
if my_user.nil?
|
103
|
-
abort "User not found"
|
104
|
-
end
|
105
|
-
formatted_print_user_details ["Username", my_user['username']]
|
106
|
-
formatted_print_user_details ["Email", my_user['email']]
|
107
|
-
formatted_print_user_details ["Hakee yliopistoon", my_user['hakee_yliopistoon_2014']]
|
108
|
-
formatted_print_user_details ["Koko nimi", my_user['koko_nimi']]
|
109
|
-
|
110
|
-
if @options.show_completion_percentige
|
111
|
-
formatted_print_user_details ["Points per week"]
|
112
|
-
done_exercise_percents(my_user, participants).each do |k|
|
113
|
-
begin
|
114
|
-
k = k.first
|
115
|
-
formatted_print_user_details [k[0], k[1]]
|
116
|
-
rescue
|
117
|
-
nil
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
if @options.show_missing_compulsory_points
|
123
|
-
formatted_print_user_details ["Compulsory points"]
|
124
|
-
get_points_info_for_user(my_user, week_data).each do |k,v|
|
125
|
-
formatted_print_user_details [k, v.join(", ")]
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
def formatted_print_user_details(details)
|
132
|
-
case details.size
|
133
|
-
when 1
|
134
|
-
puts "%18s" % details
|
135
|
-
when 2
|
136
|
-
puts "%18s: %-20s" % details
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
def fetch_week_datas(auth)
|
141
|
-
base_url = "http://tmc.mooc.fi/mooc/courses/18/points/"
|
142
|
-
weeks = %w(1 2 3 4 5 6 7 8 9 10 11 12)
|
143
|
-
rest = ".json?api_version=7"
|
144
|
-
week_data = {}
|
145
|
-
weeks.each do |week|
|
146
|
-
week_data[week] = JSON.parse(HTTParty.get(base_url + week + rest, basic_auth: auth).body)['users_to_points']
|
147
|
-
end
|
148
|
-
week_data
|
149
|
-
end
|
150
|
-
|
151
|
-
def list_and_filter_participants(json)
|
152
|
-
wanted_fields = %W(username email koko_nimi)
|
153
|
-
|
154
|
-
participants = json[:participants]
|
155
|
-
week_data = json[:week_data]
|
156
|
-
everyone_in_course = participants.size
|
157
|
-
only_applying!(participants)
|
158
|
-
hakee_yliopistoon = participants.size
|
159
|
-
|
160
|
-
puts "%-20s %-35s %-25s %-120s" % ["Username", "Email", "Real name", "Missing points"]
|
161
|
-
puts '-'*200
|
162
|
-
participants.each do |participant|
|
163
|
-
nice_string_in_array = wanted_fields.map do |key|
|
164
|
-
participant[key]
|
165
|
-
end
|
166
|
-
if @options.show_completion_percentige
|
167
|
-
nice_string_in_array << format_done_exercises_percents(done_exercise_percents(participant, participants))
|
168
|
-
end
|
169
|
-
if @options.show_missing_compulsory_points
|
170
|
-
nice_string_in_array << missing_points_to_list_string(get_points_info_for_user(participant, week_data))
|
171
|
-
end
|
172
|
-
|
173
|
-
|
174
|
-
to_be_printed = "%-20s %-35s %-25s "
|
175
|
-
to_be_printed << "%-180s " if @options.show_completion_percentige
|
176
|
-
to_be_printed << "%-120s" if @options.show_missing_compulsory_points
|
177
|
-
|
178
|
-
puts to_be_printed % nice_string_in_array
|
179
|
-
end
|
180
|
-
|
181
|
-
puts
|
182
|
-
puts
|
183
|
-
puts "Stats: "
|
184
|
-
puts "%25s: %4d" % ["Kaikenkaikkiaan kurssilla", everyone_in_course]
|
185
|
-
puts "%25s: %4d" % ["Hakee yliopistoon", hakee_yliopistoon]
|
186
|
-
|
187
|
-
end
|
188
|
-
|
189
|
-
|
190
|
-
def format_done_exercises_percents(hash)
|
191
|
-
hash.map do |k|
|
192
|
-
begin
|
193
|
-
k = k.first
|
194
|
-
"#{k[0].scan(/\d+/).first}: #{k[1]}"
|
195
|
-
rescue
|
196
|
-
nil
|
197
|
-
end
|
198
|
-
end.compact.join(", ")
|
199
|
-
end
|
200
|
-
|
201
|
-
|
202
|
-
def done_exercise_percents(participant, participants_data)
|
203
|
-
user_info = participants_data.find{ |p| p['username'] == participant['username'] }
|
204
|
-
exercise_weeks = user_info['groups']
|
205
|
-
week_keys = (1..12).map{|i| "viikko#{i}"}
|
206
|
-
|
207
|
-
week_keys.map do |week|
|
208
|
-
details = exercise_weeks[week]
|
209
|
-
unless details.nil?
|
210
|
-
{week => ("%3.1f%" % [(details['points'].to_f / details['total'].to_f) * 100])}
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
def missing_points_to_list_string(missing_by_week)
|
216
|
-
str = ""
|
217
|
-
missing_by_week.keys.each do |week|
|
218
|
-
missing = missing_by_week[week]
|
219
|
-
unless missing.nil? or missing.length == 0
|
220
|
-
str << week
|
221
|
-
str << ": "
|
222
|
-
str << missing.join(",")
|
223
|
-
str << " "
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
str
|
228
|
-
|
229
|
-
end
|
230
|
-
|
231
|
-
def get_points_info_for_user(participant, week_data)
|
232
|
-
# TODO: täydennä data viikolle 12
|
233
|
-
compulsory_exercises = {'6' => %w(102.1 102.2 102.3 103.1 103.2 103.3), '7' => %w(116.1 116.2 116.3), '8' => %w(124.1 124.2 124.3 124.4),
|
234
|
-
'9' => %w(134.1 134.2 134.3 134.4 134.5), '10' => %w(141.1 141.2 141.3 141.4), '11' => %w(151.1 151.2 151.3 151.4), '12' => %w()}
|
235
|
-
points_by_week = {}
|
236
|
-
week_data.keys.each do |week|
|
237
|
-
points_by_week[week] = week_data[week][participant['username']]
|
238
|
-
end
|
239
|
-
|
240
|
-
|
241
|
-
missing_by_week = {}
|
242
|
-
points_by_week.keys.each do |week|
|
243
|
-
weeks_points = points_by_week[week] || [] #palauttaa arrayn
|
244
|
-
weeks_compulsory_points = compulsory_exercises[week] || []
|
245
|
-
missing_by_week[week] = weeks_compulsory_points - weeks_points
|
246
|
-
end
|
247
|
-
|
248
|
-
missing_by_week
|
249
|
-
end
|
250
|
-
|
251
|
-
def only_applying!(participants)
|
252
|
-
participants.select! do |participant|
|
253
|
-
participant['hakee_yliopistoon_2014']
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|