Skip to content

Commit

Permalink
fix: 🐛 add missing headers for fetching children
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikHellman committed Mar 2, 2021
1 parent 7642a90 commit 68c99cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
9 changes: 2 additions & 7 deletions lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DateTime } from 'luxon'
import { EventEmitter } from 'events'
import { htmlDecode } from 'js-htmlencode'
import { decode } from 'he'
import * as html from 'node-html-parser'
import {
Expand Down Expand Up @@ -116,16 +115,12 @@ export class Api extends EventEmitter {
return parse.user(data)
}

parseXsrfToken(htmltext: string): string {
const doc = html.parse(decode(htmltext))
return doc.querySelector('input[name="__RequestVerificationToken"]').getAttribute('value') || ''
}

async getChildren(): Promise<Child[]> {
if (this.isFake) return fakeResponse(fake.children())

const hemResponse = await this.fetch('hemPage', routes.hemPage, this.session)
const xsrfToken = this.parseXsrfToken(await hemResponse.text())
const doc = html.parse(decode(await hemResponse.text()))
const xsrfToken = doc.querySelector('input[name="__RequestVerificationToken"]').getAttribute('value') || ''
if (this.session) {
this.session.headers = {
...this.session.headers,
Expand Down
24 changes: 14 additions & 10 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,21 @@ async function run() {
// const schedule = await api.getSchedule(children[0], DateTime.local(), DateTime.local().plus({ week: 1 }))
// console.log(schedule)

console.log('news')
const news = await api.getNews(children[0])

console.log('news details')
const newsItems = await Promise.all(
news.map((newsItem) =>
api.getNewsDetails(children[0], newsItem)
.catch((err) => { console.error(newsItem.id, err) })
if (children.length > 0) {
console.log('news')
const news = await api.getNews(children[0])

console.log('news details')
const newsItems = await Promise.all(
news.map((newsItem) =>
api.getNewsDetails(children[0], newsItem)
.catch((err) => { console.error(newsItem.id, err) })
)
)
)
console.log(newsItems)
console.log(newsItems)
} else {
console.log('No children found!')
}

// console.log('menu')
// const menu = await api.getMenu(children[0])
Expand Down

0 comments on commit 68c99cb

Please sign in to comment.