Skip to content

Commit c888f13

Browse files
committed
adding force http connections config option
1 parent a560867 commit c888f13

File tree

5 files changed

+94
-25
lines changed

5 files changed

+94
-25
lines changed

pyradio/config.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,9 @@ class PyRadioConfig(PyRadioStations):
919919
opts[ 'default_playlist' ] = [ 'Def. playlist: ', 'stations' ]
920920
opts[ 'default_station' ] = [ 'Def station: ', 'False' ]
921921
opts[ 'default_encoding' ] = [ 'Def. encoding: ', 'utf-8' ]
922+
opts[ 'conn_title' ] = [ 'Connection Options: ', '' ]
922923
opts[ 'connection_timeout' ] = [ 'Connection timeout: ', '10' ]
924+
opts[ 'force_http' ] = [ 'Force http connections: ', False ]
923925
opts[ 'theme_title' ] = [ 'Theme Options', '' ]
924926
opts[ 'theme' ] = [ 'Theme: ', 'dark' ]
925927
opts[ 'use_transparency' ] = [ 'Use transparency: ', False ]
@@ -938,6 +940,7 @@ def __init__(self):
938940
self.auto_save_playlist = False
939941
self.default_playlist = 'stations'
940942
self.default_station = 'False'
943+
self.force_http = False
941944
self.default_encoding = 'utf-8'
942945
self.connection_timeout = '10'
943946
self.theme = 'dark'
@@ -978,6 +981,15 @@ def player(self, val):
978981
self.opts['player'][1] = val
979982
self.opts['dirty_config'][1] = True
980983

984+
@property
985+
def force_http(self):
986+
return self.opts['force_http'][1]
987+
988+
@force_http.setter
989+
def force_http(self, val):
990+
self.opts['force_http'][1] = val
991+
self.opts['dirty_config'][1] = True
992+
981993
@property
982994
def use_transparency(self):
983995
return self.opts['use_transparency'][1]
@@ -1043,13 +1055,34 @@ def auto_save_playlist(self, val):
10431055

10441056
@property
10451057
def connection_timeout(self):
1058+
""" connection timeout as string """
10461059
return self.opts['connection_timeout'][1]
10471060

10481061
@connection_timeout.setter
10491062
def connection_timeout(self, val):
10501063
self.opts['connection_timeout'][1] = val
10511064
self.opts['dirty_config'][1] = True
10521065

1066+
@property
1067+
def connection_timeout_int(self):
1068+
""" connection timeout as integer
1069+
if < 5 or > 60, set to 10
1070+
On error set to 10
1071+
Read only
1072+
"""
1073+
try:
1074+
ret = int(self.opts['connection_timeout'][1])
1075+
if not 5 <= ret <= 60:
1076+
ret = 10
1077+
except:
1078+
ret = 10
1079+
self.opts['connection_timeout'][1] = str(ret)
1080+
return ret
1081+
1082+
@connection_timeout_int.setter
1083+
def connection_timeout_int(self, val):
1084+
return
1085+
10531086
@property
10541087
def theme(self):
10551088
return self.opts['theme'][1]
@@ -1161,6 +1194,8 @@ def read_config(self):
11611194
self.opts['player'][1] = sp[1].lower().strip()
11621195
elif sp[0] == 'connection_timeout':
11631196
self.opts['connection_timeout'][1] = sp[1].strip()
1197+
# check integer number and set to 10 if error
1198+
x = self.connection_timeout_int
11641199
elif sp[0] == 'default_encoding':
11651200
self.opts['default_encoding'][1] = sp[1].strip()
11661201
elif sp[0] == 'theme':
@@ -1195,6 +1230,11 @@ def read_config(self):
11951230
self.opts['use_transparency'][1] = True
11961231
else:
11971232
self.opts['use_transparency'][1] = False
1233+
elif sp[0] == 'force_http':
1234+
if sp[1].lower() == 'true':
1235+
self.opts['force_http'][1] = True
1236+
else:
1237+
self.opts['force_http'][1] = False
11981238
self.opts['dirty_config'][1] = False
11991239
return 0
12001240

@@ -1268,6 +1308,15 @@ def save_config(self):
12681308
# Default value: 10
12691309
connection_timeout = {4}
12701310
1311+
# Force http connections
1312+
# Most radio stations use plain old http protocol to broadcast, but
1313+
# some of them use https. If this is enabled, all connections will
1314+
# use http; results depend on the combination of station/player.
1315+
#
1316+
# Valid values: True, true, False, false
1317+
# Default value: False
1318+
force_http = {5}
1319+
12711320
# Default theme
12721321
# Hardcooded themes:
12731322
# dark (default) (8 colors)
@@ -1277,7 +1326,7 @@ def save_config(self):
12771326
# black_on_white (bow) (256 colors)
12781327
# white_on_black (wob) (256 colors)
12791328
# Default value = 'dark'
1280-
theme = {5}
1329+
theme = {6}
12811330
12821331
# Transparency setting
12831332
# If False, theme colors will be used.
@@ -1286,7 +1335,7 @@ def save_config(self):
12861335
# not running, the terminal's background color will be used.
12871336
# Valid values: True, true, False, false
12881337
# Default value: False
1289-
use_transparency = {6}
1338+
use_transparency = {7}
12901339
12911340
12921341
# Playlist management
@@ -1295,20 +1344,20 @@ def save_config(self):
12951344
# every station deletion action
12961345
# Valid values: True, true, False, false
12971346
# Default value: True
1298-
confirm_station_deletion = {7}
1347+
confirm_station_deletion = {8}
12991348
13001349
# Specify whether you will be asked to confirm
13011350
# playlist reloading, when the playlist has not
13021351
# been modified within Pyradio
13031352
# Valid values: True, true, False, false
13041353
# Default value: True
1305-
confirm_playlist_reload = {8}
1354+
confirm_playlist_reload = {9}
13061355
13071356
# Specify whether you will be asked to save a
13081357
# modified playlist whenever it needs saving
13091358
# Valid values: True, true, False, false
13101359
# Default value: False
1311-
auto_save_playlist = {9}
1360+
auto_save_playlist = {10}
13121361
13131362
'''
13141363
copyfile(self.config_file, self.config_file + '.restore')
@@ -1321,6 +1370,7 @@ def save_config(self):
13211370
self.opts['default_station'][1],
13221371
self.opts['default_encoding'][1],
13231372
self.opts['connection_timeout'][1],
1373+
self.opts['force_http'][1],
13241374
self.opts['theme'][1],
13251375
self.opts['use_transparency'][1],
13261376
self.opts['confirm_station_deletion'][1],

pyradio/config_window.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ class PyRadioConfigWindow(object):
5656
_help_text.append(['This is the encoding used by default when reading data provided by a station such as song title, etc. If reading said data ends up in an error, "utf-8" will be used instead.', '|',
5757
'If changed, playback must be restarted so that changes take effect.',
5858
'|', 'Default value: utf-8'])
59+
_help_text.append(None)
5960
_help_text.append(['PyRadio will wait for this number of seconds to get a station/server message indicating that playback has actually started.', '|',
6061
'If this does not happen within this number of seconds after the connection is initiated, PyRadio will consider the station unreachable, and display the "Failed to connect to: station" message.', '|', 'Press "h"/Left or "l"/Right to change value.',
6162
'|', 'Valid values: 5 - 60', 'Default value: 10'])
63+
_help_text.append(['Most radio stations use plain old http protocol to broadcast, but some of them use https.', '|', 'If this is enabled, all connections will use http; results depend on the combination of station/player.', '|', 'This value is read at program startup, use Ctrl-S to change its effect while mid-session.',
64+
'|', 'Default value: False'])
6265
_help_text.append(None)
6366
_help_text.append(['The theme to be used by default.', '|',
6467
'This is the equivalent to the -t , --theme command line option.', '|',
@@ -266,6 +269,7 @@ def _load_default_values(self):
266269
# Transparency
267270
#self._old_use_transparency = self._config_options['use_transparency'][1]
268271
self._config_options[ 'use_transparency' ][1] = False
272+
self._config_options[ 'force_http' ][1] = False
269273
self._toggle_transparency_function(changed_from_config_window=True, force_value=False)
270274
self._config_options[ 'playlist_manngement_title' ][1] = ''
271275
self._config_options[ 'confirm_station_deletion' ][1] = True
@@ -413,7 +417,8 @@ def keypress(self, char):
413417
return self.n_u.SELECT_STATION_MODE, []
414418
elif sel == 'confirm_station_deletion' or \
415419
sel == 'confirm_playlist_reload' or \
416-
sel == 'auto_save_playlist':
420+
sel == 'auto_save_playlist' or \
421+
sel == 'force_http':
417422
self._config_options[sel][1] = not self._config_options[sel][1]
418423
self.refresh_selection()
419424
elif sel == 'use_transparency':

pyradio/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
IMPLEMENTED_PLAYERS =('mpv', 'mplayer', 'cvlc')
1616

1717
@contextmanager
18-
def pyradio_config_manager():
18+
def pyradio_config_file():
1919
cf = PyRadioConfig()
2020
try:
2121
yield cf
@@ -89,7 +89,7 @@ def shell():
8989
args = parser.parse_args()
9090
sys.stdout.flush()
9191

92-
with pyradio_config_manager() as pyradio_config:
92+
with pyradio_config_file() as pyradio_config:
9393

9494
if args.unlock:
9595
pyradio_config.locked = False

pyradio/player.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,14 @@ class Player(object):
148148
def __init__(self, outputStream,
149149
config_encoding,
150150
playback_timeout,
151+
force_http,
151152
playback_timeout_counter,
152153
playback_timeout_handler,
153154
info_display_handler):
154155
self.outputStream = outputStream
155156
self.config_encoding = config_encoding
156-
try:
157-
self.playback_timeout = int(playback_timeout)
158-
except:
159-
self.playback_timeout = 10
157+
self.playback_timeout = playback_timeout
158+
self.force_http = force_http
160159
self.playback_timeout_counter = playback_timeout_counter
161160
self.playback_timeout_handler = playback_timeout_handler
162161
self.info_display_handler = info_display_handler
@@ -165,6 +164,12 @@ def __init__(self, outputStream,
165164
def __del__(self):
166165
self.close()
167166

167+
def _url_to_use(self, streamUrl):
168+
if self.force_http:
169+
return streamUrl.replace('https://', 'http://')
170+
else:
171+
return streamUrl
172+
168173
def save_volume(self):
169174
pass
170175

@@ -872,13 +877,20 @@ def play(self, name, streamUrl, encoding = ''):
872877
t.start()
873878
# start playback check timer thread
874879
self.stop_timeout_counter_thread = False
880+
logger.error('=========================')
881+
logger.error('function self.playback_timeout_counter = {}'.format(self.playback_timeout_counter))
882+
logger.error('int self.playback_timeout = {}'.format(self.playback_timeout))
883+
logger.error('str self.name = {}'.format(self.name))
884+
logger.error('=========================')
875885
try:
876886
self.connection_timeout_thread = threading.Thread(
877887
target=self.playback_timeout_counter,
878888
args=(self.playback_timeout,
879889
self.name,
880890
lambda: self.stop_timeout_counter_thread))
881891
self.connection_timeout_thread.start()
892+
if (logger.isEnabledFor(logging.ERROR)):
893+
logger.error("playback detection thread started")
882894
except:
883895
self.connection_timeout_thread = None
884896
if (logger.isEnabledFor(logging.ERROR)):
@@ -949,7 +961,10 @@ def toggleMute(self):
949961
self._mute()
950962
if self.muted:
951963
if self.delay_thread is not None:
952-
self.delay_thread.cancel()
964+
try:
965+
self.delay_thread.cancel()
966+
except:
967+
pass
953968
self.title_prefix = '[Muted] '
954969
self.show_volume = False
955970
else:
@@ -1084,7 +1099,7 @@ def _buildStartOpts(self, streamUrl, playList=False):
10841099
""" Builds the options to pass to subprocess."""
10851100

10861101
""" Test for newer MPV versions as it supports different IPC flags. """
1087-
p = subprocess.Popen([self.PLAYER_CMD, "--input-ipc-server"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=False)
1102+
p = subprocess.Popen([self.PLAYER_CMD, "--no-video", "--input-ipc-server"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=False)
10881103
out = p.communicate()
10891104
if "not found" not in str(out[0]):
10901105
if logger.isEnabledFor(logging.DEBUG):
@@ -1095,17 +1110,16 @@ def _buildStartOpts(self, streamUrl, playList=False):
10951110
logger.debug("--input-ipc-server is not supported.")
10961111
newerMpv = 0;
10971112

1098-
http_url = streamUrl.replace('https://', 'http://')
10991113
if playList:
11001114
if newerMpv:
1101-
opts = [self.PLAYER_CMD, "--quiet", "--playlist=" + http_url, "--input-ipc-server=" + self.mpvsocket]
1115+
opts = [self.PLAYER_CMD, "--no-video", "--quiet", "--playlist=" + self._url_to_use(streamUrl), "--input-ipc-server=" + self.mpvsocket]
11021116
else:
1103-
opts = [self.PLAYER_CMD, "--quiet", "--playlist=" + http_url, "--input-unix-socket=" + self.mpvsocket]
1117+
opts = [self.PLAYER_CMD, "--no-video", "--quiet", "--playlist=" + self._url_to_use(streamUrl), "--input-unix-socket=" + self.mpvsocket]
11041118
else:
11051119
if newerMpv:
1106-
opts = [self.PLAYER_CMD, "--quiet", http_url, "--input-ipc-server=" + self.mpvsocket]
1120+
opts = [self.PLAYER_CMD, "--no-video", "--quiet", self._url_to_use(streamUrl), "--input-ipc-server=" + self.mpvsocket]
11071121
else:
1108-
opts = [self.PLAYER_CMD, "--quiet", http_url, "--input-unix-socket=" + self.mpvsocket]
1122+
opts = [self.PLAYER_CMD, "--no-video", "--quiet", self._url_to_use(streamUrl), "--input-unix-socket=" + self.mpvsocket]
11091123
if self.USE_PROFILE == -1:
11101124
self.USE_PROFILE = self._configHasProfile()
11111125

@@ -1412,11 +1426,10 @@ def _configHasProfile(self):
14121426

14131427
def _buildStartOpts(self, streamUrl, playList=False):
14141428
""" Builds the options to pass to subprocess."""
1415-
http_url = streamUrl.replace('https://', 'http://')
14161429
if playList:
1417-
opts = [self.PLAYER_CMD, "-quiet", "-playlist", http_url]
1430+
opts = [self.PLAYER_CMD, "-vo", "null", "-quiet", "-playlist", self._url_to_use(streamUrl)]
14181431
else:
1419-
opts = [self.PLAYER_CMD, "-quiet", http_url]
1432+
opts = [self.PLAYER_CMD, "-vo", "-quiet", self._url_to_use(streamUrl)]
14201433
if self.USE_PROFILE == -1:
14211434
self.USE_PROFILE = self._configHasProfile()
14221435

@@ -1507,8 +1520,7 @@ def save_volume(self):
15071520

15081521
def _buildStartOpts(self, streamUrl, playList=False):
15091522
""" Builds the options to pass to subprocess."""
1510-
#opts = [self.PLAYER_CMD, "-Irc", "--quiet", streamUrl]
1511-
opts = [self.PLAYER_CMD, "-Irc", "-vv", streamUrl.replace('https://', 'http://')]
1523+
opts = [self.PLAYER_CMD, "--no-video", "-Irc", "-vv", self._url_to_use(streamUrl)]
15121524
return opts
15131525

15141526
def _mute(self):

pyradio/radio.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ def setup(self, stdscr):
387387
try:
388388
self.player = player.probePlayer(requested_player=self.requested_player)(self.log,
389389
self._cnf.default_encoding,
390-
self._cnf.connection_timeout,
390+
self._cnf.connection_timeout_int,
391+
self._cnf.force_http,
391392
self.playbackTimeoutCounter,
392393
self.connectionFailed,
393394
self._show_station_info_from_thread)
@@ -889,6 +890,7 @@ def playSelection(self):
889890

890891
def playbackTimeoutCounter(self, *args):
891892
timeout = args[0]
893+
logger.info('DE \n\ntimeout = {}\n\n'.format(timeout))
892894
station_name = args[1]
893895
stop = args[2]
894896
if stop():

0 commit comments

Comments
 (0)