📜 ⬆️ ⬇️

We use "Yandex Music" outside the browser

Recently I wanted to work with “Yandex Music” not through a browser. As it turned out, they have no open api, and the case turned out to be not trivial. Yes, and nothing ready to find failed.

Those who are too lazy to read, can immediately go to the result - python-yamusic .

We analyze the work


')
For starters, I armed myself with firebug and traced where the requests go. The main directions were found:

The data obtained by the first three queries can be easily filtered using Beautiful Soup , since:

My implementation of the parser can be viewed in code .

Download the track



Now it seems that everything is simple. We make a request for storage.music.yandex.ru/get storage dir of the track /2.xml and get an xml of the form:
<?xml version="1.0" encoding="utf-8"?>
<track filename=" , 2.mp3" track-length=" " />

Now we make the request storage.music.yandex.ru/download-info storage dir of the track / filename of the track and get another xml file:
<download-info>
<host></host>
<path></path>
<ts> ts</ts>
<region></region>
<s> s</s>
</download-info>

But, after looking at requests for a track jump, it became clear that not all the data were there.
Scrolling through javascript, I found a function that from the path without the first character and the s parameter creates the key we need. This function was too lazy to rewrite in python, so it is simply executed through QScript.
Now that you have all the data, you can compile the url of the track: http: // host / get-mp3 / key / parameter ts path ? Track -id = id of the track & \ region = region & from = service-search, but not just download the file will succeed. Without hesitation, I decided to try to do everything in one session - everything turned out right away. The result is the python-yamusic library.

Library use



It's very simple, first you need to initiate the Qt application and do all the imports:
>>> from yamusic.app import Search, cursor
>>> from PySide.QtCore import QCoreApplication
>>> import sys
>>> app = QCoreApplication(sys.argv)

To search there is:
>>> cursor.search(Search.TYPE_ARTISTS, 'query')
>>> cursor.search(Search.TYPE_ALBUMS, 'query')
>>> cursor.search(Search.TYPE_TRACKS, 'query')

To retrieve data from the objects found:
>>> artist.get_albums()
>>> artist.get_tracks()
>>> album.get_tracks()

And, in fact, to open the track:
>>> track.open()


PS & \ region written for ®ion

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


All Articles