run.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # GPL v3 or later
  2. import os
  3. import time
  4. from modules.ui import *
  5. from modules import youtube
  6. import subprocess
  7. center("Album Getter")
  8. # Main loop
  9. while True:
  10. c = input(" > ")
  11. if c in ["exit", "quit"]:
  12. break
  13. elif c:
  14. data_print = {"categories":["Name of Album", "Publisher", "Amount of Songs"],
  15. "size":[5,2,1],
  16. "data":[]}
  17. listis = []
  18. for i in youtube.search(c):
  19. if i.get("type") == "playlist":
  20. listis.append(i)
  21. name = i.get("title", "")
  22. publisher = i.get("author", "")
  23. amount = i.get("videoCount", "")
  24. data_print["data"].append([name, publisher, amount])
  25. #playlist = youtube.fetch_playlist(i.get("playlistId"))
  26. #print(playlist)
  27. while True:
  28. table(data_print)
  29. center("Choose an Album by number")
  30. alc = input(" >> ")
  31. if not alc:
  32. break
  33. try:
  34. alc = int(alc)
  35. playlist = youtube.fetch_playlist(listis[alc].get("playlistId"))
  36. playlistName = playlist.get("title", c)
  37. songs_print = {"categories":["Song", "Duration"],
  38. "size":[5,1],
  39. "data":[]}
  40. IDS = []
  41. names = []
  42. for song in playlist.get("videos", []):
  43. name = song.get("title", "")
  44. names.append(name)
  45. duration = song.get("lengthSeconds", 0)
  46. ID = song.get("videoId", "")
  47. IDS.append(ID)
  48. songs_print["data"].append([name, timestring(duration)])
  49. table(songs_print, False)
  50. center("Download Yes / No ?")
  51. yesno = input(" >>> ")
  52. print()
  53. if yesno.lower().startswith("y"):
  54. download_dir = os.path.expanduser("~/Downloads/"+playlistName+"/")
  55. try:
  56. os.makedirs(download_dir)
  57. except:
  58. pass
  59. for n, ID in enumerate(IDS):
  60. progress_bar(n, len(IDS), "Downloading "+names[n])
  61. youtube.insure_song(ID, download_dir+str(n+1)+" - "+names[n])
  62. subprocess.Popen(["xdg-open", download_dir])
  63. print()
  64. print()
  65. except Exception as e:
  66. print(e)