Skip to content

goerz/radio

 
 

Repository files navigation

Streaming Radio Terminal Client

This package wraps your favorite terminal based audio stream player (like mpg123). It provides a RESTful API to control stream selection and playback. Includes a simple text based UI to select stations; banners autogenerated with pyfiglet:

 _____                     _ _              _____
|  ___|_ ___   _____  _ __(_) |_ ___  ___  |_   _|   _ _ __   ___ _ __
| |_ / _` \ \ / / _ \| '__| | __/ _ \/ __|   | || | | | '_ \ / _ \ '__|
|  _| (_| |\ V / (_) | |  | | ||  __/\__ \   | || |_| | | | |  __/ |
|_|  \__,_| \_/ \___/|_|  |_|\__\___||___/   |_| \__,_|_| |_|\___|_|


 .oooooo..o                                       oooooooooooo ooo        ooooo    ooooooooooooo
d8P'    `Y8                                       `888'     `8 `88.       .888'    8'   888   `8
Y88bo.       .ooooo.  ooo. .oo.  .oo.    .oooo.    888          888b     d'888          888      oooo  oooo  ooo. .oo.    .ooooo.  oooo d8b
 `"Y8888o.  d88' `88b `888P"Y88bP"Y88b  `P  )88b   888oooo8     8 Y88. .P  888          888      `888  `888  `888P"Y88b  d88' `88b `888""8P
     `"Y88b 888   888  888   888   888   .oP"888   888    "     8  `888'   888          888       888   888   888   888  888ooo888  888
