Skip to content

Instantly share code, notes, and snippets.

@dayAlone
Last active July 15, 2016 02:21
Show Gist options
  • Save dayAlone/2282c60c404d838693aa35f133f332b6 to your computer and use it in GitHub Desktop.
Save dayAlone/2282c60c404d838693aa35f133f332b6 to your computer and use it in GitHub Desktop.
//https://www.headspace.com/ajax?resource=/pack/all
const co = require('co')
const parallel = require('co-parallel')
const download = require('download')
const fetch = require('node-fetch')
const fs = require('fs')
const token = 'DJt2PcGT_vh7UAiX_1KeCxsPXhF7Aj79yc2fFdAoL1k'
const site = 'https://www.headspace.com/ajax?resource='
const headers = {
'Cookie': 'PHPSESSID=production-www-02~q1ht6ktvn16l2jhsmcp28a6h36;',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
}
const exceptPackages = ['Foundation']
const exceptCourses = []
const getAllPacks = function * () {
return yield fetch(`${site}/packgroup/all`, {
method: 'POST',
headers,
body: `ajax_csrf_token=${token}&publishStates=PUBLISHED,PREVIEW`
})
.then(res => res.json())
.then(json => {
let packs = []
try {
json.data.response.packgroups.packgroup
.filter(el => {
console.log(el)
return exceptPackages.indexOf(el.name) === -1
})
.map(el => (packs = packs.concat(el.packs.filter(el => exceptCourses.indexOf(el.name.toLocaleLowerCase()) === -1).map(el => el.packId))))
return packs
} catch (e) {
console.log(json)
}
})
}
const getPackById = function * (packId) {
return yield fetch(`${site}/pack/get`, {
method: 'POST',
headers,
body: `publishStates=PUBLISHED,PREVIEW&packId=${packId}&ajax_csrf_token=${token}`
})
.then(res => res.json())
.then(json => {
return json.data.response.pack
})
}
const getCurrentPack = function * () {
return yield fetch(`${site}/journey/currentpack`, {
method: 'POST',
headers,
body: `ajax_csrf_token=${token}`
})
.then(res => res.json())
}
const completeLesson = function * (packId, journeySessionId, duration) {
console.log(`completeLesson: ${packId}, ${journeySessionId}, ${duration}`)
try {
yield fetch(`${site}/journey/completesession`, {
method: 'POST',
headers,
body: `packId=${packId}&journeySessionId=${journeySessionId}&duration=${duration}&ajax_csrf_token=${token}`
})
.then(res => res.json())
.then(json => {
console.log(`Complete — ${json.data.success}`)
})
} catch (e) {
console.error(e)
}
}
const downloadLesson = function * (sessionId, duration, day, course) {
const ext = 'mp4'
const filename = `${course}-day-${day}.${ext}`
const folder = `./${course}/`
const subfolder = `${folder}${duration}min/`
const dist = `${subfolder}${filename}`
if (!fs.existsSync(dist)) {
console.log(`${filename} — start`)
const url = yield fetch(`${site}/session/get`, {
method: 'POST',
headers,
body: `sessionId=${sessionId}&duration=${duration}&locale=en_GB&ajax_csrf_token=${token}`
})
.then(res => res.json())
.then(json => {
try {
if (json.data.success && json.data.response.session.content && json.data.response.session.content.length > 0) {
const file = json.data.response.session.content.filter(el => el.filename.indexOf(ext) !== -1)
console.log(json.data.response.session.content.map(el => el.type))
if (file.length > 0) {
const { url: { url } } = file[0]
return decodeURIComponent(url)
}
}
} catch (e) {
console.log(json.data)
}
return false
})
if (url) {
if (!fs.existsSync(folder)) fs.mkdirSync(folder)
if (!fs.existsSync(subfolder)) fs.mkdirSync(subfolder)
if (!fs.existsSync(dist)) {
yield download(url).then(data => {
fs.writeFileSync(dist, data);
})
console.log(`${filename} — downloaded${'\n'}`)
}
}
}
}
co(function *() {
const packs = yield getAllPacks()
for (let p = 0; p < packs.length; p++) {
const { name, packGroup: { name: group }, packId, sessions } = yield getPackById(packs[p])
course = `${group.replace('Headspace ', '')} ${name}`.trim().split(' ').join('-').toLocaleLowerCase()
for (let s = 0; s < sessions.length; s++) {
const { sessionId, durations } = sessions[s]
//const files = durations.filter(d => d.duration > 0).map(d => downloadLesson(sessionId, d.duration, s + 1, course))
yield downloadLesson(sessionId, durations[0].duration, s + 1, course)
//yield parallel(files, durations.length)
}
}
}).catch(e => console.error(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment