Skip to content

Commit

Permalink
fix typing compatibility issues and use video player to prevent poppi…
Browse files Browse the repository at this point in the history
…ng sound when starting playback
  • Loading branch information
Kjell Knudsen committed Dec 16, 2024
1 parent 41e2037 commit bc2e66d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions plugin.audio.radiobrowser/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@
import urllib.error
import urllib.parse
import urllib.request
from typing import Any
from typing import Any, Dict, List

import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin
import xbmcvfs

from resources.lib.languagecodes import LanguageCode

addonID: str = "plugin.audio.radiobrowser"
addon: xbmcaddon.Addon = xbmcaddon.Addon(id=addonID)

base_url: str = sys.argv[0]
addon_handle = int(sys.argv[1])
args: dict[str, Any] = urllib.parse.parse_qs(sys.argv[2][1:])
args: Dict[str, Any] = urllib.parse.parse_qs(sys.argv[2][1:])

xbmcplugin.setContent(addon_handle, "songs")

Expand All @@ -30,26 +29,26 @@

PAGE_LIMIT: int = 500

DEFAULT_ICON: dict[str, str] = {"icon": "DefaultFolder.png"}
DEFAULT_ICON: Dict[str, str] = {"icon": "DefaultFolder.png"}


class MyStations:
def __init__(self, my_stations: dict[str, Any]):
def __init__(self, my_stations: Dict[str, Any]):
self.stations = my_stations


MY_STATIONS: MyStations = MyStations({})


def get_radiobrowser_base_urls() -> list[str]:
def get_radiobrowser_base_urls() -> List[str]:
"""
Get all base urls of all currently available radiobrowser servers
Returns:
list: a list of strings
"""
hosts: list[str] = []
hosts: List[str] = []
# get all hosts from DNS
ips = socket.getaddrinfo("all.api.radio-browser.info", 80, 0, 0, socket.IPPROTO_TCP)
for ip_tuple in ips:
Expand All @@ -75,15 +74,15 @@ def LANGUAGE(id: int) -> str:
return addon.getLocalizedString(id)


def build_url(query: dict[str, int | str | bytes]):
def build_url(query: Dict[str, "int | str | bytes"]):
return base_url + "?" + urllib.parse.urlencode(query)


def addLink(stationuuid: str, name: str, url: str, favicon: str, bitrate: str):
li = xbmcgui.ListItem(name)
li.setArt({"icon": favicon})
li.setProperty("IsPlayable", "true")
li.setInfo(type="Music", infoLabels={"Title": name, "Size": bitrate})
li.setInfo(type="Video", infoLabels={"Title": name, "Size": bitrate})
localUrl = build_url({"mode": "play", "stationuuid": stationuuid})

if stationuuid in MY_STATIONS.stations:
Expand All @@ -109,7 +108,7 @@ def addLink(stationuuid: str, name: str, url: str, favicon: str, bitrate: str):
)


def downloadFile(uri: str, param, url_parameter: dict[str, Any] = {}) -> bytes:
def downloadFile(uri: str, param, url_parameter: Dict[str, Any] = {}) -> bytes:
"""
Download file with the correct headers set
Expand Down Expand Up @@ -138,7 +137,9 @@ def downloadFile(uri: str, param, url_parameter: dict[str, Any] = {}) -> bytes:


def downloadApiFile(
path: str, param: dict[str, Any] | None, url_parameter: dict[str, str | int] = {}
path: str,
param: "Dict[str, Any] | None",
url_parameter: Dict[str, "str | int"] = {},
) -> bytes:
"""
Download file with relative url from a random api server.
Expand Down Expand Up @@ -206,7 +207,7 @@ def delFromMyStations(stationuuid: str):


def createDirectoryItem(
urlArgs: dict[str, str | bytes | int], name: str, artArgs: dict[str, str]
urlArgs: Dict[str, "str | bytes | int"], name: str, artArgs: Dict[str, str]
):
localUrl = build_url(urlArgs)
li = xbmcgui.ListItem(name)
Expand Down Expand Up @@ -260,7 +261,7 @@ def buildMenu() -> None:
xbmcplugin.endOfDirectory(addon_handle)


def buildTagsList(args: dict[str, Any]) -> None:
def buildTagsList(args: Dict[str, Any]) -> None:
page = args.get("page")
if page is not None:
try:
Expand All @@ -270,7 +271,7 @@ def buildTagsList(args: dict[str, Any]) -> None:
else:
page = 0

url_parameter: dict[str, str | int] = {
url_parameter: Dict[str, "str | int"] = {
"limit": PAGE_LIMIT,
"offset": page * PAGE_LIMIT,
}
Expand Down Expand Up @@ -333,7 +334,7 @@ def buildCountriesList() -> None:
xbmcplugin.endOfDirectory(addon_handle)


def buildStatesList(args: dict[str, Any]) -> None:
def buildStatesList(args: Dict[str, Any]) -> None:
country = args["country"][0]
country = base64.b32decode(country)
country = country.decode("utf-8")
Expand Down Expand Up @@ -372,9 +373,9 @@ def buildStatesList(args: dict[str, Any]) -> None:
xbmcplugin.endOfDirectory(addon_handle)


def buildStationsSearch(args: dict[str, Any]) -> None:
def buildStationsSearch(args: Dict[str, Any]) -> None:
url = "/json/stations/search"
param: dict[str, Any] = {}
param: Dict[str, Any] = {}
if "url" in args:
url = args["url"][0]
else:
Expand All @@ -390,7 +391,7 @@ def buildStationsSearch(args: dict[str, Any]) -> None:
xbmcplugin.endOfDirectory(addon_handle)


def playStation(args: dict[str, Any]) -> None:
def playStation(args: Dict[str, Any]) -> None:
stationuuid = args["stationuuid"][0]
data = downloadApiFile("/json/url/" + str(stationuuid), None)
dataDecoded = json.loads(data)
Expand Down Expand Up @@ -426,7 +427,7 @@ def buildMyStations() -> None:
xbmcplugin.endOfDirectory(addon_handle)


def addStation(args: dict[str, Any]) -> None:
def addStation(args: Dict[str, Any]) -> None:
favicon = args["favicon"][0] if "favicon" in args else ""
addToMyStations(
args["stationuuid"][0],
Expand All @@ -437,7 +438,7 @@ def addStation(args: dict[str, Any]) -> None:
)


def deleteStation(args: dict[str, Any]) -> None:
def deleteStation(args: Dict[str, Any]) -> None:
delFromMyStations(args["stationuuid"][0])


Expand All @@ -461,7 +462,7 @@ def addCustomStation() -> None:
xbmc.executebuiltin("Container.Refresh(" + refresh_url + ")")


def router(mode: str | None, args: dict[str, Any]) -> None:
def router(mode: "str | None", args: Dict[str, Any]) -> None:
if mode is None:
buildMenu()
elif mode == "tags":
Expand Down
Binary file removed plugin.audio.radiobrowser/resources/.zip
Binary file not shown.

0 comments on commit bc2e66d

Please sign in to comment.