refactored the code
This commit is contained in:
parent
017a0ba242
commit
dd2780f858
|
@ -0,0 +1,90 @@
|
||||||
|
const nodeFetch = require('node-fetch')
|
||||||
|
const fetch = require('fetch-cookie/node-fetch')(nodeFetch)
|
||||||
|
const moment = require('moment')
|
||||||
|
const camel = require('camelcase-keys')
|
||||||
|
const h2m = require('h2m')
|
||||||
|
const urls = require('./lib/urls')
|
||||||
|
|
||||||
|
const fetchJson = (url) => {
|
||||||
|
return fetch(url)
|
||||||
|
.then(res => res.ok ? res : Promise.reject(res.statusText))
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(json => camel(json, { deep: true }))
|
||||||
|
.then(json => json.error ? Promise.reject(json.error) : json.data)
|
||||||
|
}
|
||||||
|
const pause = ms => new Promise((resolve) => setTimeout(resolve, ms))
|
||||||
|
|
||||||
|
const getCookie = () => fetch(urls.children)
|
||||||
|
.then(res => res.headers.get('set-cookie'))
|
||||||
|
|
||||||
|
const login = async (socialSecurityNumber) => {
|
||||||
|
const url = urls.login(socialSecurityNumber)
|
||||||
|
const {token, order} = await fetch(url).then(res => res.ok ? res.json() : Promise.reject(res.statusText))
|
||||||
|
|
||||||
|
console.log('got tokens', {token, order})
|
||||||
|
|
||||||
|
let status = ''
|
||||||
|
let tries = 0
|
||||||
|
|
||||||
|
while(tries++ < 60 && (status !== 'ERROR' && status !== 'OK')) {
|
||||||
|
status = await loginStatus(order)
|
||||||
|
console.log('status', status)
|
||||||
|
if (status !== 'OK') await pause(1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status === 'OK'){
|
||||||
|
// update cookies
|
||||||
|
await fetch(urls.loginTarget).then(res => res.text()).then(text => console.log('loginTarget', text))
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const loginStatus = (order) => fetch(urls.checkStatus(order))
|
||||||
|
.then(async res => {
|
||||||
|
if (!res.ok) return Promise.reject(res.statusText)
|
||||||
|
const status = await res.text()
|
||||||
|
return status
|
||||||
|
})
|
||||||
|
|
||||||
|
const getChildren = () => fetchJson(urls.children)
|
||||||
|
const getNews = childId => fetchJson(urls.news(childId))
|
||||||
|
.then(news => news.NewsItems.map(({body,preamble: intro, header, bannerImageUrl: imageUrl, pubDateSE: published, modDateSE: modified}) =>
|
||||||
|
({header, intro, body: h2m(body), modified, published, image: urls.image(imageUrl) })))
|
||||||
|
.catch(err => ({err}))
|
||||||
|
|
||||||
|
const getCalendar = childId => fetchJson(urls.calendar(childId)).catch(err => ({err}))
|
||||||
|
const getNotifications = childId => fetchJson(urls.notifications(childId)).catch(err => ({err}))
|
||||||
|
const getMenu = childId => fetchJson(urls.menu(childId)).catch(err => ({err}))
|
||||||
|
const getSchedule = childId => fetchJson(urls.schedule(childId, moment().startOf('day').toISOString(), moment().endOf('day').toISOString()))
|
||||||
|
|
||||||
|
|
||||||
|
const getAll = async () => {
|
||||||
|
const children = await getChildren()
|
||||||
|
const data = await Promise.all(children.map(async (child) => {
|
||||||
|
|
||||||
|
const childId = child.id
|
||||||
|
const [news, calendar, notifications, menu, schedule] = await Promise.all([
|
||||||
|
getNews(childId),
|
||||||
|
getCalendar(childId),
|
||||||
|
getNotifications(childId),
|
||||||
|
getMenu(childId),
|
||||||
|
getSchedule(childId)])
|
||||||
|
|
||||||
|
return {child, news, calendar, notifications, menu, schedule}
|
||||||
|
}))
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
login,
|
||||||
|
getChildren,
|
||||||
|
getNews,
|
||||||
|
getCalendar,
|
||||||
|
getNotifications,
|
||||||
|
getMenu,
|
||||||
|
getSchedule,
|
||||||
|
getAll
|
||||||
|
}
|
|
@ -1,100 +1,15 @@
|
||||||
const nodeFetch = require('node-fetch')
|
import OpenAPIBackend from 'openapi-backend';
|
||||||
const fetch = require('fetch-cookie/node-fetch')(nodeFetch)
|
|
||||||
const moment = require('moment')
|
|
||||||
|
|
||||||
const baseUrl = 'https://etjanst.stockholm.se'
|
// create api with your definition file or object
|
||||||
const urls = {
|
const api = new OpenAPIBackend({ definition: './petstore.yml' });
|
||||||
login: socialSecurityNumber => `https://login003.stockholm.se/NECSadcmbid/authenticate/NECSadcmbid?TARGET=-SM-HTTPS%3a%2f%2flogin001%2estockholm%2ese%2fNECSadc%2fmbid%2fb64startpage%2ejsp%3fstartpage%3daHR0cHM6Ly9ldGphbnN0LnN0b2NraG9sbS5zZS92YXJkbmFkc2hhdmFyZS9pbmxvZ2dhZDIvaGVt&initialize=bankid&personalNumber=${socialSecurityNumber}&_=${Date.now()}`,
|
|
||||||
checkStatus: order => `https://login003.stockholm.se/NECSadcmbid/authenticate/NECSadcmbid?TARGET=-SM-HTTPS%3a%2f%2flogin001%2estockholm%2ese%2fNECSadc%2fmbid%2fb64startpage%2ejsp%3fstartpage%3daHR0cHM6Ly9ldGphbnN0LnN0b2NraG9sbS5zZS92YXJkbmFkc2hhdmFyZS9pbmxvZ2dhZDIvaGVt&verifyorder=${order}&_=${Date.now()}`,
|
|
||||||
//loginTarget: `https://login001.stockholm.se/NECSadc/mbid/b64startpage.jsp?startpage=aHR0cHM6Ly9ldGphbnN0LnN0b2NraG9sbS5zZS92YXJkbmFkc2hhdmFyZS9pbmxvZ2dhZDIvaGVt`,
|
|
||||||
loginTarget: 'https://login003.stockholm.se/NECSadcmbid/authenticate/SiteMinderAuthADC?TYPE=33554433&REALMOID=06-42f40edd-0c5b-4dbc-b714-1be1e907f2de&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=IfNE0iMOtzq2TcxFADHylR6rkmFtwzoxRKh5nRMO9NBqIxHrc38jFyt56FASdxk1&TARGET=-SM-HTTPS%3a%2f%2flogin001%2estockholm%2ese%2fNECSadc%2fmbid%2fb64startpage%2ejsp%3fstartpage%3daHR0cHM6Ly9ldGphbnN0LnN0b2NraG9sbS5zZS92YXJkbmFkc2hhdmFyZS9pbmxvZ2dhZDIvR2V0Q2hpbGRyZW4%3d',
|
|
||||||
children: `${baseUrl}/vardnadshavare/inloggad2/GetChildren`,
|
|
||||||
calendar: childId => `${baseUrl}/vardnadshavare/inloggad2/Calender/GetSchoolCalender?childId=${childId}&rowLimit=50`,
|
|
||||||
user: `${baseUrl}/vardnadshavare/base/getuserdata`,
|
|
||||||
news: childId => `${baseUrl}/vardnadshavare/inloggad2/News/GetNewsOverview?childId=${childId}`,
|
|
||||||
image: url => `${baseUrl}/vardnadshavare/inloggad2/NewsBanner?url=${url}`,
|
|
||||||
notifications: childId => `${baseUrl}/vardnadshavare/inloggad2/Overview/GetNotification?childId=${childId}`,
|
|
||||||
menu: childId => `${baseUrl}/vardnadshavare/inloggad2/Matsedel/GetMatsedelChoice?childId=${childId}`,
|
|
||||||
schedule: (childId, fromDate, endDate) => `${baseUrl}/vardnadshavare/inloggad2/Calender/GetSchema?childId=${childId}&startDate=${fromDate}&endDate=${endDate}`
|
|
||||||
|
|
||||||
}
|
// register your framework specific request handlers here
|
||||||
|
api.register({
|
||||||
|
getPets: (c, req, res) => res.status(200).json({ result: 'ok' }),
|
||||||
|
getPetById: (c, req, res) => res.status(200).json({ result: 'ok' }),
|
||||||
|
validationFail: (c, req, res) => res.status(400).json({ err: c.validation.errors }),
|
||||||
|
notFound: (c, req, res) => res.status(404).json({ err: 'not found' }),
|
||||||
|
});
|
||||||
|
|
||||||
const fetchJson = (url) => {
|
// initalize the backend
|
||||||
return fetch(url)
|
api.init();
|
||||||
.then(res => {
|
|
||||||
return res.ok ? res : Promise.reject(res.statusText)
|
|
||||||
})
|
|
||||||
.then(res => res.json())
|
|
||||||
.then(json => json.Error ? Promise.reject(json.Error) : json.Data)
|
|
||||||
}
|
|
||||||
const pause = ms => new Promise((resolve) => setTimeout(resolve, ms))
|
|
||||||
|
|
||||||
const getCookie = () => fetch(urls.children)
|
|
||||||
.then(res => res.headers.get('set-cookie'))
|
|
||||||
|
|
||||||
const login = async (socialSecurityNumber) => {
|
|
||||||
const url = urls.login(socialSecurityNumber)
|
|
||||||
const {token, order} = await fetch(url).then(res => res.ok ? res.json() : Promise.reject(res.statusText))
|
|
||||||
|
|
||||||
console.log('got tokens', {token, order})
|
|
||||||
|
|
||||||
let status = ''
|
|
||||||
let tries = 0
|
|
||||||
|
|
||||||
while(tries++ < 60 && (status !== 'ERROR' && status !== 'OK')) {
|
|
||||||
status = await loginStatus(order)
|
|
||||||
console.log('status', status)
|
|
||||||
if (status !== 'OK') await pause(1000)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (status === 'OK'){
|
|
||||||
// update cookies
|
|
||||||
await fetch(urls.loginTarget).then(res => res.text()).then(text => console.log('loginTarget', text))
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const loginStatus = (order) => fetch(urls.checkStatus(order))
|
|
||||||
.then(async res => {
|
|
||||||
if (!res.ok) return Promise.reject(res.statusText)
|
|
||||||
const status = await res.text()
|
|
||||||
/*if (status === 'ERROR') {
|
|
||||||
console.error(res.headers)
|
|
||||||
}*/
|
|
||||||
return status
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
const getData = async () => {
|
|
||||||
|
|
||||||
const children = await fetchJson(urls.children)
|
|
||||||
const data = await Promise.all(children.map(async (child) => {
|
|
||||||
const childId = child.Id
|
|
||||||
const news = fetchJson(urls.news(childId)).then(news => news.NewsItems.map(({Body: body, Preamble: intro, Header: header, BannerImageUrl: image, PubDateSE: published, ModDateSE: modified}) => ({header, intro, body, modified, published, image: urls.image(image) }))).catch(err => ({err}))
|
|
||||||
const calendar = fetchJson(urls.calendar(childId)).catch(err => ({err}))
|
|
||||||
const notifications = fetchJson(urls.notifications(childId)).catch(err => ({err}))
|
|
||||||
const menu = fetchJson(urls.menu(childId)).catch(err => ({err}))
|
|
||||||
const schedule = fetchJson(urls.schedule(childId, moment().startOf('day').toISOString(), moment().endOf('day').toISOString()))
|
|
||||||
return Promise.all([news, calendar, notifications, menu, schedule]).then(([news, calendar, notifications, menu, schedule]) => ({child, news, calendar, notifications, menu, schedule}))
|
|
||||||
}))
|
|
||||||
return data
|
|
||||||
/*
|
|
||||||
|
|
||||||
const news = fetchJson(urls().children)
|
|
||||||
.then(children => Promise.all()))
|
|
||||||
.then(text => console.log('text', text))
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
const run = async (socialSecurityNumber) => {
|
|
||||||
const OK = await login(socialSecurityNumber)
|
|
||||||
if (!OK) return Promise.reject('Login failed')
|
|
||||||
const data = await getData()
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
run('197612040233')
|
|
||||||
.then(data => console.log('data', JSON.stringify(data, null, 2)))
|
|
||||||
.catch(err => console.error(err))
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
const baseUrl = 'https://etjanst.stockholm.se'
|
||||||
|
const urls = {
|
||||||
|
login: socialSecurityNumber => `https://login003.stockholm.se/NECSadcmbid/authenticate/NECSadcmbid?TARGET=-SM-HTTPS%3a%2f%2flogin001%2estockholm%2ese%2fNECSadc%2fmbid%2fb64startpage%2ejsp%3fstartpage%3daHR0cHM6Ly9ldGphbnN0LnN0b2NraG9sbS5zZS92YXJkbmFkc2hhdmFyZS9pbmxvZ2dhZDIvaGVt&initialize=bankid&personalNumber=${socialSecurityNumber}&_=${Date.now()}`,
|
||||||
|
checkStatus: order => `https://login003.stockholm.se/NECSadcmbid/authenticate/NECSadcmbid?TARGET=-SM-HTTPS%3a%2f%2flogin001%2estockholm%2ese%2fNECSadc%2fmbid%2fb64startpage%2ejsp%3fstartpage%3daHR0cHM6Ly9ldGphbnN0LnN0b2NraG9sbS5zZS92YXJkbmFkc2hhdmFyZS9pbmxvZ2dhZDIvaGVt&verifyorder=${order}&_=${Date.now()}`,
|
||||||
|
//loginTarget: `https://login001.stockholm.se/NECSadc/mbid/b64startpage.jsp?startpage=aHR0cHM6Ly9ldGphbnN0LnN0b2NraG9sbS5zZS92YXJkbmFkc2hhdmFyZS9pbmxvZ2dhZDIvaGVt`,
|
||||||
|
loginTarget: 'https://login003.stockholm.se/NECSadcmbid/authenticate/SiteMinderAuthADC?TYPE=33554433&REALMOID=06-42f40edd-0c5b-4dbc-b714-1be1e907f2de&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=IfNE0iMOtzq2TcxFADHylR6rkmFtwzoxRKh5nRMO9NBqIxHrc38jFyt56FASdxk1&TARGET=-SM-HTTPS%3a%2f%2flogin001%2estockholm%2ese%2fNECSadc%2fmbid%2fb64startpage%2ejsp%3fstartpage%3daHR0cHM6Ly9ldGphbnN0LnN0b2NraG9sbS5zZS92YXJkbmFkc2hhdmFyZS9pbmxvZ2dhZDIvR2V0Q2hpbGRyZW4%3d',
|
||||||
|
children: `${baseUrl}/vardnadshavare/inloggad2/GetChildren`,
|
||||||
|
calendar: childId => `${baseUrl}/vardnadshavare/inloggad2/Calender/GetSchoolCalender?childId=${childId}&rowLimit=50`,
|
||||||
|
user: `${baseUrl}/vardnadshavare/base/getuserdata`,
|
||||||
|
news: childId => `${baseUrl}/vardnadshavare/inloggad2/News/GetNewsOverview?childId=${childId}`,
|
||||||
|
image: url => `${baseUrl}/vardnadshavare/inloggad2/NewsBanner?url=${url}`,
|
||||||
|
notifications: childId => `${baseUrl}/vardnadshavare/inloggad2/Overview/GetNotification?childId=${childId}`,
|
||||||
|
menu: childId => `${baseUrl}/vardnadshavare/inloggad2/Matsedel/GetMatsedelChoice?childId=${childId}`,
|
||||||
|
schedule: (childId, fromDate, endDate) => `${baseUrl}/vardnadshavare/inloggad2/Calender/GetSchema?childId=${childId}&startDate=${fromDate}&endDate=${endDate}`
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = urls
|
File diff suppressed because it is too large
Load Diff
|
@ -4,9 +4,12 @@
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"camelcase-keys": "^6.2.2",
|
||||||
"fetch-cookie": "^0.11.0",
|
"fetch-cookie": "^0.11.0",
|
||||||
|
"h2m": "^0.7.0",
|
||||||
"moment": "^2.29.1",
|
"moment": "^2.29.1",
|
||||||
"node-fetch": "^2.6.1"
|
"node-fetch": "^2.6.1",
|
||||||
|
"openapi-backend": "^3.6.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^7.14.0",
|
"eslint": "^7.14.0",
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
const backend = require('./backend')
|
||||||
|
|
||||||
|
const run = async (socialSecurityNumber) => {
|
||||||
|
const OK = await backend.login(socialSecurityNumber)
|
||||||
|
if (!OK) return Promise.reject('Login failed')
|
||||||
|
const data = await backend.getAll()
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
run('197612040233')
|
||||||
|
.then(data => console.log('data', JSON.stringify(data, null, 2)))
|
||||||
|
.catch(err => console.error(err))
|
Loading…
Reference in New Issue