Skip to content

Commit

Permalink
refactor(fetcher): fetchHTML improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
philippdormann committed Apr 12, 2023
1 parent 7082745 commit 90b73a6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
64 changes: 41 additions & 23 deletions api/fetcher.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require('dotenv').config();
const request = require('request').defaults({ jar: true });
// 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 { writeFileSync } = require('fs');
const jar = new CookieJar();
const client = wrapper(axios.create({ jar }));
// =========
Expand Down Expand Up @@ -116,32 +117,49 @@ function getMensaPlanHTML({ p, e, kw = getCalendarWeek() }) {
/**
* @returns {string} html content of mensaplan
*/
async function fetchHTML({ p, e, provider, kw }) {
return new Promise(async (resolve, reject) => {
const { data } = await client.get(
`https://${provider}/LOGINPLAN.ASPX`,
{
params: { p, e },
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);
//
async function fetchHTML({
p,
e,
provider,
kw = getCalendarWeek(),
auth = false,
__VIEWSTATE = '',
__VIEWSTATEGENERATOR = ''
}) {
console.log('@@fetchHTML');
let requestData = undefined;
let requestMethod = 'GET';
if (auth === true) {
requestData = { __VIEWSTATE, __VIEWSTATEGENERATOR, btnLogin: '' };
requestMethod = 'POST';
}
const { data } = await client.request({
url: `https://${provider}/LOGINPLAN.ASPX`,
params: { p, e },
method: requestMethod,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: requestData
});
if (data.includes(`lblWoche`)) {
return data;
}
if (data.includes('btnLogin')) {
console.log('login performed');
const $ = cheerio.load(data);
const __VIEWSTATE = $('#__VIEWSTATE').val();
const __VIEWSTATEGENERATOR = $('#__VIEWSTATEGENERATOR').val();
const resp = await client.request({
method: 'POST',
url: `https://${provider}/LOGINPLAN.ASPX`,
params: { p, e },
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: { __VIEWSTATE, __VIEWSTATEGENERATOR, btnLogin: '' }
return await fetchHTML({
p,
e,
provider,
kw,
auth: true,
__VIEWSTATE,
__VIEWSTATEGENERATOR
});
resolve(resp.data);
});
}
}
exports.getMensaPlanHTML = getMensaPlanHTML;
exports.fetchHTML = fetchHTML;
19 changes: 14 additions & 5 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
const { fetchHTML } = require('./api/fetcher');
const { parser, getMensaPlanHTML } = require('./main');
const fs = require('fs');

(async function test() {
try {
const html = await getMensaPlanHTML({
const html = await fetchHTML({
p: 'FO111',
e: 'herz'
e: 'herz',
kw: 17,
provider: 'mensadigital.de'
});
if (html.includes('Klicken Sie hier um sich neu anzumelden')) {
console.log('FAIL');
} else {
console.log('SUCCESS');
}
// console.log(html);
const parsed = await parser(html);
console.log(parsed);
fs.writeFileSync('./out.json', JSON.stringify(parsed));
fs.writeFileSync('./out.html', html);
// const parsed = await parser(html);
// console.log(parsed);
// fs.writeFileSync('./out.json', JSON.stringify(parsed));
} catch (error) {
console.log(error);
}
Expand Down

0 comments on commit 90b73a6

Please sign in to comment.