Skip to content

Python 3: Radio Streams VLC: Wrapper Script for Radio Station Selection: Calls VLC Media Player: csv.reader(), subprocess.run()

License

Notifications You must be signed in to change notification settings

nick3499/radio_streams_vlc

Repository files navigation

Radio Streams VLC

Screen Capture #1

Imported app.py in Python interactive shell:

screen capture #1

Screen Capture #2

Called app.station_selection(), then entered station number which executed MPV media player to stream an Internet radio station:

screen capture #2

Tested using...

Check Out thee Fork

Before going any further, check out zcutlip's fork: zcutlip/chill_streams, which has been much more thoroughly developed and secured.

app.py

from subprocess import run
from ascii_art import get_ascii_art
import station_list

print(get_ascii_art())
station_list.get_station_list()

def station_selection():
    URLS = station_list.URLS
    station_num = int(input('\x1b[38;2;72;201;176mEnter station number\
\x1b[0m: '))
    run(['/usr/bin/mpv', URLS[station_num]], check=True)
    c_1 = '\x1b[38;2;220;118;51m'
    c_2 = '\x1b[38;2;244;208;63m'
    res = '\x1b[0m'
    print(f'{c_1}+~+~+~+~+~+~+~+~+~+~+~+~+~+{res}')
    print(f'{c_1}<{c_2}R{c_1}|{c_2}A{c_1}|{c_2}D{c_1}|{c_2}I{c_1}|{c_2}O{c_1}>\
{c_2} {c_1}<{c_2}S{c_1}|{c_2}T{c_1}|{c_2}R{c_1}|{c_2}E{c_1}|{c_2}A{c_1}|{c_2}M\
{c_1}|{c_2}S{c_1}>{res}')
    print(f'{c_1}+~+~+~+~+~+~+~+~+~+~+~+~+~+{res}')
    station_selection()

if __name__ == '__main__':
    station_selection()

The #! sequence is a Unix shebang which appears at the top line of executable text files. That human-readable sequence translates to hexadecimal numbers, called magic numbers: 0x23 0x21. Following the shebang would be a path to the program interpreter. In this case it is the Python interpreter: #!/usr/bin/env python, which enables an executable text file to be started at the terminal's $ command line prompt with the sequence $ ./app.py.

>>> hex(ord('#'))
'0x23'
>>> hex(ord('!'))
'0x21'
from subprocess import run
from ascii_art import get_ascii_art
import station_list

The subprocess.run() method enables a Python application to access the CLI. For example, the instruction...

`run(['/usr/bin/mpv', URLS[station_num]], check=True)`

...becomes the arguments $ /snap/bin/vlc --intf ncurses http://ice3.somafm.com/defcon-128-aac in the terminal emulator's command line.

The remaining imports are for the ASCII art heading and printed station list described in the next two sections.

print(get_ascii_art())
station_list.get_station_list()

After the ASCII art heading prints, the station list prints, which displays available internet radio stations. Followed by a prompt, where the user enters the number which corresponds with the radio station they want to listen to.

def station_selection():
    URLS = station_list.URLS
    station_num = int(input('\x1b[38;2;72;201;176mEnter station number\
\x1b[0m: '))
    run(['/usr/bin/mpv', URLS[station_num]], check=True)
    c_1 = '\x1b[38;2;220;118;51m'
    c_2 = '\x1b[38;2;244;208;63m'
    res = '\x1b[0m'
    print(f'{c_1}+~+~+~+~+~+~+~+~+~+~+~+~+~+{res}')
    print(f'{c_1}<{c_2}R{c_1}|{c_2}A{c_1}|{c_2}D{c_1}|{c_2}I{c_1}|{c_2}O{c_1}>\
{c_2} {c_1}<{c_2}S{c_1}|{c_2}T{c_1}|{c_2}R{c_1}|{c_2}E{c_1}|{c_2}A{c_1}|{c_2}M\
{c_1}|{c_2}S{c_1}>{res}')
    print(f'{c_1}+~+~+~+~+~+~+~+~+~+~+~+~+~+{res}')
    station_selection()

The dictionary station_list.urls contains the station data generated by station_list.get_station_list(). Then the instruction int(input(Enter item number: ')) prompts the user for the item number. Finally, the subprocess.run() method is called, as described above.

if __name__ == '__main__':
    station_selection()

The final if block checks the __name__ string for __main__. If the name is __main__, that means station_selection() is called and app.py runs as a standalone application.

ascii_art.py

def get_ascii_art():
    radio = '''\x1b[38;2;72;201;176m
:::::::..    :::.   :::::::-.  :::    ...
;;;;``;;;;   ;;`;;   ;;,   `';,;;; .;;;;;;;.\x1b[38;2;244;208;63m
 [[[,/[[['  ,[[ '[[, `[[     [[[[[,[[     \[[,
 $$$$$$c   c$$$cc$$$c $$,    $$$$$$$$,     $$$\x1b[38;2;220;118;51m
 888b "88bo,888   888,888_,o8P'888"888,_ _,88P
 MMMM   "W" YMM   ""` MMMMP"`  MMM  "YMMMMMP"\x1b[0m'''

    return radio

The ascii_art module returns color-schemed ASCII art which becomes the header at the top of the station list.

station_list.py

from csv import reader

URLS = {}

def get_station_list():
    c_1 = '\x1b[38;2;72;201;176m'
    c_2 = '\x1b[38;2;244;208;63m'
    c_3 = '\x1b[38;2;220;118;51m'
    res = '\x1b[0m'

    with open('/home/nick/.scripts/radio_streams_vlc/csv/stations.csv') as csv_file:
        csv_rec_list = list(enumerate(reader(csv_file), 1))
        for num, rec in csv_rec_list:
            print(f'{c_1}{num:>2}{res}  {c_2}{rec[0]:}{res}  {c_3}{rec[1]}{res}')
            URLS[num] = rec[2]

The station_list module is imported by the vlc_radio_wrapper module, so it can be accessed with the following instruction:

station_list.get_station_list()

The station_list module generates an enumerated, color-schemed list of station names/genres. The string \x1b[38;2;72;201;176m exemplifies a RGB color formatting sequence, followed by \x1b[0m which resets color formatting to default.

The with open() statement then opens the CSV database which contains internet radio station data used for station selection, along with URLs for various stations. The resulting file object is passed to the csv.reader() method. The resulting CSV reader object is passed to enumerate to be numbered, then to list()

Desktop Launcher

Desktop Icon Image

[Desktop Entry]
Version=1.1
Type=Application
Name=Radio Streams VLC
GenericName=Radio Streams VLC
Comment=Displays list of Internet radio stations to choose from.
Icon=/usr/share/icons/foo/256x256/radio_streams_vlc.png
TryExec=xterm
Exec=xterm -fa "monofur" -fs 10 -geometry 90x60+0 -e /home/foo/scripts/radio_streams_vlc/app.py
Path=/home/foo/scripts/radio_streams_vlc/
Terminal=true
Actions=
Categories=Audio;Player;

To list available fonts: fc-list | cut -f2 -d: | sort -u | grep -i Mono

About

Python 3: Radio Streams VLC: Wrapper Script for Radio Station Selection: Calls VLC Media Player: csv.reader(), subprocess.run()

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages