Created
February 22, 2017 16:33
-
-
Save Peleg/0377c9fb04aa3a6ad8876aa8e4d47ad6 to your computer and use it in GitHub Desktop.
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
const IS_BROWSER = typeof window !== 'undefined'; | |
if (!IS_BROWSER) { | |
require('isomorphic-fetch'); | |
} | |
const COOKIE = 'REPLACE W YOURS'; | |
const API_BASE = 'https://api.equinox.com'; | |
const CLUB_IDS = { | |
flatiron: 102, | |
}; | |
const FAVORITE_BIKES = { | |
door: 47, | |
nearDoor: 46 | |
}; | |
function reserveBike(clubId, classId, bikeNumber) { | |
getBikeByNumber(classId, bikeNumber) | |
.then((bike) => bike.reservableEquipmentId) | |
.then((bikeId) => { | |
return equinoxFetch(`/v2/bookaclass/${classId}/book/${clubId}?bikeId=${bikeId}`, { | |
method: 'PUT' | |
}) | |
}) | |
.then(console.log) | |
.catch((e) => { | |
console.error('\n\n'); | |
console.error(`FAILED TO RESERVER BIKE #${bikeNumber}`); | |
console.error(e, e.stack); | |
console.error('\n\n'); | |
}); | |
} | |
function getBikeByNumber(classId, bikeNumber) { | |
return getAvailableBikes(classId) | |
.then((bikes) => { | |
const bike = bikes.find((bike) => bike.localId === bikeNumber); | |
return bike | |
? bike | |
: Promise.reject(new Error(`failed to find bike by number (#${bikeNumber})`)); | |
}); | |
} | |
function getAvailableBikes(classId) { | |
return equinoxFetch(`/v2/bookaclass/equipments/${classId}?_=${Date.now()}`) | |
.then((json) => json.layout.equipments); | |
} | |
function timeLeftForReservation(classId) { | |
return equinoxFetch(`/v3/classes/reservation-starttimeleft/${classId}`) | |
.then((json) => json.result * 1000) | |
} | |
function equinoxFetch(path, opts) { | |
return fetch(`${API_BASE}${path}`, Object.assign({}, { | |
credentials: 'include', | |
headers: { | |
cookie: COOKIE, | |
'content-type': 'application/json' | |
} | |
}, opts)).then((r) => { | |
if (!r.ok || !String(r.status).startsWith('2')) { | |
return Promise.reject(r); | |
} | |
return r.json(); | |
}); | |
} | |
if (!IS_BROWSER) { | |
module.exports.reserveBike = reserveBike; | |
module.exports.getBikeByNumber = getBikeByNumber; | |
module.exports.getAvailableBikes = getAvailableBikes; | |
module.exports.equinoxFetch = equinoxFetch; | |
module.exports.timeLeftForReservation = timeLeftForReservation; | |
} else { | |
function reserveWhenCan() { | |
const [, clubId] = document.querySelector('ul.category-list a').href.match(/clubs=(\d+)/); | |
const [, classId] = window.location.pathname.match(/\/(\d+?)($|\?)/); | |
const bikeNumber = prompt(`What Bike Number? ${JSON.stringify(FAVORITE_BIKES)}`); | |
timeLeftForReservation(classId) | |
.then((msLeft) => { | |
const hours = (msLeft / 1000 / 60 / 60).toFixed(2); | |
alert(` | |
Cool, I'm gonna book in ${hours} hours: | |
Club: ${clubId} | |
Class: ${classId} | |
Bike: #${bikeNumber} | |
NOTE: MAKE SURE TO LEAVE THIS WINDOW OPEN | |
`); | |
setTimeout(() => { | |
reserveBike(clubId, classId, bikeNumber); | |
}, msLeft + 10); | |
window.onbeforeunload = () => { | |
return ` | |
I'm scheduled to book you a bike in ${hours} hours. | |
If you close this window, I won't! | |
`; | |
}; | |
}) | |
.catch(console.error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
amazing