Skip to content

Instantly share code, notes, and snippets.

@christoph-jerolimov
Created September 20, 2012 16:09
Show Gist options
  • Save christoph-jerolimov/3756821 to your computer and use it in GitHub Desktop.
Save christoph-jerolimov/3756821 to your computer and use it in GitHub Desktop.
// javascript reimplementation of
// https://github.com/hellozimi/HCYoutubeParser/blob/master/YoutubeParser/Classes/HCYoutubeParser.m
var url = require('url');
var http = require('http');
var videoId = '7qpvjsMaXPE';
var infoUrl = 'http://www.youtube.com/get_video_info?video_id=' + videoId;
var queryStringMap = function(data) {
var result = {};
data.split('&').forEach(function(entry) {
result[
decodeURIComponent(entry.substring(0, entry.indexOf('=')))] =
decodeURIComponent(entry.substring(entry.indexOf('=') + 1));
});
return result;
};
var listOfQueryStringMaps = function(data) {
var result = [];
data.split(',').forEach(function(entry) {
result.push(queryStringMap(entry));
});
return result;
};
var req = http.request(url.parse(infoUrl), function(res) {
if (res.statusCode != 200) {
console.log('Status code not 200: ' + res.statusCode);
process.exit(1);
}
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
data = queryStringMap(data);
if (typeof data['url_encoded_fmt_stream_map'] == 'string') {
data['url_encoded_fmt_stream_map'] = listOfQueryStringMaps(data['url_encoded_fmt_stream_map']);
}
// console.log(JSON.stringify(data.url_encoded_fmt_stream_map, null, '\t'));
data.url_encoded_fmt_stream_map.forEach(function(videoEntry) {
//if (videoEntry.quality != 'medium') return;
//if (videoEntry.type.indexOf('mp4') == -1) return;
console.log(JSON.stringify(videoEntry, null, '\t'));
});
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
@Zibri
Copy link

Zibri commented Jul 21, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment