-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
162 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# from searching_utils.spotify_to_youtube import get_url_without_api | ||
# get_url_without_api("https://open.spotify.com/intl-de/track/3ElbrKKlIkCHTB4hfFXzpb?si=c98ed9eeb77a4140") | ||
|
||
from searching_utils import query_to_youtube | ||
|
||
print(query_to_youtube.get_url("aposjd")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from enum import Enum | ||
|
||
class Platform(Enum): | ||
class Source(Enum): | ||
NO_URL = 0 | ||
TIK_TOK = 1 | ||
SPOTIFY = 2 | ||
YOUTUBE = 3 | ||
SOUND_CLOUD = 4 | ||
ANYTHING_ELSE = 5 | ||
UNKNOWN_SOURCE = 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class InvalidDiscordTokenError(Exception): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class SpotifyRequestError(Exception): | ||
pass | ||
|
||
class InvalidSpotifyCredentialsError(Exception): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
platform_handlers/music_platform_finder.py → platform_handlers/music_source_finder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
from urllib.parse import urlsplit | ||
from enums.platform import Platform | ||
from enums.source import Source | ||
|
||
async def find_platform(query_url: str) -> Platform: | ||
async def find_music_source(query_url: str) -> Source: | ||
split_url = urlsplit(query_url) | ||
hostname = split_url.hostname | ||
|
||
if not hostname: | ||
return Platform.NO_URL | ||
return Source.NO_URL | ||
|
||
elif "spotify" in hostname: | ||
return Platform.SPOTIFY | ||
return Source.SPOTIFY | ||
|
||
elif "tiktok" in hostname: | ||
return Platform.TIK_TOK | ||
return Source.TIK_TOK | ||
|
||
elif "youtube" in hostname or "youtu" in hostname: | ||
return Platform.YOUTUBE | ||
return Source.YOUTUBE | ||
|
||
elif "soundcloud" in hostname: | ||
return Platform.SOUND_CLOUD | ||
return Source.SOUND_CLOUD | ||
|
||
else: | ||
return Platform.ANYTHING_ELSE | ||
return Source.UNKNOWN_SOURCE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ python-dotenv | |
ytmusicapi | ||
peewee | ||
spotapi | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import ytmusicapi | ||
from yt_dlp import YoutubeDL | ||
|
||
def get_url(query: str) -> str: | ||
yt = ytmusicapi.YTMusic() | ||
results = yt.search(query) | ||
|
||
if results: | ||
video_id = results[0]["videoId"] | ||
return f"https://music.youtube.com/watch?v={video_id}" | ||
else: | ||
with YoutubeDL({"quiet": True}) as ydl: | ||
search_results = ydl.extract_info(f"ytsearch:{query}", download=False) | ||
if "entries" in search_results and search_results["entries"]: | ||
first_result = search_results["entries"][0] | ||
return f"https://www.youtube.com/watch?v={first_result['id']}" | ||
|
||
raise Exception("There was an error getting the url") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
from bs4 import BeautifulSoup | ||
import requests | ||
from spotdl import Spotdl | ||
from exceptions.spotify_exceptions import MissingSpotifyCredentialsError | ||
from searching_utils.query_to_youtube import get_url | ||
|
||
def get_url(spotify_url: str) -> str: | ||
client_id = os.getenv('SPOTIFY_CLIENT_ID') | ||
client_secret = os.getenv('SPOTIFY_CLIENT_SECRET') | ||
|
||
if client_id and client_secret: | ||
return get_url_with_credentials(spotify_url, client_id, client_secret) | ||
elif client_id or client_secret: | ||
raise MissingSpotifyCredentialsError("Both client ID and client secret are required.") | ||
else: | ||
return get_url_without_credentials(spotify_url) | ||
|
||
def get_url_without_credentials(spotify_url: str) -> str: | ||
response = requests.get(spotify_url) | ||
|
||
if response.status_code != 200: | ||
raise ValueError(f"Request failed with status code {response.status_code}") | ||
|
||
soup = BeautifulSoup(response.text, 'html.parser') | ||
|
||
meta_tag = soup.find('meta', {'property': 'og:description'}) | ||
|
||
if not meta_tag or 'content' not in meta_tag.attrs: | ||
raise ValueError("Could not find the 'og:description' meta tag") | ||
|
||
search_query = meta_tag['content'] | ||
|
||
return get_url(search_query) | ||
|
||
def get_url_with_credentials(spotify_url: str, client_id: str, client_secret: str) -> str: | ||
spotdl = Spotdl(client_id=client_id, client_secret=client_secret) | ||
song = spotdl.search([spotify_url]) | ||
return spotdl.get_download_urls(song)[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import configparser | ||
from discord import Bot | ||
|
||
async def apply_config(bot: Bot): | ||
config = configparser.ConfigParser() | ||
config.read('config.ini') | ||
|
||
ask_in_dms = config.getboolean('Bot', 'AskInDMs', fallback=False) | ||
admin_userid = config.getint('Admin', 'UserID', fallback=0) | ||
|
||
if ask_in_dms and admin_userid: | ||
user = await bot.fetch_user(admin_userid) | ||
|
||
dm_channel = await user.create_dm() | ||
|
||
messages = await dm_channel.history().flatten() | ||
|
||
for msg in messages: | ||
try: | ||
await msg.delete() | ||
except Exception as e: | ||
print(f"Skipped message due to error: {str(e)}") | ||
|
||
await user.send(f"Bot is ready and logged in as {bot.user.name}") |
Oops, something went wrong.