From d2171b2c1236a8f92a35c65e7a54fbf653132b8f Mon Sep 17 00:00:00 2001 From: timofey Date: Tue, 1 Nov 2022 18:35:10 +0000 Subject: [PATCH] new tor_prefix --- v6d3music/core/cache_url.py | 2 +- v6d3music/core/ffmpegnormalaudio.py | 5 +++-- v6d3music/core/real_url.py | 8 ++++++-- v6d3music/core/ytaudio.py | 3 ++- v6d3music/utils/sparq.py | 2 +- v6d3music/utils/speed_quotient.py | 2 +- v6d3music/utils/tor_extract.py | 5 ++++- v6d3music/utils/tor_prefix.py | 12 +++++++++--- 8 files changed, 27 insertions(+), 12 deletions(-) diff --git a/v6d3music/core/cache_url.py b/v6d3music/core/cache_url.py index 37eecff..3dd4432 100644 --- a/v6d3music/core/cache_url.py +++ b/v6d3music/core/cache_url.py @@ -24,7 +24,7 @@ async def cache_url(hurl: str, rurl: str, override: bool, tor: bool) -> None: tmp_path = cache_root / f'{hurl}.tmp.opus' args = [] if tor: - args.extend(tor_prefix) + args.extend(tor_prefix()) args.extend( [ 'ffmpeg', '-hide_banner', '-loglevel', 'warning', diff --git a/v6d3music/core/ffmpegnormalaudio.py b/v6d3music/core/ffmpegnormalaudio.py index 8cb33dc..50e62ef 100644 --- a/v6d3music/core/ffmpegnormalaudio.py +++ b/v6d3music/core/ffmpegnormalaudio.py @@ -17,8 +17,9 @@ class FFmpegNormalAudio(discord.FFmpegAudio): ): args = [] if tor: - args.extend([*tor_prefix[1:], executable]) - executable = tor_prefix[0] + _tor_prefix = tor_prefix() + args.extend([*_tor_prefix[1:], executable]) + executable = _tor_prefix[0] subprocess_kwargs = {'stdin': source if pipe else subprocess.DEVNULL, 'stderr': stderr} diff --git a/v6d3music/core/real_url.py b/v6d3music/core/real_url.py index c8a00bf..b3122b4 100644 --- a/v6d3music/core/real_url.py +++ b/v6d3music/core/real_url.py @@ -1,4 +1,5 @@ import asyncio +import os import subprocess from typing import Optional from adaas.cachedb import RemoteCache @@ -10,8 +11,11 @@ from v6d3music.utils.bytes_hash import bytes_hash from v6d3music.utils.tor_prefix import tor_prefix +adaas_available = bool(os.getenv('adaasurl')) + + async def real_url(url: str, override: bool, tor: bool) -> str: - if not tor: + if adaas_available and not tor: return await RemoteCache().real_url(url, override, tor) hurl: str = bytes_hash(url.encode()) if not override: @@ -21,7 +25,7 @@ async def real_url(url: str, override: bool, tor: bool) -> str: return curl args = [] if tor: - args.extend(tor_prefix) + args.extend(tor_prefix()) args.extend( [ 'youtube-dl', '--no-playlist', '-f', 'bestaudio', '-g', '--', url diff --git a/v6d3music/core/ytaudio.py b/v6d3music/core/ytaudio.py index a65ae61..8ebec4d 100644 --- a/v6d3music/core/ytaudio.py +++ b/v6d3music/core/ytaudio.py @@ -79,7 +79,7 @@ class YTAudio(discord.AudioSource): return self._durations.setdefault(url, '') if self.tor: - args = [*tor_prefix] + args = [*tor_prefix()] else: args = [] args += [ @@ -98,6 +98,7 @@ class YTAudio(discord.AudioSource): if code: pass else: + assert p.stdout is not None self._durations[url] = p.stdout.read().decode().strip().split('.')[0] def duration(self) -> str: diff --git a/v6d3music/utils/sparq.py b/v6d3music/utils/sparq.py index e48525e..3ad3216 100644 --- a/v6d3music/utils/sparq.py +++ b/v6d3music/utils/sparq.py @@ -3,5 +3,5 @@ import discord from v6d3music.utils.speed_quotient import speed_quotient -def sparq(options: str) -> float: +def sparq(options: str | None) -> float: return speed_quotient(options) * discord.opus.Encoder.FRAME_LENGTH / 1000 diff --git a/v6d3music/utils/speed_quotient.py b/v6d3music/utils/speed_quotient.py index b541b88..3a9a522 100644 --- a/v6d3music/utils/speed_quotient.py +++ b/v6d3music/utils/speed_quotient.py @@ -3,7 +3,7 @@ import re import discord -def speed_quotient(options: str) -> float: +def speed_quotient(options: str | None) -> float: options = options or '' options = ''.join(c for c in options if not c.isspace()) options += ',' diff --git a/v6d3music/utils/tor_extract.py b/v6d3music/utils/tor_extract.py index b7cbd98..2d71600 100644 --- a/v6d3music/utils/tor_extract.py +++ b/v6d3music/utils/tor_extract.py @@ -7,12 +7,14 @@ from v6d3music.utils.tor_prefix import tor_prefix async def tor_extract(params: dict, url: str, **kwargs): print(f'tor extracting {url}') + args = [*tor_prefix(), 'python', '-m', 'v6d3music.run-extract'] p = subprocess.Popen( - [*tor_prefix, 'python', '-m', 'v6d3music.run-extract'], + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True ) + assert p.stdin is not None p.stdin.write(f'{json.dumps(params)}\n') p.stdin.write(f'{json.dumps(url)}\n') p.stdin.write(f'{json.dumps(kwargs)}\n') @@ -21,4 +23,5 @@ async def tor_extract(params: dict, url: str, **kwargs): code = await asyncio.get_running_loop().run_in_executor(None, p.wait) if code: raise RuntimeError(code) + assert p.stdout is not None return json.loads(p.stdout.read()) diff --git a/v6d3music/utils/tor_prefix.py b/v6d3music/utils/tor_prefix.py index 5bdfde3..e004410 100644 --- a/v6d3music/utils/tor_prefix.py +++ b/v6d3music/utils/tor_prefix.py @@ -4,7 +4,13 @@ import os if (address := os.getenv('v6tor', None)) is not None: print('tor through torsocks') - tor_prefix = ['torsocks', '--address', address] + _tor_prefix = ['torsocks', '--address', address] else: - print('tor through torify') - tor_prefix = ['torify'] + print('tor unavailable') + _tor_prefix = None + + +def tor_prefix(): + if _tor_prefix is None: + raise ValueError('tor unavailable') + return _tor_prefix