refactoring; remove old methods; add configuration file; default to mpv instead of mpg123
This commit is contained in:
parent
283d898408
commit
446b80da2e
17
config.py
Normal file
17
config.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import configparser
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('radiostations.ini')
|
||||||
|
|
||||||
|
|
||||||
|
def api():
|
||||||
|
return config.get('DEFAULT', 'radio_browser.api')
|
||||||
|
|
||||||
|
|
||||||
|
def player():
|
||||||
|
return config.get('DEFAULT', 'player.command')
|
||||||
|
|
||||||
|
|
||||||
|
def options():
|
||||||
|
return config.get('DEFAULT', 'player.options')
|
||||||
|
|
@ -1,6 +1,8 @@
|
|||||||
from python_console_menu import AbstractMenu, MenuItem
|
from python_console_menu import AbstractMenu, MenuItem
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
from config import player, options
|
||||||
|
|
||||||
|
|
||||||
class RadioMenu(AbstractMenu):
|
class RadioMenu(AbstractMenu):
|
||||||
def __init__(self, station_list=None):
|
def __init__(self, station_list=None):
|
||||||
@ -16,7 +18,7 @@ class RadioMenu(AbstractMenu):
|
|||||||
station_list[i]["codec"] + " " +
|
station_list[i]["codec"] + " " +
|
||||||
station_list[i]["bitrate"] + " " +
|
station_list[i]["bitrate"] + " " +
|
||||||
station_list[i]["url"],
|
station_list[i]["url"],
|
||||||
lambda url=station_list[i]["url"]: subprocess.run(["mpv", "--no-video", url])
|
lambda url=station_list[i]["url"]: subprocess.run([player(), options(), url])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
5
radiostations.ini
Normal file
5
radiostations.ini
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[DEFAULT]
|
||||||
|
radio_browser.api=all.api.radio-browser.info
|
||||||
|
player.command=mpv
|
||||||
|
player.options=--no-video
|
||||||
|
|
15
stations.py
15
stations.py
@ -4,6 +4,7 @@ import json
|
|||||||
import requests
|
import requests
|
||||||
import secrets
|
import secrets
|
||||||
|
|
||||||
|
from config import api
|
||||||
from radiomenu import RadioMenu
|
from radiomenu import RadioMenu
|
||||||
|
|
||||||
|
|
||||||
@ -16,24 +17,12 @@ def nsname(ipaddrs, rectype='A'):
|
|||||||
|
|
||||||
|
|
||||||
def get_host():
|
def get_host():
|
||||||
hosts = ipaddr('all.api.radio-browser.info')
|
hosts = ipaddr(api())
|
||||||
preferred_host = secrets.choice(hosts)
|
preferred_host = secrets.choice(hosts)
|
||||||
return nsname(preferred_host)
|
return nsname(preferred_host)
|
||||||
|
|
||||||
|
|
||||||
def get_station_by_name(callsign, host):
|
|
||||||
resp = requests.get(f"https://{host}/json/stations/byname/" + callsign)
|
|
||||||
if resp.status_code == 200:
|
|
||||||
return json.loads(resp.text)
|
|
||||||
else:
|
|
||||||
return [{"response_code": resp.status_code, "reason": resp.reason}]
|
|
||||||
|
|
||||||
|
|
||||||
def get_stations(qstring, host):
|
def get_stations(qstring, host):
|
||||||
# https://de1.api.radio-browser.info/json/stations/search?name=wfmt&limit=100
|
|
||||||
# https://de1.api.radio-browser.info/json/stations/search?tag=classical&limit=100
|
|
||||||
# https://de1.api.radio-browser.info/json/stations/search?state=Illinois&tag=classical&limit=100
|
|
||||||
# https://de1.api.radio-browser.info/json/stations/search?country=America&state=Illinois&tag=classical&limit=100
|
|
||||||
resp = requests.get(f"https://{host}/json/stations/search?{qstring}&limit=100000")
|
resp = requests.get(f"https://{host}/json/stations/search?{qstring}&limit=100000")
|
||||||
if resp.status_code == 200:
|
if resp.status_code == 200:
|
||||||
return json.loads(resp.text)
|
return json.loads(resp.text)
|
||||||
|
Loading…
Reference in New Issue
Block a user