Skip to content

Instantly share code, notes, and snippets.

@badrazizi
Last active April 1, 2018 03:30
Show Gist options
  • Save badrazizi/995ec17f6b9955913f9f3863f2c7c702 to your computer and use it in GitHub Desktop.
Save badrazizi/995ec17f6b9955913f9f3863f2c7c702 to your computer and use it in GitHub Desktop.
javascript google drive video link extractor
const $ = require('jquery');
// Extract GOOGLE DRIVE Links
function GOOGLEDRIVE(link, callback) {
let id = GetID(link);
$.get(`https://drive.google.com/get_video_info?docid=${id}`)
.done(function(data, status) {
if ((data.indexOf('errorcode=100&reason=') == -1) || (data.indexOf('errorcode=150&reason=') == -1)) {
callback('error');
} else {
let qualty = {};
let response = decodeURIComponent(data);
response = decodeURIComponent(response);
response = response.split('url_encoded_fmt_stream_map')[1];
let files = response.match(/https(.*?)quality/g);
files.forEach(file => {
file = file.replace(';+codecs="avc1.42001E,+mp4a.40.2"&quality', '');
if (file.indexOf('itag=137') > -1 || file.indexOf('itag=37') > -1 || file.indexOf('itag=46') > -1) {
qualty['1080p'] = file;
} else if (file.indexOf('itag=136') > -1 || file.indexOf('itag=22') > -1 || file.indexOf('itag=45') > -1) {
qualty['720p'] = file;
} else if (file.indexOf('itag=135') > -1 || file.indexOf('itag=59') > -1 || file.indexOf('itag=44') > -1 || file.indexOf('itag=35') > -1) {
qualty['480p'] = file;
} else if (file.indexOf('itag=18') > -1 || file.indexOf('itag=134') > -1 || file.indexOf('itag=34') > -1 || file.indexOf('itag=43') > -1 || file.indexOf('itag=6') > -1) {
qualty['360p'] = file;
} else if (file.indexOf('itag=133') > -1 || file.indexOf('itag=5') > -1 || file.indexOf('itag=36') > -1) {
qualty['240p'] = file;
} else if (file.indexOf('itag=160') > -1 || file.indexOf('itag=17') > -1) {
qualty['144p'] = file;
}
});
callback(qualty);
}
})
.fail(function(data, status) {
callback('error');
});
}
function GetID(link) {
let index = link.indexOf('id=');
let closingIndex;
if (index > 0) {
index += 3;
closingIndex = link.indexOf('&', index);
if (closingIndex < 0) {
closingIndex = link.length;
}
} else {
index = link.indexOf("file/d/");
if (index < 0) {
return '';
}
index += 7;
closingIndex = link.indexOf('/', index);
if (closingIndex < 0) {
closingIndex = link.indexOf('?', index);
if (closingIndex < 0) {
closingIndex = link.Length;
}
}
}
return link.substr(index, closingIndex - index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment