VKPLS - Generate streaming audio playlist from vk.com


I want to share with readers of Habrahabr a small web service (script) that I wrote for myself.

With the advent of social networks and their wide distribution, users spend quite a lot of time online. Everyone loves music, but I personally cannot live without it. It so happened that I am storing my entire music collection in the Vkontakte profile. Since I am always up to date with new products, I do not spend my time searching and downloading, and the ability to access my music from almost any gadget anywhere where there is Internet gives maximum convenience. I am very glad that those wooden days were gone when good internet was a luxury. There is no longer any need to store such information on your hard drive. All that is not confidential, I throw in the cloud.

I do most of the work at the computer, which means that I need music as oxygen - to concentrate on the tasks. You cut your favorite album in 5.1 and create. But there is one thing: in order to listen to music on VK.com, I have to go online, and if you go online, you will certainly get a bunch of messages and drag yourself into unwanted conversations. I am a kind and responsive person, so I can not ignore my friends with their constant problems. But I need to concentrate on work, and all my music is where something always distracts me.

I love Linux, but I have never seen normal plugins for music players or players themselves for listening to music from VK.com. Then I decided that I needed to do something with this and put in a couple of hours a small php script, which I called vkpls (it's not hard to guess what I meant).

The essence of the script in obtaining direct links to record and generate a streaming playlist, the algorithm is simple to outrageous, I tied it on VK.API:

First, I created a Standalone application in the section "For Developers" and was for him the right to have access to recordings. After that, I had to go through authorization to create ACCESS_TOKEN, because access to information about audio recordings (the audio.get method in vk.api) is not possible by a simple POST or GET request.

Now, with the help of old PHP, I could send a request with the parameters that interests me without restrictions, and in return receive the information of interest to me in JSON format. The audio.get function returns a list of audio recordings of a user or community with all additional information. Bingo, that was what I needed.

So, for example, in response to such a request:

we get an array in JSON format with the following information:
Answer to audio.get in JSON
response: {
count: 505,
items: [{
id: '34',
photo: 'http: //cs7009.vk....2/rj4RvYLCobY.jpg',
name: 'Tatyana Plutalova',
name_gen: 'Tatyana'
}, {
id: 232745053,
owner_id: 34,
artist: 'Ambassadeurs',
title: 'Sparks',
duration: 274,
url: 'http: //cs6164.vk....lGEJhqRK8d5OQZngI',
lyrics_id: 120266970,
genre_id: 18
}, {
id: 232733966,
owner_id: 34,
artist: 'Aloe Blacc',
title: 'Can You Do This',
duration: 176,
url: 'http: //cs6157.vk....erOa0DvsyOCYTPO1w',
genre_id : 2
}, {
owner_id: 34,
artist: 'Aloe Blacc',
title: 'Wake Me Up',
duration: 224,
url: 'http: //cs6109.vk....FzHJU55ixz8Av8ujc',
lyrics_id: 119056069,
genre_id: 2

Look - the keys of interest to us artist, title, duration, url are present for each audio recording. Using the json_decode function, I converted the resulting array into a format clear for php. All that remains for me to achieve the result is to generate a playlist file.

M3U playlist structure:
#EXTINF: duration, artist - title

There was nothing easier to write to the file using the foreach loop all the data received and save it in m3u.
Hooray, everything worked out, now I can listen to music in any music player without the need for VKontakte authorization.


I decided to share my idea and make it accessible to people like me. Using CSS framework Maxmertkit (presented by one of the users of "Habrahabr" here ) made up a small page for the convenience of using the script. For everyone, it is available at the following link - VKPLS . There you can read the instructions or watch the video.

It should be noted that there is one thing. Due to the fact that links to audio recordings on Vkontakte servers change with a frequency of 0.5 to 3 days, I recommend updating your playlist more often.

That's all, thanks for your attention.

Also popular now: