main.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import asyncio
  2. import config
  3. import os
  4. from downloader import Downloader, Album, Track
  5. from pathlib import Path
  6. from utils import download_image, move, parse_json, add_zeros
  7. async def main(download: Album | Track):
  8. if not config.tmp_folder.exists():
  9. config.tmp_folder.mkdir()
  10. music_folder = Path(config.music_folder)
  11. album, tracks = await Downloader.download(download)
  12. if album is None:
  13. for path, track in tracks.items():
  14. track_title = config.track_filename_template.format(
  15. artist=track.artist,
  16. title=track.title,
  17. number=" "
  18. ).strip()
  19. await move(path, music_folder / track_title)
  20. else:
  21. album_folder_name = config.playlist_folder_template.format(
  22. artist=album.artist,
  23. title=album.title
  24. )
  25. album_folder = music_folder / album_folder_name
  26. if not album_folder.is_dir():
  27. album_folder.mkdir()
  28. tracks_count = len(str(len(tracks)))
  29. for path, track in tracks.items():
  30. track_title = config.track_filename_template.format(
  31. artist=track.artist,
  32. title=track.title,
  33. number=add_zeros(track.number, tracks_count)
  34. )
  35. await move(path, album_folder / track_title)
  36. cover = await download_image(album.cover)
  37. extension = str(cover).split(".")[-1]
  38. await move(cover, album_folder / ("cover." + extension))
  39. os.system(f'explorer "{album_folder}"')
  40. return album_folder
  41. if __name__ == "__main__":
  42. with open("download.json", "r", encoding="utf-8") as f:
  43. download = parse_json(f.read())
  44. asyncio.run(main(download))