commit a4ff7cfe2da93866ff69f1647af3c3fc84d75f08 Author: csasq Date: Thu Aug 22 12:07:56 2024 +0300 Разработано MVP diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..86966d1 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1 @@ +from .main import run diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..cd6217a --- /dev/null +++ b/app/main.py @@ -0,0 +1,60 @@ +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) diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..c7b9a59 --- /dev/null +++ b/config/__init__.py @@ -0,0 +1 @@ +from .main import Main, Download, Yandex diff --git a/config/main.py b/config/main.py new file mode 100644 index 0000000..73b3d77 --- /dev/null +++ b/config/main.py @@ -0,0 +1,67 @@ +from configparser import RawConfigParser +import os + + +cwd = os.getcwd() + +config = RawConfigParser() +config.optionxform = str +config.read( + os.path.join( + cwd, + 'config.ini', + ) +) + + +class Main: + working_directory = config.get( + section='Main', + option='working_directory', + fallback=cwd, + ) + + +class Download: + dirname = config.get( + section='Download', + option='dirname', + fallback='music', + ) + filename = config.get( + section='Download', + option='filename', + fallback='%(artists)s — %(title)s.%(codec)s', + ) + escape_symbols = config.get( + section='Download', + option='escape_symbols', + fallback=r'\/:*?"<>|', + ) + codec = config.get( + section='Download', + option='codec', + fallback='mp3', + ) + bitrate = config.getint( + section='Download', + option='bitrate', + fallback=320, + ) + min_delay = config.getint( + section='Download', + option='min_delay', + fallback=0, + ) + max_delay = config.getint( + section='Download', + option='max_delay', + fallback=0, + ) + + +class Yandex: + token = config.get( + section='Yandex', + option='token', + ) diff --git a/main.py b/main.py new file mode 100644 index 0000000..540380a --- /dev/null +++ b/main.py @@ -0,0 +1,5 @@ +import app + + +if __name__ == '__main__': + app.run() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9818b80 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +yandex-music