utils.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from youtubesearchpython import VideosSearch
  2. def on_ready_print(self):
  3. print("---------")
  4. print("Logged as")
  5. print(self.user.name)
  6. print(self.user.id)
  7. print("---------")
  8. async def receive_message_then_send(message, received: str, to_send=""):
  9. if message.content == received:
  10. if to_send:
  11. await message.channel.send(to_send)
  12. return True
  13. def get_commands_from_file(filename: str) -> tuple:
  14. with open(filename, 'r') as file:
  15. _help_commands = tuple((command.split('\n')[0] for command in file))
  16. return _help_commands
  17. def message_is_song_name(message):
  18. return message.content.split(' ')[0] == '!song'
  19. def _get_song_name_from_message(message):
  20. parsed_msg = message.content.split(' ')
  21. if len(parsed_msg) == 1:
  22. raise NameError("Song name can't be empty!")
  23. return ''.join(message.content.split(' ')[1:])
  24. def _get_movie_id(videos_search):
  25. return videos_search.result()['result'][0]['id']
  26. def get_video_url_by_song_name(message):
  27. url = 'https://www.youtube.com/watch?v='
  28. song_name = _get_song_name_from_message(message)
  29. videos_search = VideosSearch(song_name, limit=1)
  30. video_id = _get_movie_id(videos_search)
  31. return url + video_id