Last active
July 23, 2020 08:46
-
-
Save Angel777d/8ce26c105e6f9a68f58eb3169196f5ac to your computer and use it in GitHub Desktop.
yandex music python api example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from pathlib import Path | |
from time import sleep | |
from yandex_music import Client | |
# LOGIN = "" | |
# PASSWORD = "" | |
# token = Client().generate_token_by_username_and_password(LOGIN, PASSWORD) | |
# print("TOKEN:", token) | |
# Use token to login. Get token using commented code above. | |
TOKEN = "YOUR TOKEN HERE" | |
# create new api client instance | |
client = Client.from_token(TOKEN) | |
# get all user likes info | |
like_playlist = client.users_likes_tracks() | |
# get first track from list | |
first_short_track = like_playlist.tracks[0] | |
# get download info list and select one with mp3 codec and 192 kbps bitrate | |
info_list = client.tracks_download_info(first_short_track.track_id) | |
filtered_info_list = [f for f in info_list if f.codec == "mp3" and f.bitrate_in_kbps == 192] | |
download_info = filtered_info_list[0] | |
# get selected url | |
url = download_info.get_direct_link() | |
# configure VLC player (windows example) | |
# install VLC: https://www.videolan.org/vlc/index.ru.html | |
# install python-vlc lib: https://pypi.org/project/python-vlc/ | |
# note: use x32 VLC for x32 python | |
path = Path("C:\Program Files (x86)\VideoLAN\VLC") | |
os.add_dll_directory(path.as_posix()) | |
import vlc | |
# open url with VLC | |
player = vlc.MediaPlayer(url) | |
player.play() | |
while True: | |
print("main loop") | |
sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment