-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchgrades.js
66 lines (59 loc) · 1.89 KB
/
fetchgrades.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
require('dotenv').config()
const axios = require('axios')
const cheerio = require('cheerio')
const request = require('request')
const check = require('./html-filtering')
const grdSource = 'https://gradebook.dadeschools.net/Pinnacle/Gradebook/InternetViewer/GradeSummary.aspx'
const envStudentID = process.env.STUDENT
const initFetchURL = `https://mdcpsportalapps2.dadeschools.net/PIVredirect/?ID=${envStudentID}`
const go = async () => new Promise((resolve) => {
axios.get(initFetchURL)
.then(async response => {
const { data } = response
// console.log(data)
const $ = cheerio.load(data)
const postSRC = $('form').attr('action')
const StudentID = $('input[name="StudentID"]').attr('value')
return procTokens(postSRC, StudentID, resolve)
})
.catch(async () => go())
})
const procTokens = async (postSRC, StudentID, resolve) => {
const options = {
method: 'POST',
url: postSRC,
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
action: 'trans',
StudentID: StudentID
}
}
return request(options, async (e, res) => {
if (e) return procTokens(postSRC, StudentID)
const cookies = res.headers['set-cookie']
.reverse()
.map(a => a.split(';')[0])
.join('; ')
return fetchGrades(cookies, resolve)
})
}
const fetchGrades = async (cookies, resolve) => {
const options = {
method: 'GET',
url: grdSource,
headers: {
'Referer': initFetchURL,
'Cookie': cookies,
'Pragma': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.0.0 Safari/537.36'
}
}
return request(options, async (_, r, body) => {
const dataGrades = check(body)
resolve(dataGrades)
})
}
module.exports = () => new Promise((resolve) => go().then(data => resolve(data)))