Skip to content
Snippets Groups Projects
Commit b126918a authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

Move get_audio_url into utils and update the uses

parent c7421952
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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()
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment