Skip to content

Commit

Permalink
refactor!: replace deprecated request module with axios + cookie jar …
Browse files Browse the repository at this point in the history
…support
  • Loading branch information
philippdormann committed Apr 12, 2023
1 parent 8f89e35 commit 7082745
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 261 deletions.
47 changes: 22 additions & 25 deletions api/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ require('dotenv').config();
const request = require('request').defaults({ jar: true });
const cheerio = require('cheerio');
const axios = require('axios').default;
const { wrapper } = require('axios-cookiejar-support');
const { CookieJar } = require('tough-cookie');
const institutions = require('../institutions.json');
const jar = new CookieJar();
const client = wrapper(axios.create({ jar }));
// =========
process.env.CACHE_TIME_MINUTES = parseInt(process.env.CACHE_TIME_MINUTES || 1);
// =========
Expand Down Expand Up @@ -114,36 +118,29 @@ function getMensaPlanHTML({ p, e, kw = getCalendarWeek() }) {
*/
async function fetchHTML({ p, e, provider, kw }) {
return new Promise(async (resolve, reject) => {
const { data } = await axios.get(`https://${provider}/LOGINPLAN.ASPX`, {
params: { p, e },
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
const { data } = await client.get(
`https://${provider}/LOGINPLAN.ASPX`,
{
params: { p, e },
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
});
//
);
//
const $ = cheerio.load(data);
const __VIEWSTATE = $('#__VIEWSTATE').val();
const __VIEWSTATEGENERATOR = $('#__VIEWSTATEGENERATOR').val();
request(
{
followAllRedirects: true,
method: 'POST',
url: `https://${provider}/LOGINPLAN.ASPX`,
qs: { p, e },
formData: {
__VIEWSTATE,
__VIEWSTATEGENERATOR,
btnLogin: ''
}
const resp = await client.request({
method: 'POST',
url: `https://${provider}/LOGINPLAN.ASPX`,
params: { p, e },
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
(error, response, body) => {
if (error) {
reject('fetch_step2');
} else {
resolve(body);
}
}
);
data: { __VIEWSTATE, __VIEWSTATEGENERATOR, btnLogin: '' }
});
resolve(resp.data);
});
}
exports.getMensaPlanHTML = getMensaPlanHTML;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
],
"dependencies": {
"axios": "1.3.5",
"axios-cookiejar-support": "^4.0.6",
"cheerio": "1.0.0-rc.12",
"dotenv": "16.0.3",
"html-minifier": "4.0.0",
"ioredis": "^5.3.1",
"path": "0.12.7",
"rayo": "^1.4.0",
"request": "2.88.2"
"tough-cookie": "^4.1.2"
},
"devDependencies": {
"auto-changelog": "2.4.0",
Expand Down
Loading

0 comments on commit 7082745

Please sign in to comment.