cli-program.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from yt_download import get_mp3_from_ut, get_info_yt_video
  2. from mp3tag import modified_id3
  3. from feed import CheckFeed
  4. from agent import send_audio
  5. import sys
  6. import time
  7. import validators
  8. import os
  9. import logging
  10. yt_feed = CheckFeed()
  11. videos = yt_feed.read()
  12. logging.basicConfig(
  13. format='%(asctime)s %(message)s',
  14. datefmt='%Y-%m-%d %H:%M:%S',
  15. level=logging.INFO)
  16. num_of_items = 5
  17. def print_video_list():
  18. print("The list of titles of new items in youtube channel:")
  19. if videos:
  20. for i in range(0, num_of_items):
  21. print(f"=== {i + 1}. {videos[i]['title']}")
  22. else:
  23. print("ERROR: Can't get any videos")
  24. print_video_list()
  25. while True:
  26. select = input("Insert num of video to convert or type command exit/link/list: ")
  27. if select == "exit":
  28. logging.info("Terminate the script")
  29. sys.exit()
  30. elif select == "list":
  31. print("\n==============================\n")
  32. print_video_list()
  33. elif select == "link":
  34. while True:
  35. link = input("Insert the valid link to youtube video: ")
  36. if validators.url(link):
  37. break
  38. video = get_info_yt_video(link)
  39. print(f"Video title is: {video['title']}")
  40. break
  41. else:
  42. try:
  43. video_index = int(select) - 1
  44. if 0 <= video_index < num_of_items:
  45. video = videos[video_index]
  46. break
  47. else:
  48. logging.warning(f"Number is not from range (1, {num_of_items}). Try again")
  49. except ValueError:
  50. logging.warning("ERROR! It's not integer. Try again")
  51. while True:
  52. answer = input("Type the tags for mp3 file. Format: artist|title ")
  53. tags = answer.split("|")
  54. if len(tags) == 2:
  55. print(f"----Artist: {tags[0]}\n----Title: {tags[1]}")
  56. if input("Is it right? y/n: ").lower() != "n":
  57. break
  58. else:
  59. logging.warning("This is not right string")
  60. while True:
  61. if get_info_yt_video(video['link'])['is_live']:
  62. logging.warning('This stream is on. Waiting for end')
  63. time.sleep(30 * 60)
  64. else:
  65. break
  66. logging.info("Start Downloading")
  67. mp3 = get_mp3_from_ut(video)
  68. if mp3:
  69. files = [f for f in os.listdir() if f.endswith(".mp3") and f.startswith(mp3["title"][:9])]
  70. if len(files) == 1:
  71. file_name = files[0]
  72. else:
  73. raise RuntimeError("Something bad happened")
  74. print(f"Video title is: {mp3['title']}")
  75. modified_id3(file_name, tags[0], tags[1])
  76. logging.info(f"----DONE!\n----File name: {file_name}")
  77. send_audio(file_name, tags[0], tags[1])
  78. logging.info("Uploading completed")
  79. if os.path.isfile(file_name):
  80. os.remove(file_name)
  81. logging.info(f"file {file_name} was removed")
  82. else:
  83. logging.warning("File doesn't exists! Remove all mp3 files in folder")
  84. dir_name = os.getcwd()
  85. files = os.listdir(dir_name)
  86. for file in files:
  87. if file.endswith(".mp3"):
  88. os.remove(os.path.join(dir_name, file))