/tizonia_presets

Python 3: Tizonia music app presets: csv.reader, subprocess.run

Primary LanguagePythonMIT LicenseMIT

tizonia_presets

Python 3: Tizonia music app presets: csv.reader, subprocess.run

Tizonia

subprocess

import subprocess

subprocess module is imported to spawn the tizonia music app which plays YouTube audio tracks listed in ThePsychedelicMuse profile. In other words, subprocess enables Python to run Bash commands in the CLI. In this case, the tizonia music app will run.

Note: tizonia is a cross-platform application installed in an OS, so it is unavailable to this notebook.


csv.reader()

from csv import reader

The csv.reader() method returns a reader object (<_csv.reader object at 0x7f4340e24208>) which is stored in the csv_read variable, and which iterates over the lines track_links.csv.

track_links.csv

========== Psychedelic Muse - Ambient Tracks ==========
Shamanizm Parallelii - Strange Food For Strange People From Fairy Tales Part 3: Dub | Full Album,https://www.youtube.com/watch?v=4UWJBdPm2dA
Vena Portae - Underwater Remixes | Full Album,https://www.youtube.com/watch?v=oVrFkqiUlbg
communication Tube - Amener Lumière | Full Album,https://www.youtube.com/watch?v=HcWmWify9Wc
Dubnotic & MettāKin - Spiritual Techniques | Full EP,https://www.youtube.com/watch?v=SCyBAAxX0xs
Neuroq - Catharsis | Full Album,https://www.youtube.com/watch?v=cCEphVJ0Tek
Noosfære - Mythos Mix (Psychedelic Chillout),https://www.youtube.com/watch?v=8p1K9FE7q_w
Youginia - My Home | Full Album,https://www.youtube.com/watch?v=ucfWPuvDC0M
Reblooming - Echo Earth | Full Album,https://www.youtube.com/watch?v=FR2jtc74dVU
Orenda - Reverie | Full EP,https://www.youtube.com/watch?v=HYtIpg4TEbs
Vitriol - Visita Interiora Terrae | Full EP,https://www.youtube.com/watch?v=EiDjuabl1FE 

track_links.csv contains a list of comma-separated values. Each line is a data record. Each record consists of a YouTube video name, followed by it's associated URL. So a comma becomes a field separator.


URL Dictionary

urls = {}
>>> import pprint
>>> pprint.pprint(urls)
{1: 'https://www.youtube.com/watch?v=4UWJBdPm2dA',
 2: 'https://www.youtube.com/watch?v=oVrFkqiUlbg',
 3: 'https://www.youtube.com/watch?v=HcWmWify9Wc',
 4: 'https://www.youtube.com/watch?v=SCyBAAxX0xs',
 5: 'https://www.youtube.com/watch?v=cCEphVJ0Tek',
 6: 'https://www.youtube.com/watch?v=8p1K9FE7q_w',
 7: 'https://www.youtube.com/watch?v=ucfWPuvDC0M',
 8: 'https://www.youtube.com/watch?v=FR2jtc74dVU',
 9: 'https://www.youtube.com/watch?v=HYtIpg4TEbs',
 10: 'https://www.youtube.com/watch?v=EiDjuabl1FE'}
urls[c] = ln[1]

After the first line prints, each iteration through the csv.reader object appends a counter value and a URL to the urls dictionary. The main purpose of this dictionary is to associate a numerical value with a URL so the user can choose a music track number.


Open CSV File

with open('track_links.csv', newline='') as csv_encode

The open() built-in function opens track_links.csv and returns a corresponding file object. More specifically, a io.TextIOWrapper which wraps the raw byte stream with a layer to handle string encoding. The header <_io.TextIOWrapper name='track_links.csv' mode='r' encoding='UTF-8'> indicates that UTF-8 encoding will be used, so the variable was named csv_encode.

Note: some operating systems may add \n on the end. Even though I have not experienced such an issue, the newline='' parameter was added to the open() call.


CSV Reader Object

csv_read = reader(csv_enc, delimiter=',')

The csv_read variable stores the CSV reader object used to iterate over the lines of the CSV file.


Print First Line

print(f'{csv_enc.readline()}')

By means of the CSV reader object's iterator protocol, its __next__() method returns a string. In this case, csv_enc.readline() returns the first string of track_links.csv


Counter

c = 1

The counter is initialized with the value of 1 because it will be used to number the track list printed to the terminal emulator display. So the counter increment is c += 1 to facilitate numerical order.


Looping Structure

for rec in csv_read:
    print(f'{c:>2}: {rec[0]}')  # number/title
    urls[c] = rec[1]  # insert keys/values into `urls` dict
    c += 1  # increment counter

After the first line from the CSV reader object prints, the looping structure iterates through the remaining data records. In other words, line by line.

The print call print(f'{c:>2}: {ln[0]}') contains an f-string which contains expressions for the incremented track number and the URL. ln[0] gets the first field of each record which contains the YouTube video title.

The line url_dict[c] = ln[1] inserts an item into urls dictionary. ln[1] gets the second field of each record, which contains the YouTube URL.


Input Track Number

After the track list prints, the user is prompted to choose a track and enter its number: "Enter track number: " The variable trk_num stores that value.


Start the Music App

run(["tizonia", "--youtube-audio-stream", urls[trk_num]])

As described in the first section, subprocess allows Python to run tizonia from the CLI. tizonia gets the YouTube audio stream, and plays it.

ko-fi