diff --git a/program/serializers.py b/program/serializers.py index a8b85bba2bc58648d83b35973addd2bcbf01f426..7e9e8af40a3adee5c4a6bb2a12811d2c7fbe00d7 100644 --- a/program/serializers.py +++ b/program/serializers.py @@ -453,6 +453,7 @@ class NoteSerializer(serializers.ModelSerializer): timeslot = serializers.PrimaryKeyRelatedField(queryset=TimeSlot.objects.all()) host = serializers.PrimaryKeyRelatedField(queryset=Host.objects.all()) thumbnails = serializers.SerializerMethodField() # Read-only + cba_id = serializers.IntegerField(required=False, write_only=True) @staticmethod def get_thumbnails(note): @@ -476,7 +477,7 @@ class NoteSerializer(serializers.ModelSerializer): validated_data["user_id"] = self.context["user_id"] # Try to retrieve audio URL from CBA - validated_data["audio_url"] = get_audio_url(validated_data["cba_id"]) + validated_data["audio_url"] = get_audio_url(validated_data.get("cba_id", None)) note = Note.objects.create(**validated_data) diff --git a/program/utils.py b/program/utils.py index 6d30c2079d584dbf794f9ac5eff8430bb40df0c5..53b6420d1b95bc99e98df51ac8e905e0d073eeff 100644 --- a/program/utils.py +++ b/program/utils.py @@ -18,6 +18,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +import json from datetime import date, datetime, time from typing import Dict, Optional, Tuple, Union @@ -57,7 +58,7 @@ def parse_time(date_string: str) -> time: return datetime.strptime(date_string, "%H:%M:%S").time() -def get_audio_url(cba_id): +def get_audio_url(cba_id: Optional[int]) -> str: """ Retrieve the direct URL to the mp3 in CBA In order to retrieve the URL, stations need @@ -67,8 +68,8 @@ def get_audio_url(cba_id): For these contact cba@fro.at """ - if cba_id is None or cba_id == "" or CBA_API_KEY == "": - return None + if not cba_id or CBA_API_KEY == "": + return "" else: if DEBUG: url = ( @@ -85,9 +86,11 @@ def get_audio_url(cba_id): + CBA_API_KEY ) - audio_url = requests.get(url).json() - - return audio_url + try: + return requests.get(url).json() + except (requests.RequestException, json.JSONDecodeError): + # TODO: we might want to add some logging + return "" def get_values(