new tor_prefix

This commit is contained in:
AF 2022-11-01 18:35:10 +00:00
parent 40145ae3da
commit d2171b2c12
8 changed files with 27 additions and 12 deletions

View File

@ -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' tmp_path = cache_root / f'{hurl}.tmp.opus'
args = [] args = []
if tor: if tor:
args.extend(tor_prefix) args.extend(tor_prefix())
args.extend( args.extend(
[ [
'ffmpeg', '-hide_banner', '-loglevel', 'warning', 'ffmpeg', '-hide_banner', '-loglevel', 'warning',

View File

@ -17,8 +17,9 @@ class FFmpegNormalAudio(discord.FFmpegAudio):
): ):
args = [] args = []
if tor: if tor:
args.extend([*tor_prefix[1:], executable]) _tor_prefix = tor_prefix()
executable = tor_prefix[0] args.extend([*_tor_prefix[1:], executable])
executable = _tor_prefix[0]
subprocess_kwargs = {'stdin': source if pipe else subprocess.DEVNULL, 'stderr': stderr} subprocess_kwargs = {'stdin': source if pipe else subprocess.DEVNULL, 'stderr': stderr}

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import os
import subprocess import subprocess
from typing import Optional from typing import Optional
from adaas.cachedb import RemoteCache 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 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: 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) return await RemoteCache().real_url(url, override, tor)
hurl: str = bytes_hash(url.encode()) hurl: str = bytes_hash(url.encode())
if not override: if not override:
@ -21,7 +25,7 @@ async def real_url(url: str, override: bool, tor: bool) -> str:
return curl return curl
args = [] args = []
if tor: if tor:
args.extend(tor_prefix) args.extend(tor_prefix())
args.extend( args.extend(
[ [
'youtube-dl', '--no-playlist', '-f', 'bestaudio', '-g', '--', url 'youtube-dl', '--no-playlist', '-f', 'bestaudio', '-g', '--', url

View File

@ -79,7 +79,7 @@ class YTAudio(discord.AudioSource):
return return
self._durations.setdefault(url, '') self._durations.setdefault(url, '')
if self.tor: if self.tor:
args = [*tor_prefix] args = [*tor_prefix()]
else: else:
args = [] args = []
args += [ args += [
@ -98,6 +98,7 @@ class YTAudio(discord.AudioSource):
if code: if code:
pass pass
else: else:
assert p.stdout is not None
self._durations[url] = p.stdout.read().decode().strip().split('.')[0] self._durations[url] = p.stdout.read().decode().strip().split('.')[0]
def duration(self) -> str: def duration(self) -> str:

View File

@ -3,5 +3,5 @@ import discord
from v6d3music.utils.speed_quotient import speed_quotient 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 return speed_quotient(options) * discord.opus.Encoder.FRAME_LENGTH / 1000

View File

@ -3,7 +3,7 @@ import re
import discord import discord
def speed_quotient(options: str) -> float: def speed_quotient(options: str | None) -> float:
options = options or '' options = options or ''
options = ''.join(c for c in options if not c.isspace()) options = ''.join(c for c in options if not c.isspace())
options += ',' options += ','

View File

@ -7,12 +7,14 @@ from v6d3music.utils.tor_prefix import tor_prefix
async def tor_extract(params: dict, url: str, **kwargs): async def tor_extract(params: dict, url: str, **kwargs):
print(f'tor extracting {url}') print(f'tor extracting {url}')
args = [*tor_prefix(), 'python', '-m', 'v6d3music.run-extract']
p = subprocess.Popen( p = subprocess.Popen(
[*tor_prefix, 'python', '-m', 'v6d3music.run-extract'], args,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
text=True text=True
) )
assert p.stdin is not None
p.stdin.write(f'{json.dumps(params)}\n') p.stdin.write(f'{json.dumps(params)}\n')
p.stdin.write(f'{json.dumps(url)}\n') p.stdin.write(f'{json.dumps(url)}\n')
p.stdin.write(f'{json.dumps(kwargs)}\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) code = await asyncio.get_running_loop().run_in_executor(None, p.wait)
if code: if code:
raise RuntimeError(code) raise RuntimeError(code)
assert p.stdout is not None
return json.loads(p.stdout.read()) return json.loads(p.stdout.read())

View File

@ -4,7 +4,13 @@ import os
if (address := os.getenv('v6tor', None)) is not None: if (address := os.getenv('v6tor', None)) is not None:
print('tor through torsocks') print('tor through torsocks')
tor_prefix = ['torsocks', '--address', address] _tor_prefix = ['torsocks', '--address', address]
else: else:
print('tor through torify') print('tor unavailable')
tor_prefix = ['torify'] _tor_prefix = None
def tor_prefix():
if _tor_prefix is None:
raise ValueError('tor unavailable')
return _tor_prefix