Skip to content

Instantly share code, notes, and snippets.

@1d10t
Created December 20, 2019 19:23
Show Gist options
  • Save 1d10t/bcdc1939a38c7313c9efba6214f37d9a to your computer and use it in GitHub Desktop.
Save 1d10t/bcdc1939a38c7313c9efba6214f37d9a to your computer and use it in GitHub Desktop.
String.prototype.htmldecode = function(){
if(!window.htmldecode_textarea) window.htmldecode_textarea = document.createElement('textarea');
window.htmldecode_textarea.innerHTML = this;
return window.htmldecode_textarea.value;
}
function ubtoa(str){
return window.btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
function m3u_generate(list){
var keys = Object.keys(list);
var data = "#EXTM3U\n"+keys.map(function(key){
var value = list[key];
return '#EXTINF:'+value[3]+','+value[1].htmldecode()+' - '+value[2].htmldecode()+"\n"+value[4]+"\n";
}).join('');
return data;
}
function get_file(data, filename, mime){
if(!mime) mime = 'application/octet-stream';
if(!window.URL || !window.Blob){
window.open('data:'+mime+';base64,' + encodeURIComponent(ubtoa(data)));
return;
}
var
url = window.URL.createObjectURL(new Blob([data], {type: mime})),
a = document.createElement('a')
;
a.href = url;
a.download = filename;
a.click();
setTimeout(function(){
a.remove();
window.URL.revokeObjectURL(url);
}, 1000);
}
function m3u_download(list, filename){
if(!filename) filename = 'vkontakte-'+(new Date).toISOString().replace(/[^\w\d]/g, '-');
else filename = filename.replace(/[^\w\dа-яА-ЯёЁ\(\)\[\]\- ]/g, '_');
get_file(
m3u_generate(list),
filename+'.m3u',
'audio/mpegURL'
);
}
var resolution = prompt('resolution? 360, 720, 1080', 720);
var list = Object.keys(localStorage).reduce(function(list, k) {
const m = k.match(/^v\.(.+_.+)$/);
if(!m) return list;
const id = m[1];
const d = JSON.parse(localStorage.getItem(k));
if(d.time > Date.now()-12*3600*1000){
var url = d['url'+resolution];
console.log('url = ' + url);
if(url){
list[id] = [null,
d.md_author,
d.md_title,
d.duration,
url];
}
};
return list;
}, {});
m3u_download(list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment