Skip to content

Commit

Permalink
A few small bugfixes (#753)
Browse files Browse the repository at this point in the history
* Fix typo in ProviderMappings resulting in wrong contenttype

* move preview stream into webserver

* Fix detection check of homeassistant addon
  • Loading branch information
marcelveldt authored Jul 8, 2023
1 parent 1abbe27 commit cfd22d6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion music_assistant/server/controllers/media/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def _get_provider_mappings(
return org_item.provider_mappings
if overwrite and update_item.provider_mappings:
return update_item.provider_mappings
return {*org_item.provider_mappings, *update_item.provider_mappings}
return {*update_item.provider_mappings, *org_item.provider_mappings}

async def _get_artist_mappings(
self,
Expand Down
13 changes: 0 additions & 13 deletions music_assistant/server/controllers/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
check_audio_support,
crossfade_pcm_parts,
get_media_stream,
get_preview_stream,
get_stream_details,
)
from music_assistant.server.helpers.process import AsyncProcess
Expand Down Expand Up @@ -330,7 +329,6 @@ async def setup(self, config: CoreConfig) -> None:
bind_port=self.publish_port,
base_url=f"http://{self.publish_ip}:{self.publish_port}",
static_routes=[
("GET", "/preview", self.serve_preview_stream),
(
"GET",
"/{queue_id}/multi/{job_id}/{player_id}/{queue_item_id}.{fmt}",
Expand Down Expand Up @@ -729,17 +727,6 @@ async def read_audio():

return resp

async def serve_preview_stream(self, request: web.Request):
"""Serve short preview sample."""
self._log_request(request)
provider_instance_id_or_domain = request.query["provider"]
item_id = urllib.parse.unquote(request.query["item_id"])
resp = web.StreamResponse(status=200, reason="OK", headers={"Content-Type": "audio/mp3"})
await resp.prepare(request)
async for chunk in get_preview_stream(self.mass, provider_instance_id_or_domain, item_id):
await resp.write(chunk)
return resp

async def get_flow_stream(
self,
queue: PlayerQueue,
Expand Down
23 changes: 19 additions & 4 deletions music_assistant/server/controllers/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import inspect
import logging
import os
import urllib.parse
from collections.abc import Awaitable
from concurrent import futures
from contextlib import suppress
Expand All @@ -33,6 +34,7 @@
from music_assistant.common.models.event import MassEvent
from music_assistant.constants import CONF_BIND_IP, CONF_BIND_PORT
from music_assistant.server.helpers.api import APICommandHandler, parse_arguments
from music_assistant.server.helpers.audio import get_preview_stream
from music_assistant.server.helpers.util import get_ips
from music_assistant.server.helpers.webserver import Webserver
from music_assistant.server.models.core_controller import CoreController
Expand Down Expand Up @@ -81,7 +83,7 @@ async def get_config_entries(
# if a user also wants to expose a the webserver non securely on his internal
# network he/she should open the port in the add-on config.
internal_ip = next((x for x in await get_ips() if x.startswith("172")), await get_ip())
base_url = f"http://{internal_ip:8095}"
base_url = f"http://{internal_ip}:8095"
return (
ConfigEntry(
key=CONF_BIND_PORT,
Expand Down Expand Up @@ -174,11 +176,13 @@ async def setup(self, config: CoreConfig) -> None:
# also host the image proxy on the webserver
routes.append(("GET", "/imageproxy", self.mass.metadata.handle_imageproxy))
# also host the audio preview service
routes.append(("GET", "/preview", self.mass.streams.serve_preview_stream))
routes.append(("GET", "/preview", self.serve_preview_stream))
# start the webserver
self.publish_port = config.get_value(CONF_BIND_PORT)
self.publish_ip = config.get_value(CONF_BIND_IP)
await self._server.setup(
bind_ip=config.get_value(CONF_BIND_IP),
bind_port=config.get_value(CONF_BIND_PORT),
bind_ip=self.publish_ip,
bind_port=self.publish_port,
base_url=config.get_value(CONF_BASE_URL),
static_routes=routes,
# add assets subdir as static_content
Expand All @@ -191,6 +195,17 @@ async def close(self) -> None:
await client.disconnect()
await self._server.close()

async def serve_preview_stream(self, request: web.Request):
"""Serve short preview sample."""
self._log_request(request)
provider_instance_id_or_domain = request.query["provider"]
item_id = urllib.parse.unquote(request.query["item_id"])
resp = web.StreamResponse(status=200, reason="OK", headers={"Content-Type": "audio/mp3"})
await resp.prepare(request)
async for chunk in get_preview_stream(self.mass, provider_instance_id_or_domain, item_id):
await resp.write(chunk)
return resp

async def _handle_server_info(self, request: web.Request) -> web.Response: # noqa: ARG002
"""Handle request for server info."""
return web.json_response(self.mass.get_server_info().to_dict())
Expand Down
17 changes: 11 additions & 6 deletions music_assistant/server/helpers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import urllib.error
import urllib.parse
import urllib.request
from contextlib import suppress
from functools import lru_cache
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as pkg_version
Expand Down Expand Up @@ -73,11 +72,17 @@ def call() -> set[str]:

async def is_hass_supervisor() -> bool:
"""Return if we're running inside the HA Supervisor (e.g. HAOS)."""
with suppress(urllib.error.URLError):
res = await asyncio.to_thread(urllib.request.urlopen, "http://supervisor/core")
# this should return a 401 unauthorized if it exists
return res.code == 401
return False

def _check():
try:
urllib.request.urlopen("http://supervisor/core")
except urllib.error.URLError as err:
# this should return a 401 unauthorized if it exists
return getattr(err, "code", 999) == 401
except Exception:
return False

return await asyncio.to_thread(_check)


async def get_provider_module(domain: str) -> ProviderModuleType:
Expand Down
4 changes: 2 additions & 2 deletions music_assistant/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ async def _setup_discovery(self) -> None:
info = ServiceInfo(
zeroconf_type,
name=f"{server_id}.{zeroconf_type}",
addresses=[await get_ip_pton(self.streams.publish_ip)],
port=self.streams.publish_port,
addresses=[await get_ip_pton(self.webserver.publish_ip)],
port=self.webserver.publish_port,
properties=self.get_server_info().to_dict(),
server="mass.local.",
)
Expand Down

0 comments on commit cfd22d6

Please sign in to comment.