πŸ“œ ⬆️ ⬇️

Downloading music from VK using VK api and Python3

More recently, it was the first of June, and here it was already the first of September. Autumn in the yard.

Yes, recently, too, so turned away, and woke up - Mail absorbed VK. And so it began: you will not look at the video without advertising, you will not listen to the music - the copyright holders have prohibited it. Rumor has it that generally banned. I sensed something was wrong. And then this is the time of year. So I thought, why don't I collect my stocks? I will conserve my music on my computer, I will transfer it to disk - it will be sweeter than any jam! And it will help me, if not strangely, the VC itself, or rather its api. And still the third python, the built-in urllib library and the library for working with this in json format.

You can request information about the user's audio recordings from api vk. The answer will come to us in json format, and it will contain the number of user audio recordings, as well as extended information about each song, if such information is available. And most importantly, each song will have a url address where it lies on VK servers. Just what we need.

To work with most api methods, you need a special key (hereinafter referred to as access_token), which vk issues to applications. How to get it - I'll tell you a little later.
')
Remember the window that appears as soon as the application requests access to information from your page? By clicking on β€œAllow”, you give the application the right to get access_token in your name, which will contain the parameters for accessing information from your page.

Well, let's start?

Open the " developers " tab and create a new application:

image

Enter the name of your application and select its type: standalone application:

image

We confirm the action via SMS, which falls into your mobile:

image

As soon as we have completed all the formalities, we get the id of our application, which we will use to get access_token-a. Let's open that part of the documentation, which is dedicated to the authorization of client standalone-applications .

image

image

Important
redirect_uri in our case should be equal to oauth.vk.com/blank.html oauth.vk.com/blank.html , since a different address needs to be specified only if we are developing a browser-based javascript application.

Since we will work with audio recordings in our application, we will pass the audio parameter to the scope attribute. Normally access_token is issued for a while. To get it indefinitely, you can pass an offline parameter to the scope attribute.

Notes:

For a complete table of parameters for the scope attribute, see the link .

As a result, in the address bar, we drive in something like:
  https://oauth.vk.com/authorize/client_id=YOUR_CLIENT_ID&redirect_uri=https://oauth.vk.com/blank.html&display=page&response_type=token 

If everything worked out for you, you will see this window.

image

After successful authorization and access rights, you will be redirected to oauth.vk.com/blank.html oauth.vk.com/blank.html . In the address bar, you will see the access_token= YOUR_ACCESS_TOKEN . Be sure to save this key in order not to lose it in the future. That is what we will use when writing requests for api.

In order to get a list of audio records of a user, we will use the api audio.get method. As I said above, the audio.get method returns us the answer in json-format. Here is an example of a json object that returns this method.
 {
	 "response":
		 [712,
			 {
			 "aid": 393825624,
			 "owner_id": 59223044,
			 "artist": "Saint Asonia",
			 "title": "Blow Me Wide Open",
			 "duration": 224,
			 "url": "http: \\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ bit page Δ±Δ±Δ±Δ±Δ±Δ±Δ±8Δ±Δ±8Δ±88 httpK http8??????????????????? ?mememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememememe B::: :memememememememememememememememememememememememememememememe me: :mememememememe mek::
			 "lyrics_id": "274446714",
			 "genre": 18
			 },
			 {"aid": 392782493,
			 "owner_id": 59223044,
			 "artist": "Kongos",
			 "title": "I'm Only Joking",
			 "duration": 225,
			 β€œo” o ”o” o ”o” o ”o” o ”o” p – e –––– o –––––––––––––––––––––––––––––––––––––––––––––––––––––
			 "lyrics_id": "184734619"
			 , "genre": 21
			 }
		 ]
 }

So, as you can see, the object consists of a dictionary with the response key, which contains an array of songs.

The first object is the number of songs on the user's page, and all subsequent ones are dictionaries containing information about the songs. We will use the request.urlopen method from the urllib library, which allows you to receive data stored at the url address on the Internet.

from urllib.request import urlopen

The urlopen function requires a required parameter β€” the url address that it opens. In our case, this is the audio.get method:

image

To access api vk methods, you need to pull the methods located at
  https://api.vk.com/method/METHOD_NAME? 
, passing there all the arguments needed by the method.

So, we will write that url which we will open:
address = 'https://api.vk.com/method/audio.get?owner_id=YOUR_OWNER_ID&access_token=YOUR_ACCESS_TOKEN'

and open it:

data = urlopen (address)

The data that we received must be read and decoded, since we receive non-decoded information. And since this is all wrapped up in json format, in order to handle the dictionary as with a Python object, we must use the json library.

import json

We use the loads () method, which turns a string containing a json object into a Python language object:

decoded_response = data.read (). decode ()
final_data = json.loads (decoded_response)

Hooray! Now we can access the data contained in the variable final_data. We get all the dictionaries that contain information about the songs:

songs = final_data ['response'] [1:]

This line we got all the elements of the response array from 1 to the final. Now we will work with each song separately.
 for song in songs:
	 song_artist = song ['artist']
	 song_title = song ['title']
	 song_url = song ['url'] 


Get the information stored at song_url:

cached_song = urlopen (song_url) .read ()

And write it to the file. Where to create it, you ask? And for this you will need the os library. We write outside our cycle:
import os

And create a music folder on the C function drive
os.mkdir ('C: // Music')

Now we have a folder in which we want to record our songs.

In order to keep our songs in order, we will create groups for each artist. And in order to understand whether we need to create a folder or not, we use the os.listdir function, which returns a list of objects in the specified path:
 if song_artist not in os.listdir ('C: // Music'):
	 os.mkdir ('C: // Music /% s'% (song_artist)) 


Now we create a file and write our song there.
filename = 'C: //Music/%s/%s.mp3'% (song_artist, song_title)
file = open (filename, 'wb')
file.write (cached_song)
file.close ()

Hooray. It remains only to once again look at the beautiful code and click "Run." Well, and, of course, wait a bit, because the songs need some time to record.

Full text of the program
 from urllib.request import urlopen
 import json
 import os
 os.mkdir ('C: // Music')

 address = 'https://api.vk.com/method/audio.get?owner_id=MY_ID&access_token=MY_TOKEN'
 data = urlopen (address)
 decoded_response = data.read (). decode ()
 final_data = json.loads (decoded_response)
 songs = final_data ['response'] [1:]
 for song in songs:
        song_artist = song ['artist']
        song_title = song ['title']
        song_url = song ['url']
        cached_song = urlopen (song_url) .read ()
        if song_artist not in os.listdir ('C: // Music'):
               os.mkdir ('C: // Music /% s'% (song_artist))
        filename = 'C: //Music/%s/%s.mp3'% (song_artist, song_title)
        file = open (filename, 'wb')
        file.write (cached_song)
        file.close ()


Enjoy music and love programming. Before communication!

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


All Articles