Fix proxy
All checks were successful
Deploy Service / deploy (push) Successful in 30s

This commit is contained in:
Mootfrost 2025-04-13 19:53:28 +03:00
parent df2480f0bd
commit 89b5c5cf1a
4 changed files with 18 additions and 12 deletions

View file

@ -2,11 +2,14 @@ import time
from app import config
from app.MusicProvider.auth import refresh_token, get_oauth_creds
from app.dependencies import get_session_context
from app.models import Track, User
from app.MusicProvider.Strategy import MusicProviderStrategy
from sqlalchemy import select, update
from yandex_music import ClientAsync, TracksList
from yandex_music.utils.request_async import Request
class YandexMusicStrategy(MusicProviderStrategy):
@ -29,8 +32,8 @@ class YandexMusicStrategy(MusicProviderStrategy):
token, expires_in = await refresh_token('https://oauth.yandex.com/token',
user.ymusic_auth['refresh_token'],
config.ymusic.encoded
)
config.ymusic.encoded,
config.proxy)
async with get_session_context() as session:
await session.execute(
update(User).where(User.id == self.user_id).values(spotify_auth=get_oauth_creds(token,
@ -41,7 +44,10 @@ class YandexMusicStrategy(MusicProviderStrategy):
return token
async def get_tracks(self, token) -> list[Track]:
client = await ClientAsync(token).init()
if config.proxy:
client = ClientAsync(token, request=Request(proxy_url=config.proxy))
else:
client = await ClientAsync(token).init()
liked: TracksList = await client.users_likes_tracks()
tracks = await client.tracks([x.id for x in liked.tracks[:5]])
print(tracks[0])

View file

@ -11,7 +11,7 @@ def get_oauth_creds(token, refresh_token, expires_in):
}
async def refresh_token(endpoint, refresh_token, creds):
async def refresh_token(endpoint, refresh_token, creds, proxy=None):
token_headers = {
"Content-Type": "application/x-www-form-urlencoded",
'Authorization': 'Basic ' + creds
@ -20,7 +20,7 @@ async def refresh_token(endpoint, refresh_token, creds):
"grant_type": "refresh_token",
"refresh_token": refresh_token
}
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(proxy=proxy) as session:
resp = await session.post(endpoint, data=token_data, headers=token_headers)
resp = await resp.json()
return resp['access_token'], resp['expires_in']

View file

@ -38,7 +38,7 @@ async def link_exception_handler(request: Request, exc: LinkException):
return FileResponse('app/static/error.html', media_type='text/html')
async def code_to_token(code: str, uri: str, creds: OauthCreds) -> tuple[str, str, int]:
async def code_to_token(code: str, uri: str, creds: OauthCreds, proxy=None) -> tuple[str, str, int]:
token_headers = {
"Authorization": "Basic " + creds.encoded,
"Content-Type": "application/x-www-form-urlencoded"
@ -48,7 +48,7 @@ async def code_to_token(code: str, uri: str, creds: OauthCreds) -> tuple[str, st
"code": code,
"redirect_uri": creds.redirect
}
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(proxy=proxy) as session:
resp = await session.post(uri, data=token_data, headers=token_headers)
resp = await resp.json()
if 'access_token' not in resp:
@ -96,7 +96,7 @@ async def spotify_callback(code: str, state: str, session: AsyncSession = Depend
@app.get('/ym_callback')
async def ym_callback(state: str, code: str, cid: str, session: AsyncSession = Depends(get_session)):
user_id = get_decoded_id(state)
token, refresh_token, expires_in = await code_to_token(code, 'https://oauth.yandex.com/token', config.ymusic)
token, refresh_token, expires_in = await code_to_token(code, 'https://oauth.yandex.com/token', config.ymusic, config.proxy)
creds = get_oauth_creds(token, refresh_token, expires_in)
user = await session.get(User, user_id)
if user:

View file

@ -10,8 +10,8 @@ from app.config import config
ytmusic = YTMusic('oauth.json',
oauth_credentials=OAuthCredentials(client_id=config.yt.client_id, client_secret=config.yt.client_secret))
if config.proxy:
ytmusic.proxies = {'http': config.proxy, 'https': config.proxy}
# if config.proxy:
# ytmusic.proxies = {'http': config.proxy, 'https': config.proxy}
def name_to_youtube(name: str):
@ -26,8 +26,8 @@ def _download(yt_id: str, directory: str):
'quiet': True,
'outtmpl': os.path.join(directory, 'dl.%(ext)s'),
}
if config.proxy:
params['proxy'] = config.proxy
# if config.proxy:
# params['proxy'] = config.proxy
with YoutubeDL(params) as ydl:
return ydl.extract_info(yt_id)