import os import logging import random import time from yandex_music import Client import config logging.basicConfig( level=logging.INFO, ) logger = logging.getLogger(__name__) def run(): logger.info('Init') client = Client( token=config.Yandex.token, ) client.init() logger.info('Getting tracklist') tracklist = client.usersLikesTracks() dirpath = str(os.path.join( config.Main.working_directory, config.Download.dirname, )) os.makedirs( name=dirpath, exist_ok=True, ) for track in tracklist: logger.info('Fetching track with ID %s' % track.id) track = track.fetch_track() logger.info('Downloading track %s — %s' % ( ', '.join(track.artistsName()), track.title, )) title = track.title for symbol in config.Download.escape_symbols: title = title.replace(symbol, '') track.download( filename=str(os.path.join( dirpath, config.Download.filename % { 'artists': ', '.join(track.artistsName()), 'title': title, 'codec': config.Download.codec, }, )), codec=config.Download.codec, bitrate_in_kbps=config.Download.bitrate, ) delay = random.randint( config.Download.min_delay, config.Download.max_delay, ) time.sleep(delay)