From d71059cac28abda388fe7fa701339cc6dab2e52a Mon Sep 17 00:00:00 2001 From: lk2322 Date: Mon, 3 Jan 2022 17:54:53 +0500 Subject: [PATCH 1/4] Added support for Yandex Music --- musicbot/audiocontroller.py | 4 ++++ musicbot/linkutils.py | 27 ++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/musicbot/audiocontroller.py b/musicbot/audiocontroller.py index a9bdb302..9f5335ff 100644 --- a/musicbot/audiocontroller.py +++ b/musicbot/audiocontroller.py @@ -134,6 +134,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] diff --git a/musicbot/linkutils.py b/musicbot/linkutils.py index 1441dd1e..2508207b 100644 --- a/musicbot/linkutils.py +++ b/musicbot/linkutils.py @@ -30,13 +30,11 @@ def clean_sclink(track): async def convert_spotify(url): - if re.search(url_regex, url): result = url_regex.search(url) url = result.group(0) async with session.get(url) as response: - page = await response.text() soup = BeautifulSoup(page, 'html.parser') @@ -44,7 +42,24 @@ async def convert_spotify(url): 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) + + 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('слушать онлайн на Яндекс.Музыке', '') + title = title.replace('listen online on Yandex Music', '') + return title @@ -99,7 +114,7 @@ async def get_spotify_playlist(url): print("ERROR: Check spotify CLIENT_ID and SECRET") async with session.get(url) as response: - page = await response.text() + page = await response.text() soup = BeautifulSoup(page, 'html.parser') @@ -117,7 +132,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]))+") @@ -136,6 +150,7 @@ class Sites(Enum): Twitter = "Twitter" SoundCloud = "SoundCloud" Bandcamp = "Bandcamp" + Yandex = "Yandex" Custom = "Custom" Unknown = "Unknown" @@ -170,6 +185,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 From 69f8674d2afaf1079f2c8da4537bd45c789cdd3d Mon Sep 17 00:00:00 2001 From: lk2322 Date: Mon, 3 Jan 2022 22:18:46 +0500 Subject: [PATCH 2/4] Now Yandex Music will work with any ip. And fixed Spotify --- config/requirements.txt | 3 ++- musicbot/linkutils.py | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/config/requirements.txt b/config/requirements.txt index c4decc3a..ec3f1429 100644 --- a/config/requirements.txt +++ b/config/requirements.txt @@ -4,4 +4,5 @@ yt-dlp aiohttp asyncio beautifulsoup4 -spotipy \ No newline at end of file +spotipy +yandex-music \ No newline at end of file diff --git a/musicbot/linkutils.py b/musicbot/linkutils.py index 2508207b..0e6d5187 100644 --- a/musicbot/linkutils.py +++ b/musicbot/linkutils.py @@ -4,6 +4,8 @@ import aiohttp import spotipy from bs4 import BeautifulSoup +from yandex_music import Client + from config import config from spotipy.oauth2 import SpotifyClientCredentials @@ -18,7 +20,9 @@ "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+") session = aiohttp.ClientSession( - headers={'User-Agent': 'python-requests/2.20.0'}) + 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): @@ -32,7 +36,7 @@ def clean_sclink(track): async def convert_spotify(url): if re.search(url_regex, url): result = url_regex.search(url) - url = result.group(0) + url = result.group(0) + "&nd=1" async with session.get(url) as response: page = await response.text() @@ -51,16 +55,10 @@ async def convert_yandex(url): result = url_regex.search(url) url = result.group(0) - 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('слушать онлайн на Яндекс.Музыке', '') - title = title.replace('listen online on Yandex Music', '') - - return title + temp = url.split('/') + track = yandex_client.tracks(f'{temp[-1]}:{temp[-3]}')[0] + title = track.title + return title async def get_spotify_playlist(url): From c8d0d3229303f4de39b59bc26d89349d88143420 Mon Sep 17 00:00:00 2001 From: lk2322 Date: Mon, 3 Jan 2022 22:48:16 +0500 Subject: [PATCH 3/4] Now title along with the author --- musicbot/linkutils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/musicbot/linkutils.py b/musicbot/linkutils.py index 0e6d5187..9c6741e4 100644 --- a/musicbot/linkutils.py +++ b/musicbot/linkutils.py @@ -20,7 +20,8 @@ "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() @@ -57,7 +58,7 @@ async def convert_yandex(url): temp = url.split('/') track = yandex_client.tracks(f'{temp[-1]}:{temp[-3]}')[0] - title = track.title + title = f"""{track.title} {' '.join(map(lambda x: x.name, track.artists))}""" return title From f98c2e6e149307fd53aff92eab4c96847a8153a4 Mon Sep 17 00:00:00 2001 From: lk2322 Date: Wed, 5 Jan 2022 13:47:36 +0500 Subject: [PATCH 4/4] Run sync func in executor --- musicbot/linkutils.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/musicbot/linkutils.py b/musicbot/linkutils.py index 2671758d..2e68b468 100644 --- a/musicbot/linkutils.py +++ b/musicbot/linkutils.py @@ -1,3 +1,4 @@ +import asyncio import re from enum import Enum @@ -20,12 +21,11 @@ "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+") session = aiohttp.ClientSession( -headers={ + 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): if track.startswith("https://m."): track = track.replace("https://m.", "https://") @@ -58,7 +58,9 @@ async def convert_yandex(url): url = result.group(0) temp = url.split('/') - track = yandex_client.tracks(f'{temp[-1]}:{temp[-3]}')[0] + 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 @@ -114,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')