⬆️ ⬇️

We extort music from the contact. Geek way

It all started with the fact that I liked one group, where they regularly laid out music. But since I listen to music mostly from the player, the question arose of downloading songs to my PC. I wanted to make a pretty big playlist and throw away something you didn't like. According to one song, download is masochism. I do not trust any pribluda from the network. How did I do it?

I began with a cursory inspection of the markup on the Contact page. Immediately it became clear that the link to the song is stored in clear text as the value of the hidden input, and corresponds to the mask http: //cs*.userapi.com/*/audio/*.mp3,*.

I quickly jQuery concocted a request to the console ... I got nothing, because vkontakte my favorite library is not used. Well, it does not matter, through the same console we connect jQuery to the page with the help of this trick:

function addJQuery(callback) { var script = document.createElement("script"); script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"); script.addEventListener('load', function() { var script = document.createElement("script"); script.textContent = "(" + callback.toString() + ")();"; document.body.appendChild(script); }, false); document.body.appendChild(script); } 


Now you can and do business. Having looked at all the hidden fields using the input:hidden selector, I saw that those of them contain the required link have an id corresponding to the audio_info * mask. Improve the selector a bit. input:hidden[id*='audio'] gets only the inputs we need.

It would stop here, and just get links to all the audio like this:

$('input:hidden[id*="audio"]').each(function () {

url=$(this).attr('value').split(',')[0];

console.log("wget "+url);

});
, feed them to wget-y, and get at the output ... Hash in file names, because Not all songs have idv3 tags, and the file name after downloading consists of the symbols a..z and 0..9 without a hint of the artist or track name. Vooschem, just go file on the server. Therefore, we will continue.

We are looking for information about the performer and the name of the song near our input and we come to the conclusion that we can do this:

 $('input:hidden[id*="audio"]').each(function () { url=$(this).attr('value').split(',')[0]; author=$(this).parent().parent().parent().find('b').find('a[href*="search"]').html(); song=$(this).parent().parent().parent().find('span[id*="title"]').html(); console.log('wget -O "'+author+'-'+song+'.mp3" '+url); }); 


It remains to copy the console output, remove garbage (if any) from the file, and run what turned out as an executable script.

It works fine, mine has already downloaded about 2 GB ~ 126 songs. In one stream of course, but I'm not in a hurry. Anyone who read here - thanks :)


')

Source: https://habr.com/ru/post/143406/



All Articles