diff --git a/api/helpers.py b/api/helpers.py index 994a902..a29edfb 100644 --- a/api/helpers.py +++ b/api/helpers.py @@ -2,6 +2,7 @@ import datetime from rest_framework.response import Response from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_201_CREATED, HTTP_204_NO_CONTENT, HTTP_200_OK from rest_framework.views import APIView +from core.radio import ice_scrobbler from dss import settings from spa.models import Mix, UserProfile from core.utils import session @@ -64,10 +65,14 @@ class UserSlugCheckHelper(Helper): class RadioHelper(Helper): def get(self, request): - m = Mix.objects.order_by('?').first() - ret = { - 'url': m.get_stream_url(), - 'title': str(m) - } + if 'rmix' in self.request.query_params: + m = Mix.objects.order_by('?').first() + ret = { + 'url': m.get_stream_url(), + 'slug': m.get_full_url(), + 'title': str(m) + } + elif 'np' in self.request.query_params: + ret = ice_scrobbler.get_server_details("localhost", "8000", "dss") return Response(data=ret, status=HTTP_200_OK) diff --git a/core/radio/__init__.py b/core/radio/__init__.py new file mode 100644 index 0000000..6d4caf8 --- /dev/null +++ b/core/radio/__init__.py @@ -0,0 +1 @@ +__author__ = 'fergalm' diff --git a/core/radio/ice_scrobbler.py b/core/radio/ice_scrobbler.py new file mode 100644 index 0000000..1d3185f --- /dev/null +++ b/core/radio/ice_scrobbler.py @@ -0,0 +1,45 @@ +import requests +from bs4 import BeautifulSoup +from requests.packages.urllib3.connection import ConnectionError + + +def get_server_details(server, port, mount): + server = "http://%s:%s/status.xsl?mount=/%s" % (server, port, mount) + print("Getting info for %s" % server) + try: + response = requests.get(server) + html = response.text + if html: + try: + soup = BeautifulSoup(html, "html.parser") + info = { + 'stream_title': soup.find(text="Stream Title:").findNext('td').contents[0], + 'stream_description': soup.find(text="Stream Description:").findNext('td').contents[0], + 'content_type': soup.find(text="Content Type:").findNext('td').contents[0], + 'mount_started': soup.find(text="Mount started:").findNext('td').contents[0], + 'current_listeners': soup.find(text="Current Listeners:").findNext('td').contents[0], + 'peak_listeners': soup.find(text="Peak Listeners:").findNext('td').contents[0], + 'stream_url': soup.find(text="Stream URL:").findNext('td').findNext('a').contents[0], + 'current_song': soup.find(text="Current Song:").findNext('td').contents[0] + } + return { + 'status': 2, + 'metadata': info + } + except AttributeError: + return { + 'status': 1 + } + else: + return { + 'status': 0 + } + except Exception as ex: + return { + 'status': 0 + } + + +if __name__ == '__main__': + d = get_server_details("localhost", "8000", "dss") + print(d)