-
-
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
25 changed files
with
529 additions
and
594 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
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,29 +1,18 @@ | ||
# Spoyt | ||
|
||
Spotify to YouTube; Discord and Guilded link converter. | ||
Discord bot that allows you to "convert" Spotify links to YouTube videos. | ||
|
||
## Usage | ||
|
||
Just send a message with share link from Spotify. Bot will automatically find | ||
the track in Spotify database, and search its name and artists in YouTube. | ||
If possible, it will try to delete your message, but you can disable it | ||
by permitting permissions. | ||
1. Invite the bot with this link: <https://discord.com/api/oauth2/authorize?client_id=948274806325903410&permissions=2147485696&scope=bot%20applications.commands>. | ||
|
||
Invite the bot by one of following links: | ||
- Discord: https://discord.com/api/oauth2/authorize?client_id=948274806325903410&permissions=3072&scope=bot | ||
- Guilded: https://www.guilded.gg/b/93177486-3a1d-4464-a202-1ddd6354844b | ||
1. Use `/track` or `/playlist` command to search. Bot will try to find the track or playlist (respectively) using Spotify API, and search it in YouTube (also using API). | ||
|
||
## Support | ||
|
||
You can join one of my servers (or both): | ||
### Note | ||
|
||
- Discord: [discord.gg/SRdmrPpf2z](https://discord.gg/SRdmrPpf2z) | ||
- Guilded: [guilded.gg/Anonymous-Canteen](https://guilded.gg/Anonymous-Canteen) | ||
YouTube searching currently applies only to `/track`. I'm currently inspecting YouTube API limiations. | ||
|
||
## How to run | ||
## Support | ||
|
||
Make sure you have Python `>=3.8` installed. | ||
``` | ||
[py|python|python3] -(O|OO)m Spoyt.[Discord|Guilded] | ||
``` | ||
You can join my server: <https://discord.gg/SRdmrPpf2z>. | ||
|
Empty file.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# -*- coding: utf-8 -*- | ||
from spotipy import Spotify, SpotifyException, SpotifyClientCredentials | ||
|
||
from Spoyt.exceptions import SpotifyNotFoundException, SpotifyUnreachableException | ||
from Spoyt.logger import log | ||
from Spoyt.settings import SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET | ||
|
||
|
||
class Track: | ||
def __init__(self, payload: dict) -> None: | ||
self.name: str = payload.get('name') | ||
self.track_id: str = payload.get('id') | ||
self.artists: list[str] = list(map( | ||
lambda a: a.get('name'), | ||
payload.get('artists', {}) | ||
)) | ||
self.release_date: str = payload.get('album', {}).get('release_date') | ||
self.cover_url: str = payload.get('album', {}).get('images', [{}])[0].get('url') | ||
|
||
@property | ||
def is_single_artist(self) -> bool: | ||
return len(self.artists) == 1 | ||
|
||
@property | ||
def track_url(self) -> str: | ||
return f'https://open.spotify.com/track/{self.track_id}' | ||
|
||
|
||
class User: | ||
def __init__(self, payload: dict) -> None: | ||
self.name: str = payload.get('display_name') | ||
self.id: str = payload.get('id') | ||
self.user_url: str = payload.get('external_urls', {}).get('spotify') | ||
self.avatar_url: str = payload.get('images', [{}])[-1].get('url') | ||
|
||
class Playlist: | ||
def __init__(self, payload: dict) -> None: | ||
self.name: str = payload.get('name') | ||
self.description: str = payload.get('description') | ||
self.playlist_id: str = payload.get('id') | ||
self.cover_url: str = payload.get('images', [{}])[0].get('url') | ||
self.tracks: list[Track] = list(map( | ||
lambda a: Track(a.get('track', {})), | ||
payload.get('tracks', {}).get('items') | ||
)) | ||
self.total_tracks: int = payload.get('tracks', {}).get('total') | ||
self.query_limit: int = payload.get('tracks', {}).get('limit') | ||
|
||
self.owner: User = search_user(payload.get('owner', {}).get('id')) | ||
|
||
@property | ||
def url(self) -> str: | ||
return f'https://open.spotify.com/playlist/{self.playlist_id}' | ||
|
||
@property | ||
def is_query_limited(self) -> bool: | ||
return len(self.tracks) == self.query_limit | ||
|
||
|
||
def url_to_id(url: str) -> str: | ||
""" | ||
Removes trailing parameters like share source, then extractd ID. | ||
For example this: "https://open.spotify.com/track/4cOdK2wGLETKBW3PvgPWqT?si=8a1b522f00744ee1", | ||
becomes: "4cOdK2wGLETKBW3PvgPWqT". | ||
""" | ||
return url.split('?')[0].split('&')[0].split('/')[-1] | ||
|
||
|
||
def spotify_connect() -> Spotify: | ||
return Spotify( | ||
auth_manager=SpotifyClientCredentials( | ||
client_id=SPOTIFY_CLIENT_ID, | ||
client_secret=SPOTIFY_CLIENT_SECRET | ||
) | ||
) | ||
|
||
# Search functions should not return `class Track` or `class Playlist` | ||
# because of checks if connections was successful during runtime. | ||
|
||
def search_track(track_id: str) -> Track: | ||
log.info(f'Searching track by ID "{track_id}"') | ||
try: | ||
track: dict | None = spotify_connect().track(track_id=track_id) | ||
except SpotifyException: | ||
raise SpotifyNotFoundException | ||
if not track: | ||
log.error('Spotify unreachable') | ||
raise SpotifyUnreachableException | ||
return Track(track) | ||
|
||
|
||
def search_playlist(playlist_id: str) -> Playlist: | ||
log.info(f'Searching playlist by ID "{playlist_id}"') | ||
try: | ||
playlist: dict | None = spotify_connect().playlist(playlist_id=playlist_id) | ||
except SpotifyException: | ||
raise SpotifyNotFoundException | ||
if not playlist: | ||
log.error('Spotify unreachable') | ||
raise SpotifyUnreachableException | ||
return Playlist(playlist) | ||
|
||
|
||
def search_user(user_id: str) -> User: | ||
log.info(f'Searching user by ID "{user_id}"') | ||
try: | ||
user: dict | None = spotify_connect().user(user=user_id) | ||
except SpotifyException: | ||
raise SpotifyNotFoundException | ||
if not user: | ||
log.error('Spotify unreachable') | ||
raise SpotifyUnreachableException | ||
return User(user) |
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,48 @@ | ||
# -*- coding: utf-8 -*- | ||
from json import loads as json_loads | ||
|
||
from requests import get as requests_get | ||
|
||
from Spoyt.exceptions import YouTubeException, YouTubeForbiddenException | ||
from Spoyt.logger import log | ||
from Spoyt.settings import YOUTUBE_API_KEY | ||
|
||
|
||
class YouTubeVideo: | ||
def __init__(self, payload: dict) -> None: | ||
item: dict = payload.get('items', [{}])[0] | ||
snippet: dict = item.get('snippet', {}) | ||
self.video_id: str = item.get('id', {}).get('videoId') | ||
self.title: str = snippet.get('title') | ||
self.description: str = snippet.get('description') | ||
self.published_date: str = snippet.get('publishTime', '')[:10] | ||
|
||
@property | ||
def video_link(self) -> str: | ||
return f'https://www.youtube.com/watch?v={self.video_id}' | ||
|
||
@property | ||
def video_thumbnail(self) -> str: | ||
return f'https://i.ytimg.com/vi/{self.video_id}/default.jpg' | ||
|
||
|
||
def search_video(query: str) -> YouTubeVideo: | ||
log.info(f'Searching YouTube: "{query}"') | ||
yt_r = requests_get( | ||
'https://www.googleapis.com/youtube/v3/search' | ||
'?key={}' | ||
'&part=snippet' | ||
'&maxResults=1' | ||
'&q={}'.format(YOUTUBE_API_KEY, query) | ||
) | ||
content = json_loads(yt_r.content) | ||
if (error_code := yt_r.status_code) == 200: | ||
video = YouTubeVideo(content) | ||
log.info(f'Found YouTube video "{video.title}" ({video.video_link})') | ||
elif error_code == 403: | ||
log.critical(content['error']['message']) | ||
raise YouTubeForbiddenException | ||
else: | ||
log.error(content['error']['message']) | ||
raise YouTubeException | ||
return video |
Oops, something went wrong.