After the recent hype around the audio recordings on vk.com, I decided to play it safe and copy my entire collection to a hard disk. To solve this, I wrote a simple Java utility. Below is its code with comments. The article is intended for readers who are familiar with any general-purpose programming language and who know how to compile and run programs written on it.
Important note: if your audio recordings have already been taken, then downloading them in this way will most likely fail.Training
The list of audio recordings is obtained through the site's API; access token is needed to access the API. To obtain a token, go to the address (this is the
standard method described in the vk.com documentation for developers):
oauth.vk.com/authorize?client_id=3711445&scope=audio&
redirect_uri=https://oauth.vk.com/blank.html&display=page&v=5.0&response_type=token
')
and on the downloaded page, allow the application access to its audio recordings, then you will be redirected to the address of the form:
oauth.vk.com/blank.html#access_token=abc&expires_in=86400&user_id=123456
we need the access_token parameter - remember it. The last parameter, user_id, is your user ID, which is also required.
By completing these steps, you actually gave my application access to the list of your audio recordings, but since I don’t know the received token I don’t know, then there is no access, for security reasons, you can register your application on vk.com and use its app_id.
Write the code
So, we have a token, we can start downloading, in a nutshell, the scheme is as follows: we receive a list of audio records in JSON format, parse the information we need (song name, artist name and mp3 file address itself) and load the file, giving it a meaningful name.
Obtaining a list of audio recordings is performed by sending a POST request to the address:
(see the
audio.get API
method )
URIBuilder builder = new URIBuilder(); builder.setScheme("https").setHost("api.vk.com").setPath("/method/audio.get") .setParameter("oid", USER_ID)
We send request and we process the answer:
(for work with HTTP requests we use
Apache HttpClient library)
HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = null; try { instream = entity.getContent(); String responseAsString = IOUtils.toString(instream); parseAndDownload(responseAsString); } finally { if (instream != null) instream.close(); } }
For parsing JSON use
json simple , for downloading the file -
Apache Commons IO :
(important note - it is necessary that the folder in which you want to upload files already existed)
private static void parseAndDownload(String resp) throws IOException, ParseException { JSONParser parser = new JSONParser(); JSONObject jsonResponse = (JSONObject) parser.parse(resp); JSONArray mp3list = (JSONArray) jsonResponse.get("response"); for (int i=1; i<mp3list.size(); i++) { JSONObject mp3 = (JSONObject) mp3list.get(i);
The names of songs and artists can contain characters that are prohibited to be used in file names, remove the following characters:
(used by
Apache Commons Lang )
private static String fixWndowsFileName(String pathname) { String[] forbiddenSymbols = new String[] {"<", ">", ":", "\"", "/", "\\", "|", "?", "*"};
Compile, run - rejoice, looking as the entire online collection of audio is loaded onto disk.
Concluding remarks
- the code is rather dirty and not quite suitable for industrial use (in order to download a very large collection of songs, it is better to modify it), but it solves the problem
- Java is used only to demonstrate the idea, I think it will not be difficult to translate this code into your favorite language
- there are many programs for downloading audio, but there is absolutely no confidence in them (in terms of transferring their personal data to them)
- the program can be run several times - only files that do not exist on the disk will be loaded
Full working source code
codeDependency list
- json-simple-1.1.1.jar
- commons-lang3-3.1.jar
- commons-io-2.4.jar
- httpcomponents-client-4.2.5-bin.zip (all jar files from archive)