Skip to content

Commit

Permalink
Adjust method for new calls
Browse files Browse the repository at this point in the history
  • Loading branch information
EricTendian committed Nov 9, 2023
1 parent 797d5b4 commit d0073c5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
38 changes: 35 additions & 3 deletions app/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,40 @@
import requests


def call(method: str, path: str, json: dict | None = None, params: dict | None = None):
def call(method: str, path: str, **kwargs):
"""Constructs and sends a :class:`Request <Request>`.
:param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
:param path: Path for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How many seconds to wait for the server to send data
before giving up, as a float, or a :ref:`(connect timeout, read
timeout) <timeouts>` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
:return: JSON response body
:rtype: dict
"""

api_key = os.getenv("API_KEY")
if api_key:
headers = {"Authorization": f"Bearer {api_key}"}
Expand All @@ -16,8 +49,7 @@ def call(method: str, path: str, json: dict | None = None, params: dict | None =
url=f"{os.getenv('API_BASE_URL')}/{path}",
timeout=5,
headers=headers,
json=json,
params=params,
**kwargs,
)
if r.status_code >= 400:
try:
Expand Down
40 changes: 18 additions & 22 deletions bin/broadcastify-calls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import argparse
import json
import logging
import os
import sys
Expand All @@ -16,8 +17,7 @@
# Load the .env file of our choice if specified before the regular .env can load
load_dotenv(os.getenv("ENV"))

from app import storage
from app.worker import transcribe_task
from app import api_client

USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.64"
TAGS = {
Expand Down Expand Up @@ -192,30 +192,26 @@ def process_call(call: dict, short_name: str, jar):
logging.debug(f"Downloading {url}")
with requests.get(url, cookies=jar, stream=True, timeout=10) as r:
r.raise_for_status()
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=f".{extension}")
for chunk in r.iter_content(chunk_size=1024 * 1024):
audio_file.write(chunk)
audio_file.close()
with tempfile.NamedTemporaryFile(
suffix=f".json"
) as metadata_file, tempfile.NamedTemporaryFile(
suffix=f".{extension}"
) as audio_file:
metadata_file.write(bytes(json.dumps(metadata), encoding="utf-8"))
metadata_file.flush()

start_time = datetime.fromtimestamp(metadata["start_time"], tz=pytz.UTC)
uploaded_audio_path = (
start_time.strftime("%Y/%m/%d/%H/%Y%m%d_%H%M%S")
+ f"_{metadata['short_name']}_{metadata['talkgroup']}.{extension}"
)
for chunk in r.iter_content(chunk_size=1024 * 1024):
audio_file.write(chunk)
audio_file.flush()

logging.debug(f"Re-uploading to {uploaded_audio_path}")
audio_url = storage.upload_file(audio_file.name, uploaded_audio_path)
os.unlink(audio_file.name)
api_client.call(
"POST",
"calls",
files={"call_json": metadata_file, "call_audio": audio_file},
)

transcribe_task.apply_async(
queue="transcribe",
kwargs={
"metadata": metadata,
"audio_url": audio_url,
},
)
logging.info(
f"Queued call on '{call['display']}' (TG {call['call_tg']}) for transcription - {audio_url}"
f"Queued call on '{call['display']}' (TG {call['call_tg']}) for transcription - {url}"
)


Expand Down

0 comments on commit d0073c5

Please sign in to comment.