Last active
June 29, 2020 21:10
-
-
Save maccyber/ac6e767a0f9fdbcc8287604123b245fa to your computer and use it in GitHub Desktop.
Fugly kode som henter eliteserietabellen fra NRK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch') | |
const URL = 'https://sportsresultater.nrk.no/kasparov.api/idretter/501/turneringer/3006/sesonger/199399/konkurranser/314629/tabell' | |
const reformatTable = ({ lag, _embedded: { tabell } }) => ( | |
tabell.map(({ celler }) => { | |
const { tekst: posisjon, indeks: index } = celler.find(({ type }) => type === 'posisjon') | |
const { id } = celler.find(({ type }) => type === 'lookup') | |
const { tekst: formkurve } = celler.find(({ type }) => type === 'formkurve') | |
const { navn, kortnavn, hashtag } = lag[id] | |
return { | |
index, | |
navn, | |
kortnavn, | |
hashtag, | |
posisjon, | |
formkurve: formkurve.split(' ') | |
} | |
}) | |
) | |
const getData = async url => (await fetch(url, { headers: { accept: 'application/json' } })).text() | |
// Remove utf-8 BOM bytes | |
const cleanJson = json => json.charCodeAt(0) === 65279 ? json.substring(1) : json | |
module.exports = async (url = URL) => { | |
const data = await getData(url) | |
const jsonData = JSON.parse(cleanJson(data)) | |
return reformatTable(jsonData) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
const url = 'https://sportsresultater.nrk.no/kasparov.api/idretter/501/turneringer/3006/sesonger/199399/konkurranser/314629/tabell' | |
const eliteserienTabell = require('./eliteserien-tabell-api') | |
console.log(await eliteserienTabell(url)) | |
})() |
Should give you following response:
[ { index: 0,
navn: 'Molde',
kortnavn: 'MFK',
hashtag: '#molde',
posisjon: 1,
formkurve: [ 'hs', 'bs', 'hs', 'bt', 'hs' ] },
{ index: 1,
navn: 'Odd',
kortnavn: 'ODD',
hashtag: '#odd',
posisjon: 2,
formkurve: [ 'bt', 'hs', 'bs', 'hs', 'bs' ] },
{ index: 2,
navn: 'Vålerenga',
kortnavn: 'VIF',
hashtag: '#vålerenga',
posisjon: 3,
formkurve: [ 'bu', 'hs', 'bu', 'bs', 'hs' ] },
{ index: 3,
navn: 'Bodø/Glimt',
kortnavn: 'B/G',
hashtag: '#bodø/glimt',
posisjon: 4,
formkurve: [ 'bt', 'hs', 'bu', 'bs', 'ht' ] },
{ index: 4,
navn: 'Brann',
kortnavn: 'BRA',
hashtag: '#brann',
posisjon: 5,
formkurve: [ 'bs', 'ht', 'bs', 'hs', 'bs' ] },
{ index: 5,
navn: 'Kristiansund BK',
kortnavn: 'KBK',
hashtag: '#kristiansund',
posisjon: 6,
formkurve: [ 'bs', 'hs', 'bs', 'hs', 'bu' ] },
{ index: 6,
navn: 'Viking',
kortnavn: 'VIK',
hashtag: '#viking',
posisjon: 7,
formkurve: [ 'bt', 'hu', 'bu', 'hs', 'bt' ] },
{ index: 7,
navn: 'Haugesund',
kortnavn: 'FKH',
hashtag: '#haugesund',
posisjon: 8,
formkurve: [ 'bt', 'ht', 'hu', 'bs', 'hu' ] },
{ index: 8,
navn: 'Ranheim',
kortnavn: 'RIL',
hashtag: '#ranheim',
posisjon: 9,
formkurve: [ 'hs', 'bs', 'ht', 'bt', 'bs' ] },
{ index: 9,
navn: 'Strømsgodset',
kortnavn: 'SIF',
hashtag: '#strømsgodset',
posisjon: 10,
formkurve: [ 'ht', 'bt', 'hu', 'bt', 'hs' ] },
{ index: 10,
navn: 'Mjøndalen',
kortnavn: 'MIF',
hashtag: '#mjøndalen',
posisjon: 11,
formkurve: [ 'bu', 'hs', 'bt', 'hu', 'bt' ] },
{ index: 11,
navn: 'Rosenborg',
kortnavn: 'RBK',
hashtag: '#rosenborg',
posisjon: 12,
formkurve: [ 'bt', 'hs', 'bu', 'ht', 'hs' ] },
{ index: 12,
navn: 'Lillestrøm',
kortnavn: 'LSK',
hashtag: '#lillestrøm',
posisjon: 13,
formkurve: [ 'hs', 'bt', 'hu', 'bu', 'ht' ] },
{ index: 13,
navn: 'Sarpsborg 08',
kortnavn: 'SA08',
hashtag: '#sarpsborg08',
posisjon: 14,
formkurve: [ 'hu', 'bt', 'ht', 'bt', 'ht' ] },
{ index: 14,
navn: 'Stabæk',
kortnavn: 'STAB',
hashtag: '#stabæk',
posisjon: 15,
formkurve: [ 'bt', 'hs', 'bt', 'ht', 'bt' ] },
{ index: 15,
navn: 'Tromsø',
kortnavn: 'TIL',
hashtag: '#tromsø',
posisjon: 16,
formkurve: [ 'ht', 'bt', 'ht', 'ht', 'bt' ] } ]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try it out: