Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Yandex Music (and fix Spotify) #82

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ yt-dlp
aiohttp
asyncio
beautifulsoup4
spotipy
spotipy
yandex-music
4 changes: 4 additions & 0 deletions musicbot/audiocontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ async def process_song(self, track):
title = await linkutils.convert_spotify(track)
track = self.search_youtube(title)

if host == linkutils.Sites.Yandex:
title = await linkutils.convert_yandex(track)
track = self.search_youtube(title)

if host == linkutils.Sites.YouTube:
track = track.split("&list=")[0]

Expand Down
31 changes: 24 additions & 7 deletions musicbot/linkutils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import asyncio
import re
from enum import Enum

import aiohttp
import spotipy
from bs4 import BeautifulSoup
from yandex_music import Client

from config import config
from spotipy.oauth2 import SpotifyClientCredentials

Expand All @@ -18,7 +21,9 @@
"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")

session = aiohttp.ClientSession(
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'})
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'})
yandex_client = Client()


def clean_sclink(track):
Expand All @@ -30,26 +35,36 @@ def clean_sclink(track):


async def convert_spotify(url):

if re.search(url_regex, url):
result = url_regex.search(url)

if "?si=" in url:
url = result.group(0) + "&nd=1"

async with session.get(url) as response:

page = await response.text()
soup = BeautifulSoup(page, 'html.parser')

title = soup.find('title')
title = title.string
title = title.replace('- song by', '')
title = title.replace('| Spotify', '')

return title


async def convert_yandex(url):
if re.search(url_regex, url):
result = url_regex.search(url)
url = result.group(0)

temp = url.split('/')
loop = asyncio.get_running_loop()
track = await loop.run_in_executor(None, yandex_client.tracks, f'{temp[-1]}:{temp[-3]}')
track = track[0]
title = f"""{track.title} {' '.join(map(lambda x: x.name, track.artists))}"""
return title


async def get_spotify_playlist(url):
"""Return Spotify_Playlist class"""

Expand Down Expand Up @@ -101,7 +116,7 @@ async def get_spotify_playlist(url):
print("ERROR: Check spotify CLIENT_ID and SECRET")

async with session.get(url + "&nd=1") as response:
page = await response.text()
page = await response.text()

soup = BeautifulSoup(page, 'html.parser')

Expand All @@ -119,7 +134,6 @@ async def get_spotify_playlist(url):


def get_url(content):

regex = re.compile(
"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")

Expand All @@ -138,6 +152,7 @@ class Sites(Enum):
Twitter = "Twitter"
SoundCloud = "SoundCloud"
Bandcamp = "Bandcamp"
Yandex = "Yandex"
Custom = "Custom"
Unknown = "Unknown"

Expand Down Expand Up @@ -172,6 +187,8 @@ def identify_url(url):

if "https://twitter.com/" in url:
return Sites.Twitter
if "https://music.yandex.ru/" in url:
return Sites.Yandex

if url.lower().endswith(config.SUPPORTED_EXTENSIONS):
return Sites.Custom
Expand Down