Skip to content

Commit

Permalink
check for existing provider/project + cache usage
Browse files Browse the repository at this point in the history
  • Loading branch information
philippdormann committed Sep 27, 2022
1 parent 3bbba2a commit 852d0f8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 40 deletions.
115 changes: 77 additions & 38 deletions api/fetcher.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,95 @@
require('dotenv').config();
const request = require('request').defaults({ jar: true });
const cheerio = require('cheerio');
const axios = require('axios').default;
const institutions = require('../institutions.json');
// =========
const mensaplanCache = { cacheTimestamp: 0, cacheContent: {} };
// =========
/**
* @returns {string} html content of mensaplan
*/
exports.fetcher = ({ p, e }) => {
function getMensaplanHTML({ p, e }) {
return new Promise(function (resolve, reject) {
if (p && e) {
const found = institutions.find(function (ins) {
return ins.project === p && ins.facility === e;
});
axios
.get(`https://${found.provider}/LOGINPLAN.ASPX`, {
params: { p, e },
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (response) {
const $ = cheerio.load(response.data);
const __VIEWSTATE = $('#__VIEWSTATE').val();
const __VIEWSTATEGENERATOR = $(
'#__VIEWSTATEGENERATOR'
).val();
request(
{
followAllRedirects: true,
method: 'POST',
url: `https://${found.provider}/LOGINPLAN.ASPX`,
qs: { p, e },
formData: {
__VIEWSTATE,
__VIEWSTATEGENERATOR,
btnLogin: ''
}
},
(error, response, body) => {
if (error) {
reject('fetch_step2');
} else {
resolve(body);
}
}
if (found) {
if (process.env.CACHE === 'NONE') {
return fetch({ p, e });
} else {
const cacheTimeMinutes = parseInt(
process.env.CACHE_TIME_MINUTES || 1
);
})
.catch(function (error) {
reject('fetch_step1');
});
if (
Date.now() >
mensaplanCache.cacheTimestamp +
cacheTimeMinutes * 60 * 1000
) {
// reload data
mensaplanCache.cacheTimestamp = Date.now();
fetchHTML({ p, e, provider: found.provider }).then(
(data) => {
mensaplanCache.cacheContent = data;
resolve(mensaplanCache.cacheContent);
}
);
} else {
// still in cache
resolve(mensaplanCache.cacheContent);
}
}
} else {
reject('404');
}
} else {
reject('parameters');
}
});
};
}
// =========
/**
* @returns {string} html content of mensaplan
*/
function fetchHTML({ p, e, provider }) {
return new Promise(function (resolve, reject) {
axios
.get(`https://${provider}/LOGINPLAN.ASPX`, {
params: { p, e },
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (response) {
const $ = cheerio.load(response.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: ''
}
},
(error, response, body) => {
if (error) {
reject('fetch_step2');
} else {
resolve(body);
}
}
);
})
.catch(function (error) {
reject('fetch_step1');
});
});
}
exports.getMensaplanHTML = getMensaplanHTML;
exports.fetcher = fetchHTML;
4 changes: 2 additions & 2 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { fetcher } = require('./fetcher');
const { getMensaplanHTML } = require('./fetcher');
const { parser } = require('./parser');
module.exports = async function start_it_up(req, res) {
try {
const html = await fetcher({ p: req.query.p, e: req.query.e });
const html = await getMensaplanHTML({ p: req.query.p, e: req.query.e });
const parsed = await parser(html);
let payload = parsed.json;
if (req.query.details === 'true') {
Expand Down

0 comments on commit 852d0f8

Please sign in to comment.