oo     .d8P 888   888  888   888   888  d8(  888   888          8    Y     888          888       888   888   888   888  888    .o  888
8""88888P'  `Y8bod8P' o888o o888o o888o `Y888""8o o888o        o8o        o888o        o888o      `V88V"V8P' o888o o888o `Y8bod8P' d888b

(if you don't like this, you can turn it off)

No more Flash or awkward browser windows consuming memory in order to pipe in your jams. tty_radio supports manual entries you want to import as well as auto-generated entries from SomaFM.

Also serves a web UI at http://localhost:7887, for remote management.

Install

  • Verify you have mpg123
    • else change stream.mpg123.subp_cmd to your fav player
    • Whatever you choose must not buffer output
    • For OS X, Homebrew: brew install mpg123
    • For Linux, apt: sudo apt-get install mpg123
  • Optionally, if you want to display album art
    • pip install pillow
  • Optionally, if you want to scrobble artists and song titles to Last.fm
    • pip install pylast
  • pip install git+git://github.com/goerz/radio.git@master#egg=tty_radio
    • This adds radio to your path

Usage

  • radio starts the server and terminal UI interface
  • radio server starts the server only
  • radio play, radio stop, radio toggle, ... to control a running server from the command line ("scripting interface"). See radio --help for details.
  • Defaults to a list of favorite channels
    • Auto-generated from a hardcoded value
    • Edit ~/.tty_radio-favs.csv to add/remove
    • Any changes are permanent (it's not auto updated)
  • Select the station to listen to by typing its number (left column) and pressing Enter
  • There are other views:
    • At the bottom of the list is a SomaFM view
      • Auto-generated from web scraping updated if more than 7 days old
      • Edit ~/.tty_radio-soma.csv to add/remove, but you will loose changes on next update
    • The Di.FM view has been removed since they've blocked direct streaming

Config file

radio reads settings from a config file in ~/.tty_radio-settings.ini. The file is automatically created if it does not exist. See radio config --help for details.

Color themes

The terminal UI uses color text if possible. By default, it tries to detect whether the terminal has a light or dark background (using the $COLORFGBG environment variable) and uses an appropriate color theme. If it cannot detect the terminal background color, the use of color will be disabled.

The behavior can be modified in the config file:

[UI]
theme = auto
light_theme = light
dark_theme = miami_vice
fallback_theme = nocolor

If theme is auto, one of the themes specified in light_theme, dark_theme, and fallback_theme will be used depending on the terminal background color. Alternatively, theme may be set to the name of a color theme directly, e.g. to nocolor to disable color printing.

The color themes themselves are also defined in the config file, in a section theme_<themename>. For example,

[theme_miami_vice]
meta_song_name = blue
ui_desc = green
stream_exit_confirm = purple
ui_names = yellow
meta_stream_name = blue
stream_name_confirm = purple
meta_prefix = blue
meta_prefix_str = >>>
meta_prefix_pad = 1
ui_banner = red
stream_name_banner = yellow

You may define your own themes by adding any such section.

Scrobbling to Last.fm

You can set up automatic scrobbling of author/title information from the radio stream metadata. This requires that you have the pylast Python package installed.

Next, register at https://www.last.fm/api/account/create to obtain an API key and "shared secret". Enter this information along with your account credentials in the config file, e.g.:

[Lastfm]
api key = 01592035c169e98daf1ecffb783de3ad
shared secret = be09b8b842e7b2dab0df1afc8dff1833
username = lastfmuser8
password hash = 1060b7b46a3bd36b3a0d66e0127d0517

The "password hash" is an MD5-hash of your account password. You may obtain it in Python:

>>> import pylast
>>> pylast.md5("your_password")
'1060b7b46a3bd36b3a0d66e0127d0517'

Lastly, activate scrobbling in the Server section of the config file:

[Server]
scrobble = yes
notify_logfile = ~/.tty_radio-notify.log

The notify_logfile is optional, and may be useful for debugging.

Showing "currently playing" on a Macbook Touch Bar

The BetterTouchTool app allows to customize the touch bar on a recent Macbook Pro. This makes it possible to implement a "mini-client" for tty_radio.

In BetterTouchTool, make sure Touch Bar support is enabled. Then

  • add a new "Widget/Gesture"

  • in "Select Widget", choose "Run Shell Script and Show Return Value"

  • use "Name" tty_radio, Launch Path /bin/bash/, Parameter -c and the following script:

      export LC_ALL=en_US.UTF-8
      export LANG=en_US.UTF-8
      export LANG=en_US.UTF-8
      export LC_CTYPE=en_US.UTF-8
      /full/path/to/radio status --song --stream --quiet
    

    Make sure to use the full path to the radio executable.

  • Set the script to execute every 100 seconds (the maximum): The script is actually only a fallback, the tty_server knows how to push to the widget once it exists.

  • In "Appearance & Settings", set the "Font Size" to 12 (smaller than the default to allow for two lines of text)

  • Choose "Execute Shell Script / Task" as the action for the widget, and use the following script:

      export LC_ALL=en_US.UTF-8
      export LANG=en_US.UTF-8
      export LANG=en_US.UTF-8
      export LC_CTYPE=en_US.UTF-8
      /full/path/to/radio toggle
    
  • Right-click on the newly created widget and "Copy UUID"

  • In the BetterTouchTool advanced settings, "General", check "Allow external BetterTouchTool Scripting" and enter a long random string in "Shared Secret for Scripting"

  • In the tty_radio config file, fill out the BBT section with the UUID and shared secret:

      [BTT]
      shared secret = 0hvrh1aiv1ohn9ozl8eaJeh
      widget uuid = 41CCC8F1-6DA6-45F2-8BDD-FA86A58E01E3
    
  • Lastly, activate updating the widget in the config file:

      [Server]
      update_btt_widget = yes
      notify_logfile = ~/.tty_radio-notify.log
    

    Again, the logfile is optional, but is useful for debugging.

The tty_radio server will now instantly update the touch bar widget whenever the song information changes in the stream metadata, or if a stream is paused/stopped/started.

Controlling playback with LaunchBar

To augment the touch-bar mini-client described above (which can only do play/pause), you could set up LaunchBar to issue commands to a running tty_radio server.

  • In LaunchBar, start the "Action Editor", create "New Action", choose an appropriate name

  • In the "Scripts" tab of the action, select "Shell Script", and check "Run in background", "Requires Argument", and "Accepts string arguments"

  • Click on "Edit", and add the following script:

      #!/bin/bash
      export LC_ALL=en_US.UTF-8
      export LANG=en_US.UTF-8
      export LANG=en_US.UTF-8
      export LC_CTYPE=en_US.UTF-8
      /full/path/to/radio $@
    

    Again, make sure to use the full path to radio

You now have a "radio" action in LaunchBar, and pressing enter on it allows to type in a sub-command, e.g. "play folk" or "tog" (these are equivalent to radio play folk and radio toggle in a terminal).

A similar approach should work for other app launchers (Alfred, Butler, QuickSilver, ...)

About

Python Internet Radio Program

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 93.0%
  • HTML 7.0%