mirror of
https://github.com/fergalmoran/dss.api.git
synced 2026-01-20 23:44:13 +00:00
Added metadata to radio
This commit is contained in:
@@ -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)
|
||||
|
||||
1
core/radio/__init__.py
Normal file
1
core/radio/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
__author__ = 'fergalm'
|
||||
45
core/radio/ice_scrobbler.py
Normal file
45
core/radio/ice_scrobbler.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user