stream_example.py 817 B

123456789101112131415161718192021222324252627282930313233343536
  1. from time import sleep
  2. from radio import Radio
  3. from yandex_music import Client
  4. # API instance
  5. client = Client(token='YOUR_API_KEY_HERE').init()
  6. # get some track
  7. track = client.tracks(['2816574:303266'])[0]
  8. album = track.albums[0]
  9. artist = track.artists[0]
  10. # stream by track
  11. _station_id, _station_from = f'track:{track.id}', 'track'
  12. # stream by album
  13. # _station_id, _station_from = f'album:{album.id}', 'album'
  14. # stream by artist
  15. # _station_id, _station_from = f'artist:{artist.id}', 'artist'
  16. # Radio instance
  17. radio = Radio(client)
  18. # start radio and get first track
  19. first_track = radio.start_radio(_station_id, _station_from)
  20. print('[Radio] First track is:', first_track)
  21. # get new track every 5 sec
  22. while True:
  23. sleep(5)
  24. next_track = radio.play_next()
  25. print('[Radio] Next track is:', next_track)