summaryrefslogtreecommitdiffstats
path: root/assets/js/lopolisc.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/js/lopolisc.js')
-rw-r--r--assets/js/lopolisc.js165
1 files changed, 151 insertions, 14 deletions
diff --git a/assets/js/lopolisc.js b/assets/js/lopolisc.js
index 6170ede..dfd9ff6 100644
--- a/assets/js/lopolisc.js
+++ b/assets/js/lopolisc.js
@@ -4,12 +4,15 @@ function getStringBetween(string, start, end) {
const LOPOLIS_URL = "https://lopolis.gimb.tk/";
const LOPOLISC_ERR_NET = "LOPOLSIC NETWORK ERROR (ajax error)";
-const LOPOLISC_ERR_NET_POSTBACK_GET = "LOPOLISC NETWORK ERROR (ajax error) in postback GET"
-const LOPOLISC_ERR_NET_POSTBACK_POST = "LOPOLISC NETWORK ERROR (ajax error) in postback POST"
+const LOPOLISC_ERR_NET_POSTBACK_GET = "LOPOLISC NETWORK ERROR (ajax error) "+
+ "in postback GET";
const LOPOLISC_ERR_LOGIN = "LOPOLISC LOGIN ERROR";
-const LOPOLISC_ERR_NOTAPPLIED = "LOPOLISC DATA NOT APPLIED ERROR"
-const LOPOLISC_SIGNATURE = "lopolisc.js neuradni API - anton<at>sijanec.eu"
-
+const LOPOLISC_ERR_NET_POSTBACK_POST = "LOPOLISC NETWORK ERROR (ajax error) "+
+ "in postback POST";
+const LOPOLISC_ERR_NET_POSTBACK_POST_IN_POSTBACK = "LOPOLISC NETWORK ERROR $$$";
+const LOPOLISC_ERR_NOTAPPLIED = "LOPOLISC DATA NOT APPLIED ERROR";
+const LOPOLISC_SIGNATURE = "lopolisc.js neuradni API - anton<at>sijanec.eu";
+const LOPOLISC_ERR_OUT_OF_RETRIES = "LOPOLISC ERROR NI VEČ POSKUSOV!";
class lopolisc {
constructor() {
@@ -58,6 +61,7 @@ class lopolisc {
type: "POST",
data: params,
dataType: "text",
+ maxRetries: 3,
success: (postData, textStatus, xhr) => {
resolve({data: postData, textStatus: textStatus, code: xhr.status});
},
@@ -80,12 +84,15 @@ class lopolisc {
type: "GET",
dataType: "html",
success: (data) => {
- if (useDiffAction == true) {
+ if (useDiffAction === true) {
useDiffAction = getUrl;
}
- this.parseAndPost(data, params, formId, useDiffAction).then((value) => {
- resolve(value);
- });
+ this.parseAndPost(data, params, formId, useDiffAction)
+ .then((value) => {
+ resolve(value);
+ }).catch((e)=>{
+ reject(new Error(LOPOLISC_ERR_NET_POSTBACK_POST_IN_POSTBACK));
+ });
},
error: () => {
reject(new Error(LOPOLISC_ERR_NET_POSTBACK_GET));
@@ -94,14 +101,65 @@ class lopolisc {
});
}
+ getUserData() {
+ return new Promise((resolve, reject)=>{
+ $.ajax({
+ xhrFields: {
+ withCredentials: true
+ },
+ crossDomain: true,
+ url: LOPOLIS_URL+"?MeniID=2",
+ cache: false,
+ type: "GET",
+ dataType: "html",
+ success: (data) => {
+ if (data.includes("Dostop ni dovoljen")) {
+ // console.log(data);
+ resolve(false);
+ return;
+ }
+ let parser = new DOMParser();
+ let p = parser.parseFromString(data, "text/html");
+ let uporabnik = {
+ u: p.getElementsByClassName("obrazecPovdarjen")[0].innerText.trim(),
+ n: p.getElementsByClassName("obrazecPovdarjen")[1].innerText.trim(),
+ e: p.getElementById("Email").value
+ }
+ resolve(uporabnik);
+ },
+ error: () => {
+ reject(new Error(LOPOLISC_ERR_NET));
+ }
+ });
+ });
+ }
+
+ logout() { // you can get pretty race conditiony if you use this wrong! // nah
+ return new Promise((resolve, reject)=>{
+ this.postback(LOPOLIS_URL + "Uporab/Prijava", {}, null, false).then((response) => { // če je true, bo URL, če je false, bo action
+ resolve(true); // don't bother checking cookies...
+ });
+ });
+ }
+
login(usernameToLogin, passwordToLogin) {
- return new Promise((resolve, reject) => {
+ return new Promise(async function(resolve, reject) {
+ let l = new lopolisc();
+ var uporabnik = await l.getUserData();
+ if (uporabnik != false) {
+ if (uporabnik.u = usernameToLogin) {
+ resolve(true);
+ return;
+ } else {
+ await this.logout();
+ }
+ }
var dataToSend = {
"Uporabnik": usernameToLogin,
"Geslo": passwordToLogin,
"OsveziURL": "https://pornhub.com/\"; lopolis=\"boljsi od easistenta",
};
- this.postback(LOPOLIS_URL + "Uporab/Prijava", dataToSend, null, true).then((response) => { // če je true, bo URL, če je false, bo action
+ l.postback(LOPOLIS_URL + "Uporab/Prijava", dataToSend, null, true).then((response) => { // če je true, bo URL, če je false, bo action
let parser = new DOMParser();
let parsed = parser.parseFromString(response.data, "text/html");
if (parsed.getElementById("divPrijavaOsvezi") != null) {
@@ -130,7 +188,7 @@ class lopolisc {
getElementsByTagName("tr")) {
let date_idx = element.getElementsByTagName("input")[2].value;
checkouts[date_idx] = {
- checked: element.getElementsByTagName("input")[0].checked,
+ checked/*out*/: element.getElementsByTagName("input")[0].checked,
readonly: element.getElementsByTagName("input")[0].disabled,
// spodaj spremenljivke, ki so potrebne za submit (ne-API)
index: Number(getStringBetween( // string, start, end
@@ -159,7 +217,53 @@ class lopolisc {
});
}
+ fetchAllMeals(koliko = 3) { // "vsi" pomeni nas. n mes. (vklj. s tem me.)
+ return new Promise (async function(resolve, reject) {
+ let date = new Date();
+ let podatki = {};
+ while (koliko-- > 0) {
+ let l = new lopolisc(); // this zajebava, sorry; seja je itak na
+ let resp = await l.fetchMeals(date); // browserju, ne na objectu.
+ podatki = {...podatki, ...resp};
+ date.setMonth(date.getMonth()+1); // ja, popravi se letnica!
+ }
+ resolve(podatki);
+ });
+ }
+
+
+ fetchAllCheckouts(koliko = 3) { // "vsi" pomeni nas. n mes. (vklj. s tem me.)
+ return new Promise (async function(resolve, reject) {
+ let date = new Date();
+ let podatki = {};
+ while (koliko-- > 0) {
+ let l = new lopolisc(); // this zajebava, sorry; seja je itak na
+ let resp = await l.fetchCheckouts(date); // browserju, ne na objectu.
+ podatki = {...podatki, ...resp};
+ date.setMonth(date.getMonth()+1); // ja, popravi se letnica!
+ }
+ resolve(podatki);
+ });
+ }
+
setCheckouts(odjava_objects) {
+ let odjava_objects_sorted = {};
+ for (const [odjava_da, odjava_ob] of Object.entries(odjava_objects)) {
+ let yearmonth_combo = odjava_da.substring(0,7);
+ if (odjava_objects_sorted[yearmonth_combo] == undefined) {
+ odjava_objects_sorted[yearmonth_combo] = {};
+ }
+ odjava_objects_sorted[yearmonth_combo][odjava_da] = odjava_ob;
+ }
+ if (Object.entries(odjava_objects_sorted).length < 1) {
+ return false;
+ } else if (Object.entries(odjava_objects_sorted).length > 1) {
+ var response;
+ for (const [ym_combo, odj_ob] of Object.entries(odjava_objects_sorted)) {
+ response = this.setCheckouts(odj_ob);
+ }
+ return response; // napake so itak exceptioni, promisov ne potrebujemo!
+ } // else: samo en mesec podatkov imamo, let's go!
return new Promise((resolve, reject) => {
var dataToSend = { "Ukaz": "Shrani" };
for (const [odjava_da, odjava_object] of Object.entries(odjava_objects)) {
@@ -183,7 +287,7 @@ class lopolisc {
});
}
- fetchMeals(date_object = null) { // todo: fetchAllMeals(): naslednja 2 meseca
+ fetchMeals(date_object = null, retried = 3) { // retried je interni parameter
if (date_object == null) {
date_object = new Date();
}
@@ -195,7 +299,7 @@ class lopolisc {
"API-METODA": "fetchMeals",
"MesecModel.Leto": String(date_object.getFullYear())
}
- this.postback(LOPOLIS_URL+"Prehrana/Prednarocanje",dataToSend,null,true).
+ this.postback(LOPOLIS_URL+"?MeniID=78",dataToSend,"form1",false).
then((response) => {
let parser = new DOMParser();
let parsed = parser.parseFromString(response.data, "text/html");
@@ -253,11 +357,35 @@ class lopolisc {
String(element.getElementsByTagName("input")[2].value); // readonly
}
resolve(meals);
+ }).catch((err)=>{
+ if (retried <= 0) {
+ reject(new Error(LOPOLISC_ERR_OUT_OF_RETRIES));
+ } else {
+ resolve(this.fetchMeals(date_object, retried-1)); // retry
+ }
});
});
}
setMeals(meal_objects) {
+ let meal_objects_sorted = {};
+ for (const [meal_da, meal_ob] of Object.entries(meal_objects)) {
+ let yearmonth_combo = meal_da.substring(0,7);
+ if (meal_objects_sorted[yearmonth_combo] == undefined) {
+ meal_objects_sorted[yearmonth_combo] = {};
+ }
+ meal_objects_sorted[yearmonth_combo][meal_da] = meal_ob;
+ }
+ if (Object.entries(meal_objects_sorted).length < 1) { // ni podatkov sploh
+ return false;
+ } else if (Object.entries(meal_objects_sorted).length > 1) {
+ var response;
+ for (const [ym_combo, meal_ob] of Object.entries(meal_objects_sorted)) {
+ response = this.setMeals(meal_ob);
+ }
+ return response; // itak ne uporabljamo response ampak try{}catch{} except
+ } // else: samo en mesec podatkov imamo, let's go!
+
return new Promise((resolve, reject) => {
var dataToSend = { "Ukaz": "Shrani" };
for (const [meal_date, meal_object] of Object.entries(meal_objects)) {
@@ -290,6 +418,15 @@ class lopolisc {
});
});
}
+
+ chooseMenu(meal_object, meal_index) {
+ for (const menu_option of meal_object.menu_options) {
+ menu_option.selected = false;
+ }
+ meal_object.menu_options[meal_index].selected = true;
+ return;
+ }
+
}
// Edited with \ / o _ _ this script is I /\/\ 2020