-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
160 lines (126 loc) · 3.83 KB
/
main.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
152
153
154
155
156
157
158
159
160
require 'sinatra'
require 'json'
require_relative 'requests.rb'
require_relative 'parser.rb'
# Sample route
get '/' do
'API is live.'
end
# Test route to check if login works
post '/login' do
content_type :json
data = JSON.parse(request.body.read)
username, password, error = Utils.check_credentials(data)
unless error.nil?
return error
end
session_status = SLCM.get_session_cookie()
unless session_status[:success]
return session_status.to_json
end
session = session_status[:message]
SLCM.login_user(username, password, session).to_json
end
# Route to fetch attendance
post '/bunks' do
content_type :json
data = JSON.parse(request.body.read)
auth_status = Utils.auth_request(data)
unless auth_status[:success]
return auth_status.to_json
end
session = auth_status[:message]
html_success, html = SLCM.get_academics_page(session)
unless html_success
return Utils.send_failure()
end
details = Parser.get_attendance(html)
{ success: true, data: details }.to_json
end
# Route to fetch course details
post '/courses' do
content_type :json
data = JSON.parse(request.body.read)
auth_status = Utils.auth_request_with_semester(data)
unless auth_status[:success]
return auth_status.to_json
end
semester = auth_status[:semester]
session = auth_status[:session]
academics_html_success, academics_html = SLCM.get_academics_page(session)
unless academics_html_success
return Utils.send_failure()
end
viewstate, eventvalidation = Parser.get_auth(academics_html)
html_success, html = SLCM.get_academics_info(session, viewstate, eventvalidation, semester)
unless html_success
return Utils.send_failure()
end
details = Parser.get_courses(html)
{ success: true, data: details }.to_json
end
# Route to fetch gradesheet details
post '/grades' do
content_type :json
data = JSON.parse(request.body.read)
auth_status = Utils.auth_request_with_semester(data)
unless auth_status[:success]
return auth_status.to_json
end
semester = auth_status[:semester]
session = auth_status[:session]
gradesheet_html_success, gradesheet_html = SLCM.get_gradesheet_page(session)
unless gradesheet_html_success
return Utils.send_failure()
end
viewstate, eventvalidation = Parser.get_auth(gradesheet_html)
html_success, html = SLCM.get_gradesheet_info(session, viewstate, eventvalidation, semester)
unless html_success
return Utils.send_failure()
end
details = Parser.get_grades(html)
{ success: true, data: details }.to_json
end
# Route to fetch student details
post '/student' do
content_type :json
data = JSON.parse(request.body.read)
auth_status = Utils.auth_request(data)
unless auth_status[:success]
return auth_status.to_json
end
session = auth_status[:message]
html_success, html = SLCM.get_student_page(session)
unless html_success
return Utils.send_failure()
end
details = Parser.get_profile(html)
{ success: true, data: details }.to_json
end
# Route to fetch internal marks details
post '/marks' do
content_type :json
data = JSON.parse(request.body.read)
auth_status = Utils.auth_request_with_semester(data)
unless auth_status[:success]
return auth_status.to_json
end
semester = auth_status[:semester]
session = auth_status[:session]
academics_html_success, academics_html = SLCM.get_academics_page(session)
unless academics_html_success
return Utils.send_failure()
end
viewstate, eventvalidation = Parser.get_auth(academics_html)
html_success, html = SLCM.get_marks_info(session, viewstate, eventvalidation, semester)
unless html_success
return Utils.send_failure()
end
details = Parser.get_internal_marks(html)
{ success: true, data: details }.to_json
end
# Route to fetch all details
post '/init' do
content_type :json
return Utils.send_status(false, 'Route is incomplete').to_json
end