diff --git a/program/models.py b/program/models.py index be786a4b13880d970dddec2aeb5e46f9c7807cbd..306be0a91bd989ecc34a27fd5bb4bc11f2261f19 100644 --- a/program/models.py +++ b/program/models.py @@ -1072,32 +1072,3 @@ class Note(models.Model): note = Note.objects.get(pk=note_id) return int(note.show_id) in note_view_set.request.user.shows.all().values_list('id', flat=True) - - # FIXME: this does not belong here - @staticmethod - def get_audio_url(cba_id): - """ - Retrieve the direct URL to the mp3 in CBA - In order to retrieve the URL, stations need - - to be whitelisted by CBA - - an API Key - - Therefore contact cba@fro.at - """ - - from steering.settings import CBA_AJAX_URL, CBA_API_KEY - - audio_url = '' - - if cba_id is not None and cba_id != '' and CBA_API_KEY != '': - url = CBA_AJAX_URL + '?action=cba_ajax_get_filename&post_id=' + str(cba_id) + '&api_key=' + CBA_API_KEY - - # For momentary testing without being whitelisted - TODO: delete the line - url = 'https://cba.fro.at/wp-content/plugins/cba/ajax/cba-get-filename.php?post_id=' + str( - cba_id) + '&c=Ml3fASkfwR8' - - with urlopen(url) as conn: - audio_url_json = conn.read().decode('utf-8-sig') - audio_url = json.loads(audio_url_json) - - return audio_url diff --git a/program/serializers.py b/program/serializers.py index 9043ecae3457d2ebcb48aed0e2894a6243436737..e9d5a34ee15187c2555a8206a57897210b294204 100644 --- a/program/serializers.py +++ b/program/serializers.py @@ -28,6 +28,7 @@ from profile.serializers import ProfileSerializer from program.models import Show, Schedule, TimeSlot, Category, FundingCategory, Host, Topic, MusicFocus, Note, Type, Language, \ RRule, Link from steering.settings import THUMBNAIL_SIZES +from utils import get_audio_url class UserSerializer(serializers.ModelSerializer): @@ -363,7 +364,7 @@ class NoteSerializer(serializers.ModelSerializer): validated_data['user_id'] = self.context['user_id'] # Try to retrieve audio URL from CBA - validated_data['audio_url'] = Note.get_audio_url(validated_data['cba_id']) + validated_data['audio_url'] = get_audio_url(validated_data['cba_id']) note = Note.objects.create(**validated_data) @@ -392,7 +393,7 @@ class NoteSerializer(serializers.ModelSerializer): instance.status = validated_data.get('status', instance.status) instance.host = validated_data.get('host', instance.host) instance.cba_id = validated_data.get('cba_id', instance.cba_id) - instance.audio_url = Note.get_audio_url(instance.cba_id) + instance.audio_url = get_audio_url(instance.cba_id) instance.save() diff --git a/program/utils.py b/program/utils.py index d80eb96653105b5fbbf5aefd226c2eae3e7fcea5..18d7d751d667271ddd2da2114a6991e5e3b399c9 100644 --- a/program/utils.py +++ b/program/utils.py @@ -20,8 +20,11 @@ from datetime import datetime +import requests from django.utils import timezone +from steering.settings import CBA_AJAX_URL, CBA_API_KEY, DEBUG + def parse_datetime(date_string: str) -> datetime: """ @@ -33,3 +36,26 @@ def parse_datetime(date_string: str) -> datetime: parsed_datetime = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S%z") return timezone.make_aware(parsed_datetime) + + +def get_audio_url(cba_id): + """ + Retrieve the direct URL to the mp3 in CBA + In order to retrieve the URL, stations need + - to be whitelisted by CBA + - an API Key + + For these contact cba@fro.at + """ + + if cba_id is None or cba_id == '' or CBA_API_KEY == '': + return None + else: + if DEBUG: + url = 'https://cba.fro.at/wp-content/plugins/cba/ajax/cba-get-filename.php?post_id=' + str(cba_id) + '&c=Ml3fASkfwR8' + else: + url = CBA_AJAX_URL + '?action=cba_ajax_get_filename&post_id=' + str(cba_id) + '&api_key=' + CBA_API_KEY + + audio_url = requests.get(url).json() + + return audio_url