diff --git a/scripts/utils/reporting.py b/scripts/utils/reporting.py index 5763d39..336010b 100644 --- a/scripts/utils/reporting.py +++ b/scripts/utils/reporting.py @@ -6,6 +6,8 @@ import os import sqlite3 import subprocess import tempfile +import io +import soundfile from time import sleep import requests @@ -169,15 +171,22 @@ def bird_weather(file: ParseFileName, detections: [Detection]): if conf['BIRDWEATHER_ID'] == "": return if detections: + try: + data, samplerate = soundfile.read(file.file_name) + buf = io.BytesIO() + soundfile.write(buf, data, samplerate, format='FLAC') + flac_data = buf.getvalue() + except Exception as e: + log.error("Error during FLAC conversion: %s", e) + return + gzip_flac_data = gzip.compress(flac_data) + # POST soundscape to server soundscape_url = (f'https://app.birdweather.com/api/v1/stations/' f'{conf["BIRDWEATHER_ID"]}/soundscapes?timestamp={file.iso8601}') - with open(file.file_name, 'rb') as f: - wav_data = f.read() - gzip_wav_data = gzip.compress(wav_data) try: - response = requests.post(url=soundscape_url, data=gzip_wav_data, timeout=30, + response = requests.post(url=soundscape_url, data=gzip_flac_data, timeout=30, headers={'Content-Type': 'application/octet-stream', 'Content-Encoding': 'gzip'}) log.info("Soundscape POST Response Status - %d", response.status_code) sdata = response.json()