Trondheim folkebibliotek

Getting overview of due loan dates for loaned books at the library.

Use this homeyscript, and replace the username and pin code:

let user = 'N001234567'
let pin = '1234'
const response = await fetch("https://trhbib1.bib.no/cgi-bin/oauthlogin", {
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language": "nb-NO,nb;q=0.9,no;q=0.8,nn;q=0.7,en-US;q=0.6,en;q=0.5",
"cache-control": "max-age=0",
"content-type": "application/x-www-form-urlencoded",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1"
},
redirect: 'manual',
"referrer": "https://trhbib1.bib.no/cgi-bin/oauthlogin?response_type=token&client_id=56d09e37acec1895f6fdeffb29a628aff8814410a04aec2fc58f5d32cd9256f9&redirect_uri=https://biblioteket.trondheim.kommune.no/mine-sider/mine-lan-og-reservasjoner/",
"referrerPolicy": "no-referrer-when-downgrade",
"body": "username=" + user + "&password=" + pin + "&client_id=56d09e37acec1895f6fdeffb29a628aff8814410a04aec2fc58f5d32cd9256f9&redirect_uri=https%3A%2F%2Fbiblioteket.trondheim.kommune.no%2Fmine-sider%2Fmine-lan-og-reservasjoner%2F&response_type=token&type=sjekkinnlogging",
"method": "POST",
"mode": "cors"
});
let rawcookies = response.headers.raw()['set-cookie']
let cookie = _.map(rawcookies, (x) => {
let rawd = x.split(';')
return rawd[0]
}).join('; ')
const loanresponse = await fetch("https://trhbib1.bib.no/cgi-bin/rest_service/oawebapi_getLoans/1.0/data", {
"headers": {
"accept": "*/*",
"accept-language": "nb-NO,nb;q=0.9,no;q=0.8,nn;q=0.7,en-US;q=0.6,en;q=0.5",
"authorization": "Bearer null",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
"cookie": cookie
},
"referrer": "https://biblioteket.trondheim.kommune.no/mine-sider/mine-lan-og-reservasjoner/",
"referrerPolicy": "no-referrer-when-downgrade",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "include"
});
let loanjson = await loanresponse.json()
let l = loanjson.loans
let today = new Date()
let daysFromNow = (x) => {
let diff = Math.floor((x.getTime() - today.getTime()) / (1000*60*60*24));
return diff
}
let edc = (x) => {
let y = _.unescape(x)
return y.replace(/ø/g, 'ø')
.replace(/å/g, 'å')
.replace(/æ/g, 'æ')
.replace(/Ø/g, 'Ø')
.replace(/Å/g, 'Å')
.replace(/Æ/g, 'Æ')
}
let firstDue = 999
let summary = {}
_.each(l, (x) => {
let ld = new Date(x.dueDate)
let days = daysFromNow(ld)
if (!summary.hasOwnProperty(days)) {
summary[days] = {
"days": days,
"count": 0,
"titles": [],
"renew": true
}
}
if (days < firstDue) {
firstDue = days
}
summary[days].count++
summary[days].titles.push(edc(x.title))
if (!x.loanIsRenewable || x.renewedToday) {
summary[days].renew = false
}
// console.log("dato [" + x.loanDate + "] " + daysFromNow(ld))
})
let st = _.map(Object.values(summary), (x) => {
return x.count + ' bøker må leveres innnen ' + x.days + ' dager. ' +
(x.renew ? 'Kan forlenges. ' : 'Kan IKKE forlenges. ') + '(' + x.titles.join(', ') + ')'
}).join("-- \n")
// console.log("Set cookie", cookie)
// console.log(st)
// console.log(summary)
// console.log("Mine lån")
// console.log(loanjson.loans)
await setTagValue("bibtext", {type: 'string', title:'Bibliotek tekst'}, st)
await setTagValue("bibdue", {type: 'number', title:'Bibliotek dager igjen'}, firstDue)
return true

Then use the tags to send notifications on mobile og telegram, and / or set logic variables depending on preferences.

The genereated bibtext value has a format like this:

9 bøker må leveres innnen 21 dager. Kan forlenges. (Pyse, Ali ser rødt, Kom rundt omkring, Hvor bor dyrene?, Kryptalportalen, Vikinger på tokt, Ann, Max og Kim får ål, Snørrunger, Flink drage) 3 bøker må leveres innnen 56 dager. Kan IKKE forlenges. (En hemmelig venn, Et monster i mattetimen, Vi lager fest!)