99 行
3.5 KiB
Python
99 行
3.5 KiB
Python
from random import random
|
|
|
|
from yandex_music import Track
|
|
|
|
|
|
class Radio:
|
|
def __init__(self, client):
|
|
self.client = client
|
|
self.station_id = None
|
|
self.station_from = None
|
|
|
|
self.play_id = None
|
|
self.index = 0
|
|
self.current_track = None
|
|
self.station_tracks = None
|
|
|
|
def start_radio(self, station_id, station_from) -> Track:
|
|
self.station_id = station_id
|
|
self.station_from = station_from
|
|
|
|
# get first 5 tracks
|
|
self.__update_radio_batch(None)
|
|
|
|
# setup current track
|
|
self.current_track = self.__update_current_track()
|
|
return self.current_track
|
|
|
|
def play_next(self) -> Track:
|
|
# send prev track finalize info
|
|
self.__send_play_end_track(self.current_track, self.play_id)
|
|
self.__send_play_end_radio(self.current_track, self.station_tracks.batch_id)
|
|
|
|
# get next index
|
|
self.index += 1
|
|
if self.index >= len(self.station_tracks.sequence):
|
|
# get next 5 tracks. Set index to 0
|
|
self.__update_radio_batch(self.current_track.track_id)
|
|
|
|
# setup next track
|
|
self.current_track = self.__update_current_track()
|
|
return self.current_track
|
|
|
|
def __update_radio_batch(self, queue=None):
|
|
self.index = 0
|
|
self.station_tracks = self.client.rotor_station_tracks(self.station_id, queue=queue)
|
|
self.__send_start_radio(self.station_tracks.batch_id)
|
|
|
|
def __update_current_track(self):
|
|
self.play_id = self.__generate_play_id()
|
|
track = self.client.tracks([self.station_tracks.sequence[self.index].track.track_id])[0]
|
|
self.__send_play_start_track(track, self.play_id)
|
|
self.__send_play_start_radio(track, self.station_tracks.batch_id)
|
|
return track
|
|
|
|
def __send_start_radio(self, batch_id):
|
|
self.client.rotor_station_feedback_radio_started(
|
|
station=self.station_id, from_=self.station_from, batch_id=batch_id
|
|
)
|
|
|
|
def __send_play_start_track(self, track, play_id):
|
|
total_seconds = track.duration_ms / 1000
|
|
self.client.play_audio(
|
|
from_="desktop_win-home-playlist_of_the_day-playlist-default",
|
|
track_id=track.id,
|
|
album_id=track.albums[0].id,
|
|
play_id=play_id,
|
|
track_length_seconds=0,
|
|
total_played_seconds=0,
|
|
end_position_seconds=total_seconds,
|
|
)
|
|
|
|
def __send_play_start_radio(self, track, batch_id):
|
|
self.client.rotor_station_feedback_track_started(station=self.station_id, track_id=track.id, batch_id=batch_id)
|
|
|
|
def __send_play_end_track(self, track, play_id):
|
|
# played_seconds = 5.0
|
|
played_seconds = track.duration_ms / 1000
|
|
total_seconds = track.duration_ms / 1000
|
|
self.client.play_audio(
|
|
from_="desktop_win-home-playlist_of_the_day-playlist-default",
|
|
track_id=track.id,
|
|
album_id=track.albums[0].id,
|
|
play_id=play_id,
|
|
track_length_seconds=int(total_seconds),
|
|
total_played_seconds=played_seconds,
|
|
end_position_seconds=total_seconds,
|
|
)
|
|
|
|
def __send_play_end_radio(self, track, batch_id):
|
|
played_seconds = track.duration_ms / 1000
|
|
self.client.rotor_station_feedback_track_finished(
|
|
station=self.station_id, track_id=track.id, total_played_seconds=played_seconds, batch_id=batch_id
|
|
)
|
|
pass
|
|
|
|
@staticmethod
|
|
def __generate_play_id():
|
|
return "%s-%s-%s" % (int(random() * 1000), int(random() * 1000), int(random() * 1000))
|