📜 ⬆️ ⬇️

Backup audio records from a playlist on VKontakte (up to 6000) using Python and the VK API

Hello.

Previously, I often listened to music using Vkontakte (VC). After switching to Ubuntu 14.10, problems arose in the form of a complete freezing of the computer while listening to audio recordings through the Google Chrome browser for linux systems. In this regard, it became necessary to save your playlist to listen to music in offline mode. For these purposes, I decided to write a small script in the Python language, which can not only download music from scratch, but also update the existing library.

I used such modules:

Actually, let's start.

First we connect the modules:
')
import os import requests from selenium import webdriver import json 

Next, you need to get access_token to execute requests to the API and obtain the necessary access rights.
Before this we need to create and activate our Standalone / Desktop application, the id of which we will specify in the request.

The scheme is quite simple: we open the browser window, click on the link, enter data from the account, allow access, copy the necessary data from the url at the exit (this is access_token and expires_in - token expiration).

More details about creating the application and authorization can be found here .

I used selenium because of my own laziness. This module has enough functionality to automate all the above actions. You can use any other virtual browser that you like.

Actually code with comments:

 #    driver = webdriver.Firefox() #   . # client_id -     # scope -   driver.get("http://api.vkontakte.ru/oauth/authorize?" "client_id=4591034&scope=audio" "&redirect_uri=http://api.vk.com/blank.html" "&display=page&response_type=token") user = "email/phone" password = "password" #         user_input = driver.find_element_by_name("email") user_input.send_keys(user) password_input = driver.find_element_by_name("pass") password_input.send_keys(password) #    submit = driver.find_element_by_id("install_allow") submit.click() #        api current = driver.current_url access_list = (current.split("#"))[1].split("&") access_token = (access_list[0].split("="))[1] # acces_token expires_in = (access_list[1].split("="))[1] #     user_id = (access_list[2].split("="))[1] # id      #    driver.close() 

Until recently, “VK API” returned all data in xml format, which was a bit inconvenient. Now the answers are being serialized in the json dictionary with which you can work without problems. This is where the json library comes in handy, and more specifically the loads () method, which converts a string to a dictionary.

Then the action plan is quite simple:
  1. We execute the request and get in the response a list of all audio recordings from the playlist of the account (limit to 6000);
  2. We transform the answer into the dictionary;
  3. We take the necessary information from the dictionary (performer, track name, track link);
  4. We download audio recordings in the right directory, calling the desired names, rather than a standard set of letters, which stores the name of the track.


First step:

 #    print "Connecting" #   url = "https://api.vkontakte.ru/method/" \ "audio.get?uid=" + user_id +\ "&access_token=" + access_token #      artists_list = [] titles_list = [] links_list = [] #         number = 0 #        page = requests.get(url) html = page.text 

Second step:

 my_dict = json.loads(html) #  loads() 


The third step, I think everything is clear without commenting:

 for i in my_dict['response']: artists_list.append(i['artist']) titles_list.append(i['title']) links_list.append(i['url']) number += 1 

The fourth and final step:

 #  ,       path = "downloads" if not os.path.exists(path): os.makedirs(path) #    print "Need to download: ", number #    for i in range(0, number): #     /   new_filename = path+"/"+artists_list[i] + " - " + titles_list[i] + ".mp3" print "Downloading: ", new_filename, i #     if not os.path.exists(new_filename): #   ,           with open(new_filename, "wb") as out: response = requests.get(links_list[i].split("?")[0]) out.write(response.content) print "Download complete." 

Now I am working on a self-made player for listening to audio recordings from a VC (yes, it is a separate player, and not a plugin for already existing popular players). In the future I plan to write an article in which the process will be described.

That's all, thank you for your attention.

UPD:
The article is slightly edited in accordance with the wishes of the comments.

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


All Articles