Skip to content

Instantly share code, notes, and snippets.

@jerolimov
Created September 20, 2012 16:09
Show Gist options
  • Save jerolimov/3756821 to your computer and use it in GitHub Desktop.
Save 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 16, 2019

How to get the final url?
Because if i use "url" it does not work (403).
if I use url + &sig= s still 403

@jerolimov
Copy link
Author

jerolimov commented Jul 21, 2019

@Zibri this code is ~ 7 years old and I didn't tested it since then. You can expect that the YouTube "API" changed.

You should take a look into the youtube-dl command line project. See https://github.com/ytdl-org/youtube-dl

@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