Last active
February 5, 2016 19:04
-
-
Save mallendeo/69b7f18f9544d5372eb2 to your computer and use it in GitHub Desktop.
Grabar canales chilenos con node
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
{ | |
"main": "recorder.js", | |
"dependencies": { | |
"fs-extra": "^0.24.0", | |
"request": "^2.61.0" | |
} | |
} |
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
'use strict' | |
const request = require('request') | |
const fs = require('fs-extra') | |
const parseUrl = require('url').parse | |
const C13_URL = 'http://13.live.cdn.cl/13hddesktop/13hd-1250.m3u8' | |
const CHV_URL = 'http://live.hls.http.chv.ztreaming.com/chvhddesktop/chvHi.m3u8' | |
const TVN_URL = 'http://edge-11-mvstr.edge.mdstrm.com/tvn-live/_definst_/smil:7cb6048ff45cb5cdf7f5fed828597fd1/chunklist_b1500000.m3u8' | |
const MEGA_URL = 'http://edge-11-mvstr.edge.mdstrm.com/live/smil:db6a38a0a2bffd96d01965c4d975b9fb/playlist_b1500.m3u8' | |
function getFilename(url) { | |
const matches = url.match(/(?=[\w\d-]+\.\w{2,4}$).+/i) | |
return matches ? matches[0] : null | |
} | |
function getPlaylist(url) { | |
return new Promise((resolve, reject) => { | |
let args = '' | |
if (url.match(/edge\.mdstrm\.com/i)) { | |
args += '?time=' + new Date().getTime() | |
} | |
request(url + args, (err, response) => { | |
if (err) return reject(err) | |
let urls = (response.body + '').match(/(.+?)\.ts/ig) | |
// ensure filenames | |
urls = urls.map(getFilename) | |
resolve(urls) | |
}) | |
}) | |
} | |
function downloadChunk(url, path, filename) { | |
return new Promise((resolve, reject) => { | |
const filepath = './recordings/' + path + '/' + filename | |
let info = { | |
saved: true, | |
exists: false, | |
filename: filename | |
} | |
// check if file exists | |
fs.stat(filepath, (err, stat) => { | |
if (!err) { | |
info.exists = true | |
resolve(info) | |
} else if (err.code == 'ENOENT') { | |
fs.ensureFile(filepath, err => { | |
request(url) | |
.pipe(fs.createWriteStream(filepath)) | |
.on('finish', _ => resolve(info)) | |
}) | |
} | |
}) | |
}) | |
} | |
function downloadStream(pUrl, path) { | |
let url = pUrl.replace(getFilename(pUrl), '') | |
return getPlaylist(pUrl).then(chunks => { | |
let promises = chunks.map(chunk => downloadChunk(url + chunk, path, chunk)) | |
return Promise.all(promises) | |
}).then(data => { | |
console.log('data', data) | |
setTimeout(_ => downloadStream(pUrl, path), 10 * 1000) | |
}) | |
} | |
downloadStream(CHV_URL, 'chv') | |
downloadStream(C13_URL, 'c13') | |
downloadStream(TVN_URL, 'tvn') | |
downloadStream(MEGA_URL, 'mega') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment