Skip to content

Commit

Permalink
Update antenna.py
Browse files Browse the repository at this point in the history
This version makes the following changes:
1. the station database is moved from /home/user/.config/antennarc to /home/user/.local/share/antenna/antennarc. If you were using the previous version, you'll have to move the database file.
2. the player changes from the python playsound module to gst123.
3. added the environment statement in line 1 to permit the file to be made directly executable.
4. release added for Debian 11 Bullseye.
  • Loading branch information
Visiblink committed Aug 1, 2022
1 parent 64ebffb commit ddb261f
Showing 1 changed file with 8 additions and 36 deletions.
44 changes: 8 additions & 36 deletions antenna.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3

# Antenna - an internet radio stream player.
# Copyright (C) 2022 Visiblink
Expand All @@ -16,44 +17,19 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import os
import multiprocessing
import time
from playsound import playsound
from sys import exit

# The functions in the class below were added to suppress logging to stdout by libva.
# The code is by users jeremiahbuddha and randlet, "Suppress stdout / stderr print
# from Python functions," Stack Overflow, https://stackoverflow.com/questions/
# 11130156/suppress-stdout-stderr-print-from-python-functions, CC-BY-SA 4.0.

class suppress_stdout_stderr(object):

def __init__(self):
self.null_fds = [os.open(os.devnull,os.O_RDWR) for x in range(2)]
self.save_fds = [os.dup(1), os.dup(2)]

def __enter__(self):
os.dup2(self.null_fds[0],1)
os.dup2(self.null_fds[1],2)

def __exit__(self, *_):
os.dup2(self.save_fds[0],1)
os.dup2(self.save_fds[1],2)
for fd in self.null_fds + self.save_fds:
os.close(fd)

# End of code by users jeremiahbuddha and randlet

# The functions below are by user junglejet, "How to save a dictionary to a file?" Stack Overflow,
# https://stackoverflow.com/questions/19201290/how-to-save-a-dictionary-to-a-file, CC-BY-SA 4.0.

def save_dict_to_file(dic):
f = open(os.path.join(os.path.expanduser('~'), '.config', 'antennarc'),'w')
f = open(os.path.join(os.path.expanduser('~'), '.local', 'share', 'antenna', 'antennarc'),'w')
f.write(str(dic))
f.close()

def load_dict_from_file():
f = open(os.path.join(os.path.expanduser('~'), '.config', 'antennarc'),'r')
f = open(os.path.join(os.path.expanduser('~'), '.local', 'share', 'antenna', 'antennarc'),'r')
data=f.read()
f.close()
return eval(data)
Expand Down Expand Up @@ -229,15 +205,9 @@ def move_station (internal_station_list, user_choice_integer):
move_station(internal_station_list, user_choice_integer)

def play_stream(stream_url):
# The code below is modified from a snippet by user Artemis, "How to stop audio with playsound module?" Stack Overflow,
# https://stackoverflow.com/questions/57158779/how-to-stop-audio-with-playsound-module, CC-BY-SA 4.0.
with suppress_stdout_stderr():
player = multiprocessing.Process(target=playsound, args=(stream_url,))
player.start()
print()
input("Press ENTER to stop playback ")
player.terminate()
# End of code by user Artemis.
print("Press 'q' to stop playback ")
os.system(f'gst123 -q {stream_url}')
main_menu()

def station_display(internal_station_list):
Expand Down Expand Up @@ -268,7 +238,9 @@ def station_display(internal_station_list):
print()

def main_menu():
if os.path.isfile(os.path.join(os.path.expanduser('~'), '.config', 'antennarc'))==False:
if os.path.isfile(os.path.join(os.path.expanduser('~'), '.local', 'share', 'antenna', 'antennarc'))==False:
if os.path.isdir(os.path.join(os.path.expanduser('~'), '.local', 'share', 'antenna', 'antennarc'))==False:
os.mkdir(os.path.join(os.path.expanduser('~'), '.local', 'share', 'antenna'))
station_list={}
else:
station_list = load_dict_from_file()
Expand Down

0 comments on commit ddb261f

Please sign in to comment.