uw_learn 1.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/uw_learn +13 -0
- data/lib/uw_learn.rb +118 -0
- metadata +47 -0
data/bin/uw_learn
ADDED
data/lib/uw_learn.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'mechanize'
|
2
|
+
require 'hpricot'
|
3
|
+
require 'awesome_print'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'httparty'
|
6
|
+
require 'rainbow'
|
7
|
+
|
8
|
+
require './lib/uw_learn/grade'
|
9
|
+
require './lib/uw_learn/course'
|
10
|
+
|
11
|
+
class Uwlearn
|
12
|
+
|
13
|
+
def initialize(login, password)
|
14
|
+
@user_login = login
|
15
|
+
@user_passw = password
|
16
|
+
|
17
|
+
@learn_courses = Array.new
|
18
|
+
@learn_grades = Array.new
|
19
|
+
|
20
|
+
if @user_login.nil? || @user_passw.nil?
|
21
|
+
raise "Couldn't authenticate. Please try again.".foreground(:red)
|
22
|
+
end
|
23
|
+
|
24
|
+
start
|
25
|
+
end
|
26
|
+
|
27
|
+
def login
|
28
|
+
@user_login
|
29
|
+
end
|
30
|
+
|
31
|
+
def print_courses
|
32
|
+
@learn_courses.each do |course|
|
33
|
+
puts course.name.foreground(:yellow)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def courses
|
38
|
+
@learn_courses
|
39
|
+
end
|
40
|
+
|
41
|
+
def print_grades
|
42
|
+
@learn_grades.each do |grade|
|
43
|
+
puts grade.which_course?.foreground(:yellow) + ": ".foreground(:yellow)
|
44
|
+
puts grade.summary
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def grades
|
49
|
+
@learn_grades
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def learn_url
|
55
|
+
"http://learn.uwaterloo.ca"
|
56
|
+
end
|
57
|
+
|
58
|
+
def course_url
|
59
|
+
"/d2l/lp/ouHome/home.d2l?ou="
|
60
|
+
end
|
61
|
+
|
62
|
+
def course_html
|
63
|
+
"//ul[@id='z_bl']//div[@class='dco_c']//a"
|
64
|
+
end
|
65
|
+
|
66
|
+
def start
|
67
|
+
agent = Mechanize.new
|
68
|
+
|
69
|
+
begin
|
70
|
+
links = authenticate agent
|
71
|
+
acquire links, agent
|
72
|
+
puts "Grades acquired.".foreground(:cyan)
|
73
|
+
rescue Exception => e
|
74
|
+
puts e.message
|
75
|
+
#puts e.backtrace.inspect
|
76
|
+
rescue Mechanize::ResponseCodeError, Net::HTTPNotFound
|
77
|
+
puts "Looks like the website has changed. Contact the developer to fix this issue.".foreground(:red)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
def authenticate(agent)
|
83
|
+
puts "Authenticating ".foreground(:cyan) + @user_login.foreground(:cyan) + "...".foreground(:cyan)
|
84
|
+
|
85
|
+
page = agent.get learn_url
|
86
|
+
form = page.forms.first
|
87
|
+
form.username = @user_login
|
88
|
+
form.password = @user_passw
|
89
|
+
page = agent.submit form
|
90
|
+
|
91
|
+
course_links = page.search course_html
|
92
|
+
if course_links.empty?
|
93
|
+
raise "Couldn't authenticate. Please try again.".foreground(:red)
|
94
|
+
end
|
95
|
+
|
96
|
+
course_links
|
97
|
+
end
|
98
|
+
|
99
|
+
def acquire(links, agent)
|
100
|
+
puts "Acquiring data...".foreground(:cyan)
|
101
|
+
|
102
|
+
links.each do |link|
|
103
|
+
name = link.inner_html.to_s
|
104
|
+
code = link["href"].to_s.sub course_url, ""
|
105
|
+
|
106
|
+
if name == "" || code == ""
|
107
|
+
raise "Incorrect parsing. Please contact the developer.".foreground(:red)
|
108
|
+
end
|
109
|
+
|
110
|
+
grade = Grade.new name, code, agent
|
111
|
+
course = Course.new name, code, grade
|
112
|
+
|
113
|
+
@learn_courses << course
|
114
|
+
@learn_grades << grade
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uw_learn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.pre
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gaurav Mali
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Gets student grades from University of Waterloo's D2L website.
|
15
|
+
email: hello@gauravmali.com
|
16
|
+
executables:
|
17
|
+
- uw_learn
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/uw_learn.rb
|
22
|
+
- bin/uw_learn
|
23
|
+
homepage: https://github.com/GMali/uw_learn
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>'
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.3.1
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.24
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Tiny web crawler for University of Waterloo students
|
47
|
+
test_files: []
|