-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-cross-listings.js
159 lines (115 loc) · 4.79 KB
/
import-cross-listings.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
// this program just needs to be run once a semester
// TODO: integrate as check function in the main program
const GoogleSpreadsheet = require('google-spreadsheet');
const { promisify } = require('util');
require('dotenv').config()
// website loading
const axios = require("axios");
// build credential object
let creds = {
type: process.env.type,
project_id: process.env.project_id,
private_key_id: process.env.private_key_id,
private_key: JSON.parse(`"${process.env.private_key}"`), // escape newlines in string
client_email: process.env.client_email,
client_id: process.env.client_id,
auth_uri: process.env.auth_uri,
token_uri: process.env.token_uri,
auth_provider_x509_cert_url: process.env.auth_provider_x509_cert_url,
client_x509_cert_url: process.env.client_x509_cert_url,
}
// call main function
accessSpreadsheet()
// async to open spreadsheet
async function accessSpreadsheet() {
let sheetId = '1DjsN1HiiS7Iv7lKNucjeoQ6aS0_291JAovZ0LfgOItM'
let testId = '1wtHWjTTWn9LNp4r8_xJiGGiO-YV4PsoQ_gWTeahbUxs'
const doc = new GoogleSpreadsheet(testId);
// pass credentials to doc
await promisify(doc.useServiceAccountAuth)(creds);
const info = await promisify(doc.getInfo)();
// load spreadsheet as rows
const staticCourseInfoSheet = info.worksheets[2]
const rowsOfStaticCourseInfo = await promisify(staticCourseInfoSheet.getRows)({})
console.log("Spreadsheet loaded")
// grab string object from uvm registrar's page
let websiteString = await getCourseData()
let arrayOfCrossListings = getCrossListings(websiteString)
crossListingsToSpreadsheet(arrayOfCrossListings, rowsOfStaticCourseInfo)
}
async function getCourseData() {
const courseDataSource = "https://giraffe.uvm.edu/~rgweb/batch/curr_enroll_spring.html"
try {
let response = await axios.get(courseDataSource);
courseData = response.data
} catch (error) {
console.error(error);
}
return courseData
}
function getCrossListings(courseDataString) {
// let mainData = document.getElementsByTagName("pre")[0]
// let dataContent = mainData.textContent
// let rowsOfData = dataContent.split("\n")
let rowsOfData = courseDataString.split("\n")
// rows of all cross listed courses
crossListedCourses = rowsOfData.filter(row => row.includes("XL:"))
let allRowCRNs = []
// new idea with matching - first CRN is current course
crossListedCourses.forEach(row => {
allRowCRNs.push(row.match(/\b\d{5}\b/g))
})
return allRowCRNs
}
function crossListingsToSpreadsheet(arrayOfCrossListings, rowsOfStaticCourseInfo) {
// rowsOfStaticCourseInfo.forEach(spreadsheetRow => {
// arrayOfCrossListings.forEach(xlData => {
// if (spreadsheetRow.compnumb == xlData[0]) {
// matches.push(xlData)
// }
// })
// })
arrayOfCrossListings.forEach(row => {
rowsOfStaticCourseInfo.forEach(dataRow => {
if (row[0] == dataRow.compnumb) {
// commented out to be make main program easier to write // removes first CRN (which is the main course CRN)
// let splicedCrossListings = row.slice(1)
// convert to string for spreadsheet reading
// let formattedCrossListings = splicedCrossListings.toString()
// ship out array values as one string for the spreadsheet cell
let formattedCrossListings = row.toString()
// for some reason is losing the comma
dataRow.crosslistings = formattedCrossListings
dataRow.save()
}
})
})
// check to see if there are duplicate listings from the main data sheet
// listOfDuplicates = []
// rowsOfStaticCourseInfo.filter(checkingRow => {
// if (checkingRow.compnumb == checkingRow.compnumb) {
// listOfDuplicates.push()
// }
// })
// listOfDuplicates.forEach(item => {
// if (item.length > 0) {
// console.log(item)
// }
// })
// mainList = []
// rowsOfStaticCourseInfo.forEach(checkingRow => {
// rowsOfStaticCourseInfo.forEach(row => {
// if (checkingRow.compnumb == row.compnumb) {
// mainList.push(checkingRow)
// }
// })
// })
// console.log("xlData duplicates",findDuplicates(matches).length)
// console.log("Main block duplicates",findDuplicates(courseNumbs).length)
// console.log("# of cross-listings", arrayOfCrossListings.length)
// console.log("# of times CompNumb & cross-listings matched", matches.length)
// console.log("Length of all course data",rowsOfStaticCourseInfo.length)
// if (allRowCRNs[i][0] == row.compnumb) {
// row.crosslistings = allRowCRNs[i]
// }
}