Skip to content

Commit

Permalink
feat(fetcher/fetchHTML): support searching for calendar week
Browse files Browse the repository at this point in the history
  • Loading branch information
philippdormann committed Apr 12, 2023
1 parent 90b73a6 commit 1d07d79
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions api/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,39 +123,73 @@ async function fetchHTML({
provider,
kw = getCalendarWeek(),
auth = false,
nextWeek = false,
__EVENTVALIDATION = '',
__VIEWSTATE = '',
__VIEWSTATEGENERATOR = ''
}) {
console.log('@@fetchHTML');
let requestData = undefined;
let requestMethod = 'GET';
let url = `https://${provider}/LOGINPLAN.ASPX`;
if (auth === true) {
requestData = { __VIEWSTATE, __VIEWSTATEGENERATOR, btnLogin: '' };
requestData = {
__VIEWSTATE,
__EVENTVALIDATION,
__VIEWSTATEGENERATOR,
btnLogin: ''
};
requestMethod = 'POST';
}
if (nextWeek) {
requestData.btnVor = '>';
requestData.__EVENTARGUMENT = '';
requestData.__EVENTTARGET = '';
url = `https://${provider}/mensamax/Wochenplan/WochenplanExtern/WochenPlanExternForm.aspx`;
}
const { data } = await client.request({
url: `https://${provider}/LOGINPLAN.ASPX`,
url,
params: { p, e },
method: requestMethod,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: requestData
});
if (data.includes(`lblWoche`)) {
return data;
}
const $ = cheerio.load(data);
__EVENTVALIDATION = $('#__EVENTVALIDATION').val();
__VIEWSTATE = $('#__VIEWSTATE').val();
__VIEWSTATEGENERATOR = $('#__VIEWSTATEGENERATOR').val();
if (data.includes('btnLogin')) {
console.log('login performed');
const $ = cheerio.load(data);
const __VIEWSTATE = $('#__VIEWSTATE').val();
const __VIEWSTATEGENERATOR = $('#__VIEWSTATEGENERATOR').val();
return await fetchHTML({
p,
e,
provider,
kw,
auth: true,
__EVENTVALIDATION,
__VIEWSTATE,
__VIEWSTATEGENERATOR
});
}
//
var kwText = $('#lblWoche').text();
console.log({ kwText });
if (kwText.includes(`(KW${kw})`)) {
console.log('kw found!');
// console.log(data);
return data;
} else {
console.log('next week...');
return await fetchHTML({
p,
e,
provider,
kw,
auth: true,
nextWeek: true,
__EVENTVALIDATION,
__VIEWSTATE,
__VIEWSTATEGENERATOR
});
Expand Down

0 comments on commit 1d07d79

Please sign in to comment.