Last active
September 11, 2019 03:50
-
-
Save alhankeser/33e70bae0e3e188ecdbf7fce02f88ae6 to your computer and use it in GitHub Desktop.
Zwift API Wrapper (node.js)
This file contains 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
var ZwiftConnection = require('zwift-mobile-api'); | |
var zwiftAccount = new ZwiftConnection(process.env.ZWIFT_USERNAME, process.env.ZWIFT_PASSWORD); | |
var axios = require('axios'); | |
var Q = require('q'); | |
try { | |
var world = zwiftAccount.getWorld(1); | |
} | |
catch (err) { | |
res.sendStatus(500); | |
res.end(); | |
} | |
module.exports = function(app) { | |
app.get('/events/:eventId', function(req, res) { | |
zwiftAccount.getAccessToken().then(token => { | |
axios.get('https://us-or-rly101.zwift.com/api/events/' + req.params.eventId, { | |
'headers': { | |
'Authorization': 'Bearer ' + token, | |
'User-Agent': 'Zwift/115 CFNetwork/758.0.2 Darwin/15.0.0', | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
} | |
}).then(response => { | |
res.send(response.data); | |
}).catch(error => { | |
console.log(error); | |
}) | |
}) | |
}); | |
app.get('/riders', function(req, res) { | |
world.riders().then(riders => { | |
res.send(riders); | |
}).catch(error => { | |
console.log(error.response); | |
res.sendStatus(error.response.status); | |
res.end(); | |
}); | |
}); | |
app.get('/riders/:riderId', function(req, res) { | |
zwiftAccount.getProfile(req.params.riderId).profile().then(p => { | |
res.send(p); | |
}).catch(error => { | |
console.log(error.response); | |
res.sendStatus(error.response.status); | |
res.end(); | |
}); | |
}); | |
app.get('/riders/:riderId/status', function(req, res) { | |
world.riderStatus(req.params.riderId).then(status => { | |
res.send(status); | |
}).catch(error => { | |
console.log(error.response); | |
res.sendStatus(error.response.status); | |
res.end(); | |
}); | |
}); | |
app.get('/events/:eventId/live', function(req, res) { | |
var riders = []; | |
var riderStatuses = []; | |
var subgroupPromises = []; | |
var riderPromises = []; | |
var subgroups = [] | |
zwiftAccount.getAccessToken().then(token => { | |
axios.get('https://us-or-rly101.zwift.com/api/events/' + req.params.eventId, { | |
'headers': { | |
'Authorization': 'Bearer ' + token, | |
'User-Agent': 'Zwift/115 CFNetwork/758.0.2 Darwin/15.0.0', | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}, | |
'timeout': 120000 | |
}).then(race => { | |
subgroups = race.data.eventSubgroups; | |
subgroups.forEach(subgroup => { | |
zwiftAccount.getAccessToken().then(token => { | |
subgroupPromises.push( | |
axios.get('https://us-or-rly101.zwift.com/api/events/subgroups/entrants/' + subgroup.id + '?participation=signed_up&type=all&limit=1000000', { | |
'headers': { | |
'Authorization': 'Bearer ' + token, | |
'User-Agent': 'Zwift/115 CFNetwork/758.0.2 Darwin/15.0.0', | |
"Accept-Encoding": "gzip, deflate", | |
"Accept": "application/json", | |
"Connection": "keep-alive", | |
"Accept-Language": "en-us" | |
}, | |
'timeout': 120000 | |
}) | |
); | |
}) | |
}); | |
Q.all(subgroupPromises).then(categories => { | |
categories.forEach(function(categoryEntrants, categoryIndex, categories) { | |
categoryEntrants.data.forEach(function(categoryEntrant, index, categoryEntrants) { | |
categoryEntrants[index]['categoryLabel'] = subgroups[categoryIndex].label; | |
}) | |
riders.push(categoryEntrants.data) | |
}); | |
riders = [].concat.apply([], riders); | |
riders.forEach(rider => { riderPromises.push(world.riderStatus(Number(rider.id))) }); | |
Q.allSettled(riderPromises) | |
.then(statusResults => { | |
statusResults.forEach(status => { | |
if (status.state === 'fulfilled') { riderStatuses.push(status.value)} | |
}); | |
riders.forEach(function(rider, index, riders) { | |
var matchingStatus = false; | |
matchingStatus = riderStatuses.filter(status => status.id === rider.id )[0]; | |
if (matchingStatus) { | |
riders[index] = Object.assign({},matchingStatus.riderStatus, rider) | |
} | |
}); | |
riders.sort(function(a, b) { | |
if ((!a.distance && b.distance) || (a.distance < b.distance)) { | |
return 1; | |
} | |
if ((a.distance && !b.distance) || (a.distance > b.distance)) { | |
return -1; | |
} | |
return Number(b.distance) - Number(a.distance); | |
}); | |
res.send(riders); | |
}); | |
}); | |
}).catch(error => { | |
console.log(error); | |
}) | |
}).catch(error => { | |
console.log(error.response); | |
res.sendStatus(error.response.status); | |
res.end(); | |
}); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment