basic auth support

This commit is contained in:
boyska 2021-09-17 10:45:36 +02:00
parent 0d83a6fcd6
commit 9b10e525f0
3 changed files with 26 additions and 8 deletions

View file

@ -9,6 +9,7 @@ DEBUG = True
DB_URI = "sqlite:///techrec.db"
AUDIO_OUTPUT = "output/"
AUDIO_INPUT = "rec/"
AUDIO_INPUT_BASICAUTH = None # Could be a ("user", "pass") tuple instead
AUDIO_INPUT_FORMAT = "%Y-%m/%d/rec-%Y-%m-%d-%H-%M-%S.mp3"
AUDIO_OUTPUT_FORMAT = "techrec-%(startdt)s-%(endtime)s-%(name)s.mp3"
FORGE_TIMEOUT = 20

View file

@ -1,4 +1,3 @@
import aiohttp
import asyncio
import logging
import tempfile
@ -26,7 +25,8 @@ async def get_timefile_exact(time) -> str:
)
if path.startswith("http://") or path.startswith("https://"):
logger.info(f"downloading: {path}")
local = await download(path)
local = await download(path,
basic_auth=get_config()['AUDIO_INPUT_BASICAUTH'])
return local
return path

View file

@ -1,17 +1,21 @@
# -*- encoding: utf-8 -*-
import asyncio
import os
from typing import Optional
from typing import Optional, Tuple
from tempfile import mkdtemp
from logging import getLogger
import aiohttp # type: ignore
from .config_manager import get_config
CHUNK_SIZE = 2 ** 12
log = getLogger("http")
async def download(remote: str, staging: Optional[str] = None) -> str:
async def download(
remote: str,
staging: Optional[str] = None,
basic_auth: Optional[Tuple[str, str]] = None,
) -> str:
"""
This will download to AUDIO_STAGING the remote file and return the local
path of the downloaded file
@ -24,12 +28,25 @@ async def download(remote: str, staging: Optional[str] = None) -> str:
# used by techrec: rm -rf /tmp/techrec*
base = mkdtemp(prefix="techrec-", dir="/tmp")
local = os.path.join(base, filename)
async with aiohttp.ClientSession() as session:
session_args = {}
if basic_auth is not None:
session_args["auth"] = aiohttp.BasicAuth(
login=basic_auth[0], password=basic_auth[1], encoding="utf-8"
)
log.debug("Downloading %s with %s options", remote, ",".join(session_args.keys()))
async with aiohttp.ClientSession(**session_args) as session:
async with session.get(remote) as resp:
if resp.status != 200:
raise ValueError(
"Could not download %s: error %d" % (remote, resp.status)
)
with open(local, "wb") as f:
while True:
chunk = await resp.content.read(CHUNK_SIZE)
if not chunk:
break
f.write(chunk)
log.debug("Downloading %s complete", remote)
return local