📜 ⬆️ ⬇️

Downloading music from vk.com

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) //    ,     .setParameter("need_user", "0") .setParameter("count", "2000") //    .setParameter("offset", "0") // ,       .setParameter("access_token", ACCESS_TOKEN); //  ,   URI uri = builder.build(); 

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); //   ! String pathname = "e:/vkmp3/" + fixWndowsFileName(mp3.get("artist") + " - " + mp3.get("title")); try { File destination = new File(pathname + ".mp3"); if (!destination.exists()) { FileUtils.copyURLToFile(new URL((String) mp3.get("url")), destination); } } catch (FileNotFoundException e) { System.out.print("ERROR "+pathname); } } } 

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[] {"<", ">", ":", "\"", "/", "\\", "|", "?", "*"}; //  windows String result = pathname; for (String forbiddenSymbol: forbiddenSymbols) { result = StringUtils.replace(result, forbiddenSymbol, ""); } //      '& amp',      return StringEscapeUtils.unescapeXml(result); } 


Compile, run - rejoice, looking as the entire online collection of audio is loaded onto disk.

Concluding remarks


Full working source code

code

Dependency list

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


All Articles