refactoring; remove old methods; add configuration file; default to mpv instead of mpg123

This commit is contained in:
Greg Gauthier 2021-03-15 20:46:15 +00:00
parent 283d898408
commit 446b80da2e
4 changed files with 27 additions and 14 deletions

17
config.py Normal file
View 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')

View File

@ -1,6 +1,8 @@
from python_console_menu import AbstractMenu, MenuItem
import subprocess
from config import player, options
class RadioMenu(AbstractMenu):
def __init__(self, station_list=None):
@ -16,7 +18,7 @@ class RadioMenu(AbstractMenu):
station_list[i]["codec"] + " " +
station_list[i]["bitrate"] + " " +
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
View File

@ -0,0 +1,5 @@
[DEFAULT]
radio_browser.api=all.api.radio-browser.info
player.command=mpv
player.options=--no-video

View File

@ -4,6 +4,7 @@ import json
import requests
import secrets
from config import api
from radiomenu import RadioMenu
@ -16,24 +17,12 @@ def nsname(ipaddrs, rectype='A'):
def get_host():
hosts = ipaddr('all.api.radio-browser.info')
hosts = ipaddr(api())
preferred_host = secrets.choice(hosts)
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):
# 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")
if resp.status_code == 200:
return json.loads(resp.text)