From 4ba03ff8bc245749d5203ef4f13849f01a21f687 Mon Sep 17 00:00:00 2001 From: Rafael Vargas Date: Fri, 24 Dec 2021 19:05:03 -0400 Subject: [PATCH] Adding new classes to upgrade the music code organization --- config/config.py | 5 ++- main.py | 2 +- {vulkan => vulkanbot}/ErrorHandler.py | 0 {vulkan => vulkanbot}/commands/Phrases.py | 4 +- {vulkan => vulkanbot}/commands/Warframe.py | 0 vulkanbot/commands/__init__.py | 0 {vulkan => vulkanbot}/general/Control.py | 0 {vulkan => vulkanbot}/general/Filter.py | 0 vulkanbot/general/__init__.py | 0 vulkanbot/music/Downloader.py | 13 +++++++ {vulkan => vulkanbot}/music/Music.py | 9 +++-- vulkanbot/music/Playlist.py | 9 +++++ vulkanbot/music/Searcher.py | 43 ++++++++++++++++++++++ vulkanbot/music/Song.py | 12 ++++++ vulkanbot/music/Spotify.py | 22 +++++++++++ vulkanbot/music/Types.py | 21 +++++++++++ vulkanbot/music/Youtube.py | 8 ++++ 17 files changed, 140 insertions(+), 8 deletions(-) rename {vulkan => vulkanbot}/ErrorHandler.py (100%) rename {vulkan => vulkanbot}/commands/Phrases.py (94%) rename {vulkan => vulkanbot}/commands/Warframe.py (100%) create mode 100644 vulkanbot/commands/__init__.py rename {vulkan => vulkanbot}/general/Control.py (100%) rename {vulkan => vulkanbot}/general/Filter.py (100%) create mode 100644 vulkanbot/general/__init__.py create mode 100644 vulkanbot/music/Downloader.py rename {vulkan => vulkanbot}/music/Music.py (98%) create mode 100644 vulkanbot/music/Playlist.py create mode 100644 vulkanbot/music/Searcher.py create mode 100644 vulkanbot/music/Song.py create mode 100644 vulkanbot/music/Spotify.py create mode 100644 vulkanbot/music/Types.py create mode 100644 vulkanbot/music/Youtube.py diff --git a/config/config.py b/config/config.py index e531c56..98c3197 100644 --- a/config/config.py +++ b/config/config.py @@ -4,10 +4,11 @@ CETUS_API = dotenv_values('.env')['CETUS_API'] BOT_TOKEN = dotenv_values('.env')['BOT_TOKEN'] SPOTIFY_ID = dotenv_values('.env')['SPOTIFY_ID'] SPOTIFY_SECRET = dotenv_values('.env')['SPOTIFY_SECRET'] +SECRET_MESSAGE = dotenv_values('.env')['SECRET_MESSAGE'] BOT_PREFIX = '!' -INITIAL_EXTENSIONS = {'vulkan.commands.Phrases', 'vulkan.commands.Warframe', - 'vulkan.general.Filter', 'vulkan.general.Control', 'vulkan.music.Music'} +INITIAL_EXTENSIONS = {'vulkanbot.commands.Phrases', 'vulkanbot.commands.Warframe', + 'vulkanbot.general.Filter', 'vulkanbot.general.Control', 'vulkanbot.music.Music'} VC_TIMEOUT = 600 # seconds VC_TIMEOUT_DEFAULT = True diff --git a/main.py b/main.py index 7723ce8..608f10e 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import discord from config import config from discord.ext import commands -from vulkan.ErrorHandler import ErrorHandler +from vulkanbot.ErrorHandler import ErrorHandler intents = discord.Intents.default() diff --git a/vulkan/ErrorHandler.py b/vulkanbot/ErrorHandler.py similarity index 100% rename from vulkan/ErrorHandler.py rename to vulkanbot/ErrorHandler.py diff --git a/vulkan/commands/Phrases.py b/vulkanbot/commands/Phrases.py similarity index 94% rename from vulkan/commands/Phrases.py rename to vulkanbot/commands/Phrases.py index b9513b9..71bd446 100644 --- a/vulkan/commands/Phrases.py +++ b/vulkanbot/commands/Phrases.py @@ -1,6 +1,7 @@ import requests import json import discord +from config import config from discord.ext import commands from random import random as rand @@ -47,9 +48,8 @@ class Phrases(commands.Cog): async def calculate_rgn(self, ctx): x = rand() - print(x) if x < 0.15: - await ctx.send('Se leu seu cu é meu\nBy: Minha Pica') + await ctx.send(config.SECRET_MESSAGE) return True else: return False diff --git a/vulkan/commands/Warframe.py b/vulkanbot/commands/Warframe.py similarity index 100% rename from vulkan/commands/Warframe.py rename to vulkanbot/commands/Warframe.py diff --git a/vulkanbot/commands/__init__.py b/vulkanbot/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/vulkan/general/Control.py b/vulkanbot/general/Control.py similarity index 100% rename from vulkan/general/Control.py rename to vulkanbot/general/Control.py diff --git a/vulkan/general/Filter.py b/vulkanbot/general/Filter.py similarity index 100% rename from vulkan/general/Filter.py rename to vulkanbot/general/Filter.py diff --git a/vulkanbot/general/__init__.py b/vulkanbot/general/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/vulkanbot/music/Downloader.py b/vulkanbot/music/Downloader.py new file mode 100644 index 0000000..fd620d8 --- /dev/null +++ b/vulkanbot/music/Downloader.py @@ -0,0 +1,13 @@ +class Downloader(): + """Download music source from Youtube with a music name""" + + def __init__(self) -> None: + pass + + def download(self, track_name: str) -> str: + if type(track_name) != str: + return + + def download_many(self, track_list: list) -> list: + if type(track_list) != list: + return diff --git a/vulkan/music/Music.py b/vulkanbot/music/Music.py similarity index 98% rename from vulkan/music/Music.py rename to vulkanbot/music/Music.py index f80e03a..7a88173 100644 --- a/vulkan/music/Music.py +++ b/vulkanbot/music/Music.py @@ -1,10 +1,10 @@ import discord -from discord import colour -from discord.embeds import Embed from discord.ext import commands -from discord.ext.commands.core import command from youtube_dl import YoutubeDL +from vulkanbot.music.Downloader import Downloader +from vulkanbot.music.Searcher import Searcher + colours = { 'red': 0xDC143C, 'green': 0x00FF7F, @@ -15,6 +15,9 @@ colours = { class Music(commands.Cog): def __init__(self, client): + self.__searcher = Searcher() + self.__downloader = Downloader() + self.client = client self.is_playing = False self.repetingOne = False diff --git a/vulkanbot/music/Playlist.py b/vulkanbot/music/Playlist.py new file mode 100644 index 0000000..e3e68b9 --- /dev/null +++ b/vulkanbot/music/Playlist.py @@ -0,0 +1,9 @@ +class Playlist(): + """Class to manage and control the musics to play""" + + def __init__(self) -> None: + self.queue = [] + self.history = [] + + def next_music(): + pass diff --git a/vulkanbot/music/Searcher.py b/vulkanbot/music/Searcher.py new file mode 100644 index 0000000..9fb9a01 --- /dev/null +++ b/vulkanbot/music/Searcher.py @@ -0,0 +1,43 @@ +from vulkanbot.music.Types import Provider +from vulkanbot.music.Spotify import SpotifySearch +from vulkanbot.music.Youtube import YoutubeSearch + + +class Searcher(): + """Turn the user input into list of musics names, support youtube and spotify""" + + def __init__(self) -> None: + self.__Youtube = YoutubeSearch() + self.__Spotify = SpotifySearch() + print(f'Spotify Connected: {self.__Spotify.connect()}') + + def search(self, music: str) -> list: + """Return a list with the track name of a music or playlist""" + url_type = self.__identify_source(music) + + if url_type == Provider.Name: + return [music] + + elif url_type == Provider.YouTube: + musics = self.__Youtube.search(music) + return musics + + elif url_type == Provider.Spotify: + musics = self.__Spotify.search(music) + return musics + + def __identify_source(self, music): + if 'http' not in music: + return Provider.Name + + if "https://www.youtu" in music or "https://youtu.be" in music: + return Provider.YouTube + + if "https://open.spotify.com/track" in music: + return Provider.Spotify + + if "https://open.spotify.com/playlist" in music or "https://open.spotify.com/album" in music: + return Provider.Spotify_Playlist + + # If no match + return Provider.Unknown diff --git a/vulkanbot/music/Song.py b/vulkanbot/music/Song.py new file mode 100644 index 0000000..7287dca --- /dev/null +++ b/vulkanbot/music/Song.py @@ -0,0 +1,12 @@ +class Song(): + """Deal with information of a song""" + + def __init__(self, source) -> None: + self.__source = source + self.__get_info() + + def __get_info(self): + pass + + def info(): + """Return the compiled info of this song""" diff --git a/vulkanbot/music/Spotify.py b/vulkanbot/music/Spotify.py new file mode 100644 index 0000000..f9dda8a --- /dev/null +++ b/vulkanbot/music/Spotify.py @@ -0,0 +1,22 @@ +import spotipy +from spotipy.oauth2 import SpotifyClientCredentials +from config import config + + +class SpotifySearch(): + """Search and return musics names from Spotify""" + + def __init__(self) -> None: + pass + + def connect(self) -> bool: + try: + # Initialize the connection with Spotify API + self.__sp_api = spotipy.Spotify(auth_manager=SpotifyClientCredentials( + client_id=config.SPOTIFY_ID, client_secret=config.SPOTIFY_SECRET)) + return True + except: + return False + + def search(self, music) -> list: + pass diff --git a/vulkanbot/music/Types.py b/vulkanbot/music/Types.py new file mode 100644 index 0000000..c30a349 --- /dev/null +++ b/vulkanbot/music/Types.py @@ -0,0 +1,21 @@ +from enum import Enum + + +class Provider(Enum): + """Store Enum Types of the Providers""" + Spotify = "Spotify" + Spotify_Playlist = "Spotify Playlist" + YouTube = "YouTube" + Name = 'Track Name' + Unknown = "Unknown" + + +class Playlist_Types(Enum): + Spotify_Playlist = "Spotify Playlist" + YouTube_Playlist = "YouTube Playlist" + Unknown = "Unknown" + + +class Origins(Enum): + Default = "Default" + Playlist = "Playlist" diff --git a/vulkanbot/music/Youtube.py b/vulkanbot/music/Youtube.py new file mode 100644 index 0000000..753b6d4 --- /dev/null +++ b/vulkanbot/music/Youtube.py @@ -0,0 +1,8 @@ +class YoutubeSearch(): + """Search for tracks in youtube""" + + def __init__(self) -> None: + pass + + def search(self, track): + pass