summaryrefslogtreecommitdiff
path: root/test/test_record_upload.py
blob: a0c3faf0c2baa2bb1592adaa4d6ded156f602eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
import logging
import sys
import os.path
sys.path.extend([
    os.path.realpath(
        os.path.join(os.path.dirname(os.path.join(__file__)), '..', '..')
    )
])

import time

from src.home.api import WebAPIClient, RequestParams
from src.home.config import config
from src.home.media import SoundRecordClient
from src.home.util import parse_addr

logger = logging.getLogger(__name__)


# record callbacks
# ----------------

def record_error(info: dict, userdata: dict):
    node = userdata['node']
    # TODO


def record_finished(info: dict, fn: str, userdata: dict):
    logger.info('record finished: ' + str(info))

    node = userdata['node']
    api.upload_recording(fn, node, info['id'], int(info['start_time']), int(info['stop_time']))


# api client callbacks
# --------------------

def api_error_handler(exc, name, req: RequestParams):
    if name == 'upload_recording':
        logger.error('failed to upload recording, exception below')
        logger.exception(exc)

    else:
        logger.error(f'api call ({name}, params={req.params}) failed, exception below')
        logger.exception(exc)


def api_success_handler(response, name, req: RequestParams):
    if name == 'upload_recording':
        node = req.params['node']
        rid = req.params['record_id']

        logger.debug(f'successfully uploaded recording (node={node}, record_id={rid}), api response:' + str(response))

        # deleting temp file
        try:
            os.unlink(req.files['file'])
        except OSError as exc:
            logger.error(f'error while deleting temp file:')
            logger.exception(exc)

        record.forget(node, rid)


if __name__ == '__main__':
    config.load('test_record_upload')

    nodes = {}
    for name, addr in config['nodes'].items():
        nodes[name] = parse_addr(addr)
    record = SoundRecordClient(nodes,
                               error_handler=record_error,
                               finished_handler=record_finished,
                               download_on_finish=True)

    api = WebAPIClient()
    api.enable_async(error_handler=api_error_handler,
                     success_handler=api_success_handler)

    record_id = record.record('localhost', 3, {'node': 'localhost'})
    print(f'record_id: {record_id}')

    while True:
        try:
            time.sleep(0.1)
        except (KeyboardInterrupt, SystemExit):
            break