diff --git a/radio b/radio index cf2e8f5..57916e3 100755 --- a/radio +++ b/radio @@ -1,174 +1,54 @@ -#!/bin/zsh +#!/usr/bin/python3 -case "$1" in - kalx) - # KALX 90.7 FM Berkeley, CA - # http://kalx.berkeley.edu/how-listen - stream_url=http://stream.kalx.berkeley.edu:8000/kalx-320.aac - ;; - kaos) - # KAOS 89.3 FM Olympia, WA - # http://kaosradio.org/listen/ - stream_url=http://205.134.192.90:8930/ - ;; - kcpr) - # KCPR 91.3 FM San Luis Obispo, CA - # http://kcpr.org/listen/ - stream_url=http://129.65.35.106:8000/KCPRHIGH - #stream_url=http://129.65.35.106:8000/KCPRMP3 - ;; - kcsb) - # KCSB-FM 91.9 Santa Barabara, CA - stream_url=http://live.kcsb.org/KCSB_128 - ;; - kdvs) - # KDVS-FM 90.3 Davis, CA - # http://kdvs.org/programming/listen-now/ - stream_url=http://169.237.101.239:8000/kdvs128vorbis - ;; - kexp) - # KEXP 90.3 FM Seattle, WA - stream_url=http://live-mp3-128.kexp.org:8000/ - ;; - kpbs) - # KPBS-FM 89.5 San Diego, CA - stream_url=http://kpbs.streamguys.tv:80/kpbs-aac - ;; - kqac) - # KQAC 89.9 FM Portland, OR - stream_url=http://allclassical-ice.streamguys.com/ac128kmp3 - ;; - ksdt) - stream_url=http://ksdt.ucsd.edu:8000/stream - ;; - kvcu) - # KVCU AM 1190 Boulder, CO - # http://www.radio1190.org/ - stream_url=http://radio1190.colorado.edu:8000/master.mp3 - ;; - kvrx) - # KVRX 91.7 FM Austin, TX - # http://www.kvrx.org/livestream/ - stream_url=http://tstv-stream.tsm.utexas.edu:8000/kvrx_livestream - ;; - kzsu) - # KZSU 90.1 FM Stanford, CA - # http://kzsu.stanford.edu/live/ - stream_url=http://kzsu-streams.stanford.edu/kzsu-1-192.aac - ;; - punk-irratia) - stream_url=http://kinoki.es:8000/punkirratia - ;; - radioq) - # http://www.radioq.de/hoeren/ - stream_url=http://stream.radioq.de:8000/gross.mp3 - ;; - somafm-defcon) - # http://somafm.com/defcon/directstreamlinks.html - stream_url=http://ice1.somafm.com/defcon-128-mp3 - ;; - somafm-dronezone) - # http://somafm.com/dronezone/directstreamlinks.html - stream_url=http://ice1.somafm.com/dronezone-128-aac - ;; - somafm-fluid) - # http://somafm.com/fluid/directstreamlinks.html - stream_url=http://ice1.somafm.com/fluid-128-aac - ;; - somafm-groovesalad) - # http://somafm.com/groovesalad/directstreamlinks.html - stream_url=http://ice1.somafm.com/groovesalad-128-aac - ;; - somafm-secretagent) - # http://somafm.com/secretagent/directstreamlinks.html - stream_url=http://ice1.somafm.com/secretagent-128-aac - ;; - somafm-spacestation) - # http://somafm.com/spacestation/directstreamlinks.html - stream_url=http://ice1.somafm.com/spacestation-128-aac - ;; - somafm-thetrip) - # http://somafm.com/thetrip/directstreamlinks.html - stream_url=http://ice1.somafm.com/thetrip-128-mp3 - ;; - startfm) - stream_url=http://eteris.startfm.lt/startfm - ;; - unifm) - # uniFM 88,4 Freiburg, Germany - # https://www.unicross.uni-freiburg.de/ - stream_url=http://132.230.1.154:8181/mount.mp3 - ;; - wcbn) - # WCBN-FM 88.3 Ann Arbor, MI - # http://www.wcbn.org/ - stream_url=http://floyd.wcbn.org:8000/wcbn-hd.mp3 - ;; - wknc) - # WKNC-FM 88.1 Raleigh, NC - # http://www.wknc.org/listen - stream_url=http://wknc.sma.ncsu.edu:8000/wknchq.ogg - ;; - wmfo) - # WMFO 91.5 FM Medford, MA - # http://www.wmfo.org/ - stream_url=http://130.64.87.48:8000/ - ;; - wmse) - # WMSE 91.7 FM Milwaukee, WI - # http://www.wmse.org/stream/ - stream_url=http://peace.str3am.com:6780 - ;; - wrek) - # WREK 91.1 FM Atlanta, GA - # https://www.wrek.org - stream_url=http://streaming.wrek.org:8000/main/128kb.mp3 - ;; - wuvt) - # WUVT-FM 90.7 Blacksburg, VA - # https://www.wuvt.vt.edu/listen-live - stream_url=https://stream.wuvt.vt.edu/wuvt-hq.ogg - ;; - wvtf) - # WVTF 89.1 FM Roanoke, VA - stream_url=http://mp3.rev.net:8000/wvtf-64.mp3 - ;; - wwvt) - # Radio IQ - stream_url=http://mp3.rev.net:8000/riq-64.mp3 - ;; - wxyc) - # WXYC 89.3 FM Chapel Hill, NC - # http://www.wxyc.org/listen - stream_url=http://audio-mp3.ibiblio.org:8000/wxyc-alt.mp3 - #stream_url=http://audio-mp3.ibiblio.org:8000/wxyc.mp3 - #stream_url=http://152.46.7.128:8000/wxyc.ogg - ;; - yurt) - # Hampshire College - # http://yurt.hampshire.edu/ - stream_url=http://192.101.188.47:8000 - ;; - xbn-320) - stream_url=https://stream.xbn.fm/xbn-320 - ;; - xbn-fc-archive) - stream_url=https://stream.xbn.fm/xbn-fc-archive - ;; - xbn-flac) - stream_url=https://stream.xbn.fm/xbn-flac - ;; - *) - echo "Please enter a supported station." - return 1 - ;; -esac +import argparse +import json +import os.path +import requests +import subprocess +import urllib.parse -if [ -n "$MPD_HOST" ]; then - mpc clear - mpc add $stream_url - mpc play -else - #PULSE_PROP='media.role=music' torsocks -i mpv --no-cache-pause --loop-playlist=force $stream_url - http_proxy=http://127.0.0.1:8118 PULSE_PROP='media.role=music' mpv --no-cache-pause --loop-playlist=force --tls-verify $stream_url -fi +parser = argparse.ArgumentParser( + description="Search for and play a radio station using radio-browser.info") +parser.add_argument('--search-only', '-n', action='store_true', + help="Don't play the station, search only") +parser.add_argument('query', nargs='+') +args = parser.parse_args() + +query = ' '.join(args.query) +station = None + +try: + configpath = os.path.expanduser('~/.config/radio/stations.json') + with open(configpath) as f: + stations = json.load(f) + station = stations.get(query) +except Exception as exc: + print("Failed to check local stations file: {0}".format(exc)) + +if station is None: + r = requests.get( + 'http://www.radio-browser.info/webservice/json/stations/byname/' + '{0}'.format(urllib.parse.quote(query))) + results = r.json() + if len(results) > 0: + r = requests.get( + 'http://www.radio-browser.info/webservice/json/url/' + '{0}'.format(results[0]['id'])) + try: + data = r.json() + station = data[0] + except json.decoder.JSONDecodeError: + # Unbelievably, radio-browser.info fails to quote the station name, + # so fall back to the search results if decoding fails + station = results[0] + +if station is None: + print("No stations found") +else: + print(station['name']) + print(station['url']) + + if not args.search_only: + subprocess.call(['mpc', '-q', 'clear']) + subprocess.call(['mpc', '-q', 'add', station['url']]) + subprocess.call(['mpc', '-q', 'play'])