
We use Yandex Music outside the browser
Recently, I wanted to work with Yandex Music not through a browser. As it turned out, they did not have an open api, and the case was not trivial. And nothing ready could be found.
Those who are too lazy to read can immediately go to the result - python-yamusic .
To get started, I armed myself with a firebug and tracked where the requests go. The main directions were found:
The data received by the first three queries is easy to filter using Beautiful Soup , as:
You can see my parser implementation in the code .
Now it seems that everything is simple. We make a request to storage.music.yandex.ru/get storage dir of the track /2.xml and get an xml of the form: Now we make a request storage.music.yandex.ru/download-info storage dir of the track / filename of the track and get one more xml'ku: But, having looked at the requests for downloading the track, it became clear that there is not all the data. Scrolling through javascript, I found a function that creates the key we need from the path without the first character and parameter s. This function was too lazy to rewrite in python, so it just runs through QScript. Now, having all the data, you can compile the url of the track: http: // host / get-mp3 / key /
the ts parameter is the path ? track-id = id of the track & \ region = region & from = service-search, but you won’t be able to download the file just like that. Without hesitation, I decided to try to do everything in one session - everything worked out right away. The result is a python-yamusic library
It is very simple, first you need to initiate a Qt application and make all the imports: To search, there is: To get data from the found objects: And, in fact, to open the track: PS & \ region is written because of ®ion
Those who are too lazy to read can immediately go to the result - python-yamusic .
We analyze the work
To get started, I armed myself with a firebug and tracked where the requests go. The main directions were found:
- music.yandex.ru/fragment/search?text= search query with & type = type & page = page where 3 types are possible: tracks , albums and artists ; other parameters are clear. This is a search query, from the result of which we can get the id of songs, albums and artists;
- music.yandex.ru/fragment/artist id of artist / tracks - page with all tracks and albums of the artist;
- music.yandex.ru/fragment/album id of the album - album page;
- storage.music.yandex.ru/get storage dir track /2.xml - xml with the file name and track length;
- storage.music.yandex.ru/download-info storage dir track / track filename - xml with data for loading the track;
- incomprehensible and always different track address.
The data received by the first three queries is easy to filter using Beautiful Soup , as:
- all tracks are in divs with the b-track class with all the data in the onclick property (from here we get its storage dir and id );
- albums are in divs with the b-albums class ;
- artists in divs with class b-artist-group .
You can see my parser implementation in the code .
Download track
Now it seems that everything is simple. We make a request to storage.music.yandex.ru/get storage dir of the track /2.xml and get an xml of the form: Now we make a request storage.music.yandex.ru/download-info storage dir of the track / filename of the track and get one more xml'ku: But, having looked at the requests for downloading the track, it became clear that there is not all the data. Scrolling through javascript, I found a function that creates the key we need from the path without the first character and parameter s. This function was too lazy to rewrite in python, so it just runs through QScript. Now, having all the data, you can compile the url of the track: http: // host / get-mp3 / key /
хост
путь
параметр ts
регион
параметр s
the ts parameter is the path ? track-id = id of the track & \ region = region & from = service-search, but you won’t be able to download the file just like that. Without hesitation, I decided to try to do everything in one session - everything worked out right away. The result is a python-yamusic library
Library usage
It is very simple, first you need to initiate a Qt application and make all the imports: To search, there is: To get data from the found objects: And, in fact, to open the track: PS & \ region is written because of ®ion
>>> from yamusic.app import Search, cursor
>>> from PySide.QtCore import QCoreApplication
>>> import sys
>>> app = QCoreApplication(sys.argv)
>>> cursor.search(Search.TYPE_ARTISTS, 'query')
>>> cursor.search(Search.TYPE_ALBUMS, 'query')
>>> cursor.search(Search.TYPE_TRACKS, 'query')
>>> artist.get_albums()
>>> artist.get_tracks()
>>> album.get_tracks()
>>> track.open()