main.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import asyncio
  2. import config
  3. import genius
  4. import segmenter
  5. import utils
  6. from utils import os
  7. import youtube
  8. youtube_url = "https://www.youtube.com/playlist?list=PL6HdtK-ZQcnDWAMVIRXj-_bcBb0QxVQXf"
  9. genius_url = "https://genius.com/albums/Eminem/The-eminem-show"
  10. try:
  11. youtube_url
  12. except NameError:
  13. youtube_url = None
  14. try:
  15. genius_url
  16. except NameError:
  17. genius_url = None
  18. async def main():
  19. hash = utils.hash(youtube_url)
  20. path = os.mkdirs(os.path_join("temp", hash))
  21. tasks = []
  22. intervals = None
  23. youtube_filename = None
  24. if youtube_url:
  25. youtube_filename = hash + ".mp3"
  26. tasks, intervals = await youtube.download(youtube_url, path, youtube_filename)
  27. if genius_url is not None:
  28. task = asyncio.create_task(genius.album(genius_url, path))
  29. tasks.append(task)
  30. tasks_result = await asyncio.gather(*tasks)
  31. if intervals:
  32. await segmenter.slice(path, youtube_filename, intervals)
  33. if genius_url is None:
  34. return
  35. album = tasks_result[-1]
  36. playlist_folder = utils.format(config.playlist_folder_template, album)
  37. dist = os.mkdirs(os.path_join(config.music_folder, playlist_folder))
  38. cover = os.path_join(path, "cover.jpg")
  39. if not os.path_exists(cover):
  40. await utils.wget(album["cover_url"], cover)
  41. await os.copy_folder(path, dist, [youtube_filename])
  42. await utils.set_meta_to_albumfolder(album, dist)
  43. loop = asyncio.get_event_loop()
  44. loop.run_until_complete(main())