📜 ⬆️ ⬇️

VKPLS - Generate streaming audio playlist from vk.com

image

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

With the advent of social networks and their wide distribution, users spend a lot of time online. Everyone loves music, and personally I can't live without it. So it turned out that I keep all my music collection in the Vkontakte profile. Since I am always up to date on new products, I do not spend my time searching and downloading, and the ability to access my music from almost any gadget in any place where there is internet gives maximum convenience. I am very glad that those wooden times, when good Internet was a luxury, have passed. It is no longer necessary to store such information on your hard disk. Anything that is not private is thrown into the cloud.
')
I do most of my work at the computer, which means that I need music as oxygen — to concentrate on the tasks. Cut your favorite album in 5.1 and create. But there is one thing: in order to listen to VK.com music, I have to go online, and if you go online, you will certainly get a bunch of messages and drag out into unwanted conversations. I am a kind and sympathetic 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 never met the normal plug-ins for music players or the players themselves for listening to music from VK.com. Then I decided that I needed to do something with this and in a couple of hours I threw in a small php script, which I called vkpls (it's not hard to guess what I meant).

The essence of the script is to get direct links to audio recordings and to generate a streaming playlist, the algorithm is ugly simple, I tied it to VK.API:

To start, I created the Standalone application in the "For Developers" section and got the right to access audio recordings for it. After that, I had to go through authorization to create ACCESS_TOKEN, since access to information about audio recordings (the audio.get method in vk.api) is not possible with a simple POST or GET request.

Now I could, using the tools of old PHP, send a request with parameters of interest to 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 records of a user or community with all the additional information. Bingo, that's what I needed.

So, for example, in response to such a request:
https://api.vk.com/method/audio.get?user_id=_ID&v=5.28&access_token=_ACCESS_TOKEN 

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
}, {
id: 232735496,
owner_id: 34,
artist: 'Aloe Blacc',
title: 'Wake Me Up',
duration: 224,
url: 'http: //cs6109.vk....FzHJU55ixz8Av8ujc',
lyrics_id: 119056069,
genre_id: 2
}]
}

Look - we are interested in the artist, title, duration, url keys for each audio recording. Using the json_decode function, I converted the resulting array into a format understandable to php. All I have left to achieve the result is to generate a playlist file.

M3U playlist structure:
# EXTM3U
#EXTINF: duration, artist - title
url
...


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

Summary


I decided to share my idea and make it available to people like me. Using the CSS framework, Maxmertkit (presented by one of the Habrahabr users here ) has created 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 periodicity of 0.5 - 3 days, I recommend to update your playlist more often.

That's all, thank you for your attention.

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


All Articles