Skip to content

Instantly share code, notes, and snippets.

@brcontainer
Created July 4, 2017 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brcontainer/27c8c0af9bd6b494d728e96139b326b4 to your computer and use it in GitHub Desktop.
Save brcontainer/27c8c0af9bd6b494d728e96139b326b4 to your computer and use it in GitHub Desktop.
Example with Flask for retrieve https://www.youtube.com/get_video_info?video_id=
from flask import Flask
import urllib
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
@app.route("/youtubeproxy/<youtubeid>")
r = urllib.urlopen('https://www.youtube.com/get_video_info?video_id=' + youtubeid)
return Response(r.read(), headers={
'Access-Control-Allow-Origin': '*',
'Access-Control-Request-Method': '*',
'Access-Control-Allow-Methods': 'OPTIONS, GET',
'Access-Control-Allow-Headers': '*'
})
if __name__ == "__main__":
app.run()
//get queryStringMap, listOfQueryStringMaps functions from https://gist.github.com/jerolimov/3756821
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 getVideoFormat = function(format) {
var videoFormats = {
22: { fileType: 'mp4', resolution: 'hd720' },
43: { fileType: 'webm', resolution: '360p' },
18: { fileType: 'mp4', resolution: '360p' },
36: { fileType: '3gp', resolution: '240p' },
17: { fileType: '3gp', resolution: '144p' }
};
if (videoFormats[format]) {
return videoFormats[format];
} else {
return {
resolution : "Unrecognized",
fileType : "Unrecognized"
};
}
};
var get_info_video = function() {
if (inputUrl.text == '')
return;
var video_id = inputUrl.text.substring(inputUrl.text.indexOf('v=')).split('&')['0'].replace('v=', '')
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.YOURDOMAIN.com/youtubeproxy/" + video_id, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (!(xhr.status >= 200 && xhr.status < 300)) {
console.log("HTTP error");
return;
}
var ready_ = xhr.responseText;
print(ready_.indexOf('errorcode=2'))
if (ready_.indexOf('errorcode=2') > -1)
return;
var query_ = listOfQueryStringMaps(queryStringMap(ready_)['url_encoded_fmt_stream_map']);
var format;
query_.forEach(function(videoEntry) {
format = getVideoFormat(videoEntry.itag);
console.log(decodeURIComponent(JSON.stringify(videoEntry, null, '\t')));
listView.append({url: videoEntry.url, "type": format.resolution + ' - ' + format.fileType});
});
}
};
xhr.send();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment