Skip to content

Instantly share code, notes, and snippets.

@joegrice
Created June 6, 2019 08:29
Show Gist options
  • Save joegrice/260930376ecfc6f408af104f71b127e7 to your computer and use it in GitHub Desktop.
Save joegrice/260930376ecfc6f408af104f71b127e7 to your computer and use it in GitHub Desktop.
async fetchBusPredictions() {
const response = await fetch('http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?StopCode1=74804&DirectionID=1&VisitNumber=1&ReturnList=StopCode1,StopPointName,LineName,DestinationText,EstimatedTime,MessageUUID,MessageText,MessagePriority,MessageType,ExpireTime');
const reader = response.body.getReader().read().then(({ value }) => {
let string = new TextDecoder('utf-8').decode(value);
let split = string.split(']');
// Remove blank at end
split.pop();
// Remove unneeded first array
split.splice(0, 1);
let count = 0;
let jsonRes = '{';
for (let i = 0; i < split.length; i++) {
let inS = split[i].split('[');
split[i] = '"' + count + '": [' + inS[1];
split[i] = split[i] + '],';
count++;
jsonRes += split[i];
}
// Remove trailling comma
jsonRes = jsonRes.substring(0, jsonRes.length - 1);
jsonRes += '}';
return JSON.parse(jsonRes);
});
let json = await reader;
return json;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment