-
Notifications
You must be signed in to change notification settings - Fork 0
/
antenna.py
245 lines (219 loc) · 9.65 KB
/
antenna.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python3
# Antenna - an internet radio stream player.
# Copyright (C) 2022 Visiblink
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import time
from sys import exit
# The functions below are adapted from examples 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('~'), '.local', 'share', 'antenna', 'antennarc'),'w')
f.write(str(dic))
f.close()
def load_dict_from_file():
f = open(os.path.join(os.path.expanduser('~'), '.local', 'share', 'antenna', 'antennarc'),'r')
data=f.read()
f.close()
return eval(data)
# End of code adapted from examples by user junglejet.
def main_menu():
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'))==False:
os.mkdir(os.path.join(os.path.expanduser('~'), '.local', 'share', 'antenna'))
station_list={}
else:
station_list = load_dict_from_file()
station_display(station_list)
print("Command Menu:")
print()
print("a add a station shortcut")
print("d delete a station shortcut")
print("e edit a station shortcut")
print("m move a station shortcut up or down")
print("p play a manually entered URL")
print("q exit")
print()
menu_user_choice = input("Enter the number or letter of your selection: ")
if menu_user_choice.isnumeric() == False:
if menu_user_choice == "q":
print()
exit()
elif menu_user_choice == "a":
add_station(station_list)
elif menu_user_choice == "d":
input_error_filter("delete", station_list)
elif menu_user_choice == "e":
input_error_filter("edit", station_list)
elif menu_user_choice == "m":
input_error_filter("move", station_list)
elif menu_user_choice == "p":
print()
user_stream_url = input("Enter the stream URL: ")
play_stream(user_stream_url)
else:
main_menu()
elif int(menu_user_choice) == 0:
main_menu()
elif int(menu_user_choice) <= len(station_list):
print()
print("Now Playing -", station_list[int(menu_user_choice)]['Name'])
play_stream(station_list[int(menu_user_choice)]['sURL'])
else:
main_menu()
def station_display(station_list):
os.system('clear')
print()
print("Antenna - Internet Radio Player")
print("===============================")
print()
print("Station List:")
print()
total_entries = int(len(station_list))
if total_entries <= 12:
for i in station_list:
print(i,station_list[i]['Name'])
if total_entries > 12 and total_entries < 25:
col1 = 1
col2 = int(total_entries/2) + total_entries%2 + 1
rows = int(total_entries/2) + total_entries%2
for i in range (0, rows):
if col1 < rows:
print(f"{str(col1) + ' ' + station_list[col1]['Name']:<30}{str(col2) + ' ' + station_list[col2]['Name']}")
if col1 == rows and col2 > total_entries:
print(col1, station_list[col1]['Name'])
if col1 == rows and col2 == total_entries:
print(f"{str(col1) + ' ' + station_list[col1]['Name']:<30}{str(col2) + ' ' + station_list[col2]['Name']}")
col1 = col1 + 1
col2 = col2 + 1
if total_entries > 24:
rmndr = total_entries%3
if rmndr == 0:
rows = int(total_entries/3)
else:
rows = int(total_entries/3) + 1
col1 = 1
col2 = rows + 1
col3 = (rows*2) + 1
for i in range (0, rows):
if col3 > total_entries:
print(f"{str(col1) + ' ' + station_list[col1]['Name']:<30}{str(col2) + ' ' + station_list[col2]['Name']}")
else:
print(f"{str(col1) + ' ' + station_list[col1]['Name']:<30}{str(col2) + ' ' + station_list[col2]['Name']:<30}{str(col3) + ' ' + station_list[col3]['Name']}")
col1 = col1 + 1
col2 = col2 + 1
col3 = col3 + 1
print()
def input_error_filter (action, station_list):
print()
user_choice = input(f"Enter the number of the station to {action}: ")
if user_choice.isnumeric() == False:
input_error_message()
user_choice_integer = int(user_choice)
total_entries = len(station_list)
if user_choice_integer < 1 or user_choice_integer > total_entries:
input_error_message()
eval(action+"_station")(station_list, user_choice_integer, total_entries) #this calls a function using a variable: eval(action+"_station") produces the function name.
def input_error_message():
print()
print("Invalid Entry - Returning to Main Menu")
time.sleep(1)
main_menu()
def add_station(station_list):
print()
new_station_URL = input("Enter the station URL: ")
print()
new_station_name = input("Enter the station name: ")
new_station = {len(station_list)+1: {'Name': new_station_name, 'sURL': new_station_URL}}
save_dict_to_file(station_list | new_station)
main_menu()
def delete_station(station_list, user_choice_integer, total_entries):
new_station_list = {}
for i in station_list:
if i < user_choice_integer:
new_station_list[i] = {'Name': station_list[i]['Name'], 'sURL': station_list[i]['sURL']}
if i > user_choice_integer:
new_station_list[i-1] = {'Name': station_list[i]['Name'], 'sURL': station_list[i]['sURL']}
save_dict_to_file(new_station_list)
main_menu()
def edit_station(station_list, user_choice_integer, total_entries): # total_entries is not used in this function, but the generic call from station_filter() requires it.
print()
print("The current station name is:", station_list[user_choice_integer]['Name'])
print()
edited_name = input("Enter a new name or leave blank to keep the current entry: ")
if edited_name != "":
edited_name_value={'Name':edited_name}
station_list[user_choice_integer].update(edited_name_value)
save_dict_to_file(station_list)
print()
print("The current station URL is:", station_list[user_choice_integer]['sURL'])
print()
edited_URL = input("Enter a new URL or leave blank to keep the current entry: ")
if edited_URL != "":
edited_URL_value={'sURL':edited_URL}
station_list[user_choice_integer].update(edited_URL_value)
save_dict_to_file(station_list)
main_menu()
def move_station (station_list, user_choice_integer, total_entries):
station_display(station_list)
print("Command Menu:")
print()
print("u move the station up")
print("d move the station down")
print("q return to the main menu")
print()
station_movement = input("Enter your selection: ")
if station_movement.isnumeric() == True:
move_station(station_list, user_choice_integer, total_entries)
elif station_movement == "q":
main_menu()
elif station_movement == "u":
if user_choice_integer == 1:
print()
print("The station is already at the first position.")
time.sleep(1)
move_station(station_list, user_choice_integer, total_entries)
new_station_list = {}
merged_station_list = {}
new_station_list[user_choice_integer-1] = {'Name': station_list[user_choice_integer]['Name'], 'sURL': station_list[user_choice_integer]['sURL']}
new_station_list[user_choice_integer] = {'Name': station_list[user_choice_integer-1]['Name'], 'sURL': station_list[user_choice_integer-1]['sURL']}
merged_station_list = station_list | new_station_list
save_dict_to_file(merged_station_list)
user_choice_integer=user_choice_integer-1
move_station(merged_station_list, user_choice_integer, total_entries)
elif station_movement == "d":
if user_choice_integer == total_entries:
print()
print("The station is already at last position.")
time.sleep(1)
move_station(station_list, user_choice_integer, total_entries)
new_station_list = {}
merged_station_list = {}
new_station_list[user_choice_integer+1] = {'Name': station_list[user_choice_integer]['Name'], 'sURL': station_list[user_choice_integer]['sURL']}
new_station_list[user_choice_integer] = {'Name': station_list[user_choice_integer+1]['Name'], 'sURL': station_list[user_choice_integer+1]['sURL']}
merged_station_list = station_list | new_station_list
save_dict_to_file(merged_station_list)
user_choice_integer=user_choice_integer+1
move_station(merged_station_list, user_choice_integer, total_entries)
else:
move_station(station_list, user_choice_integer, total_entries)
def play_stream(stream_url):
print()
print("Press 'q' to stop playback ")
os.system(f'gst123 -q {stream_url}')
main_menu()
main_menu()