-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassLooper.js
198 lines (183 loc) · 6.79 KB
/
ClassLooper.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var worker = require('./worker');
var pg = require('pg');
require('./database')
//var database_URL = "postgres://postgres@localhost/cuny_first_db";
pg.defaults.poolIdleTimeout = 2000;
var logger = require('tracer').console({
format : [ "<{{title}}> {{file}}:{{line}}: {{message}}",
{
error: "<{{title}}> {{file}}:{{line}}: {{message}} \nCall Stack: {{stack}}"
}
],
preprocess: function(data){ data.title = data.title.toUpperCase()}
})
global.logger = logger
/*(function myLoop (i) {
setTimeout(function () {
alert('hello'); // your code here
if (--i) myLoop(i); // decrement i and call myLoop again if i > 0
}, 3000)
})(10); */
/*getInst(function(q){
for (i in q){
setTimeout ( getSession('QNS01', function (r, m){
logger.log(i)
logger.log(m)
logger.log("____________________________")
logger.log("____________________________")
}), 1000*60)
}
})*/
allClasses = function (callback){
/*getInst(function(inst){
var institutionsArray= []
var institutionsArray2= []
for (i in inst){
institutionsArray.push(inst[i])
institutionsArray2.push({id: inst[i], name: i})
}*/
institutionsArray = ['QNS01']
allSessions(institutionsArray, function (array){
allSubjects(array, function(array1){
allSections(array1, function(array2){
callback (array2)
})
})
//})
});
}
allSessions = function (institutions, callback){
var sessionArray= []
var s= function sessionLoop (i) {
setTimeout(function () {
getSession(institutions[--i], function (r, m){
for (j in m){
var item = {inst: institutions[i], session: m[j], name:j}
sessionArray.push(item)
}
})
if (i>-1) sessionLoop(i); //it's i>-1 because this way it'll try to iterate an extra time before the callback, buying us an extra 3 seconds
else if( sessionArray.length ) callback(sessionArray)
}, 1000*8)
};
s(institutions.length)
}
allSubjects = function (array, callback){
var classArray= []
var s= function sessionLoop (i) {
setTimeout(function () {
if (i>0){
getDept(array[--i].inst, array[i].session, function (m){
for (j in m){
var item = {inst: array[i].inst, session: array[i].session, subject: m[j], subject_name: j}
classArray.push(item)
}
})
}
else{
i--
}
if (i>-1) sessionLoop(i); //it's i>-1 because this way it'll try to iterate an extra time before the callback, buying us an extra 3 seconds
else callback(classArray)
}, 1000*8)
};
s(array.length)
}
allSections = function(array, callback){
var sectionsArray = []
var s = function sectionLoop(i){
setTimeout(function(){
getSections(array[--i].inst, array[i].session, array[i].subject, function(classData){
for(classNbr in classData){
for (c in classData[classNbr]){
var data = classData[classNbr][c]
var tmp= array[i]
for (d in data){
if(data[d] === undefined || data[d] === "<br />")
data[d] = "";
tmp[d] = data[d]
}
var classInfo = tmp
if(classInfo["Class Title"] === undefined)
continue
classInfo["Class Title"] = classInfo["Class Title"].split("\n").join("").split("\t").join("").split("<br />").join(" ").split("\r").join(" ")
classInfo.Topic = classInfo.Topic.split("\n").join("").split("\t").join("").split("<br />").join(" ").split("\r").join(" ")
params = [ {
"inst" : classInfo.inst,
"session" : classInfo.session,
"dept" : classInfo.Dept,
"classNbr" : classNbr,
"section" : classInfo.Section,
"title" : classInfo["Class Title"],
"days_and_times" : classInfo['Days & Times'],
"Status" : classInfo.Status==('Open'),
"topic" : classInfo.Topic,
"room" : classInfo.Room,
"meeting_dates": classInfo['Dates'],
"teacher" : classInfo.Instructor,
"labLec" : classInfo[ "className" ], // lab or lec
}]
sectionsArray.push(params)
}
}
})
if (i > 0) sectionLoop(i); //it's i>-1 because this way it'll try to iterate an extra time before the callback, buying us an extra 3 seconds
else {
logger.log("we're up to here")
callback(sectionsArray)
}
},1000*8);
}
s(array.length)
}
putInDatabase = function (client, params, a, outOf){
paramas= params.slice()
var classNum = [ paramas[4] ]
logger.log('class num is ')
logger.log(classNum)
logger.log('we dealing with '+a+' out of '+outOf )
client.query("INSERT INTO classes VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)", paramas, function(err, result) {
if(err) {
err["Error"] = true;
logger.error('error running query2', err);
}
})
}
allInst = function(callback){
getInst(function(institutions){
callback(Object.keys(institutions).map(function(key){ return institutions[key] }))
})
}
updateAllSessions = function(table){
allInst(function(institutions){
allSessions(institutions, function(sessionArray){
logger.log(sessionArray)
var q = "INSERT INTO " + table + " (name, id, school) values \n("
for(var index = 0; index < sessionArray.length - 1; index++){
var item = sessionArray[index]
q += "\'" + item.name + "\', \'" + item.session + "\', \'" + item.inst + "\' ), ("
}
q = q.substring(0, q.lastIndexOf(","))
logger.log(q)
})
})
}
allClasses( function(a){
var q = "INSERT INTO newClasses (inst, session, dept, classNbr, section, title, days_and_times, open, topic, room, meeting_dates, teacher, lablec) values ("
logger.log(a[0])
logger.log(a[0][0])
for(var i in a){
var obj = a[i][0]
if(obj !== undefined)
q += "\'" + obj.inst + "\', \'" + obj.session + "\', \'" + obj.dept + "\', \'" + obj.classNbr + "\', \'" + obj.section + "\', \'" + obj.title + "\', \'" + obj.days_and_times
+ "\'," + obj.Status + ", \'" + obj.topic + "\', \'" + obj.room + "\', \'" + obj.meeting_dates + "\', \'" + obj.teacher + "\', \'" + obj.labLec + "\'), ("
}
q = q.substring(0, q.lastIndexOf(","))
sendQuery(q,[],function(result){
logger.log(result)
})
})
//updateAllSessions()
//getSections('QNS01', '1169', 'PHYS', function (m){
// logger.log(m)
//})