Skip to content

Commit 80785ae

Browse files
committed
- handling opening empty Radio Browser result
- adding hex to rgb functions - Radio Browser search is probably broken
1 parent 00e3ed8 commit 80785ae

File tree

5 files changed

+111
-76
lines changed

5 files changed

+111
-76
lines changed

pyradio/browser.py

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ def initialize(self):
273273
'post_data': {'order': 'votes', 'reverse': 'true'},
274274
})
275275
self._search_history_index = 1
276+
277+
self._search_history.append({
278+
'type': 'search',
279+
'term': '',
280+
'post_data': {'name': 'jazsssss'},
281+
})
282+
self._search_history_index = 2
276283
return True
277284
return False
278285

@@ -477,9 +484,12 @@ def search(self, go_back_in_history=True):
477484
try:
478485
# r = requests.get(url=url)
479486
r = self._session.get(url=url, headers=self._headers, params=post_data, timeout=(self._search_timeout, 2 * self._search_timeout))
487+
logger.error('== request sent ')
480488
r.raise_for_status()
489+
logger.error('== request raised ')
481490
self._raw_stations = self._extract_data(json.loads(r.text))
482-
# logger.error('DE \n\n{}'.format(self._raw_stations))
491+
logger.error('== got reply')
492+
logger.error('DE \n\n{}'.format(self._raw_stations))
483493
ret = True, go_back_in_history
484494
except requests.exceptions.RequestException as e:
485495
if logger.isEnabledFor(logging.ERROR):
@@ -527,6 +537,14 @@ def _get_search_elements(self, a_search):
527537
elif a_type == 'tags':
528538
self.search_by = 'tags'
529539

540+
if self.search_by is None:
541+
if p_data:
542+
if 'name' in p_data.keys():
543+
self.search_by = 'name'
544+
545+
if self.search_by is None:
546+
self.search_by = 'name'
547+
530548
def get_next(self, search_term, start=0, stop=None):
531549
if search_term:
532550
for n in range(start, len(self._raw_stations)):
@@ -625,6 +643,13 @@ def _format_url(self, a_search):
625643
)
626644
self._search_type = 1
627645

646+
elif a_search['type'] == 'search':
647+
url = 'http://{0}{1}'.format(
648+
self._server,
649+
'/json/stations/search'
650+
)
651+
self._search_type = 2
652+
628653
return url
629654

630655
def format_empty_line(self, width):
@@ -1105,14 +1130,21 @@ def do_search(self, parent=None, init=False):
11051130

11061131
class RadioBrowserInfoSearchWindow(object):
11071132

1133+
# search_by_items = (
1134+
# 'No search term',
1135+
# 'Name',
1136+
# 'Tag',
1137+
# 'Country',
1138+
# 'State',
1139+
# 'Codec',
1140+
# 'Language',
1141+
# )
1142+
11081143
search_by_items = (
1109-
'No search term',
1110-
'Name',
1111-
'Tag',
1112-
'Country',
1113-
'State',
1114-
'Codec',
1115-
'Language',
1144+
'Votes',
1145+
'Clicks',
1146+
'Recent click',
1147+
'Recently changed'
11161148
)
11171149

11181150
sort_by_items = (
@@ -1195,22 +1227,27 @@ def show(self):
11951227
for i, n in enumerate(self._widgets):
11961228
if n is None:
11971229
if i == 0:
1198-
self._widgets[0] = SimpleCursesLineEdit(
1199-
parent=self._win,
1200-
width=-2,
1201-
begin_y=3,
1202-
begin_x=2,
1203-
boxed=False,
1204-
has_history=False,
1205-
caption='',
1206-
box_color=curses.color_pair(9),
1207-
caption_color=curses.color_pair(4),
1208-
edit_color=curses.color_pair(9),
1209-
cursor_color=curses.color_pair(8),
1210-
unfocused_color=curses.color_pair(5),
1211-
string_changed_handler='')
1212-
self._widgets[0].bracket = False
1213-
self._line_editor = self._widgets[0]
1230+
self._widgets[2] = SimpleCursesCheckBox(
1231+
1, 2, 'Display by',
1232+
curses.color_pair(9),
1233+
curses.color_pair(4),
1234+
curses.color_pair(5))
1235+
#self._widgets[0] = SimpleCursesLineEdit(
1236+
# parent=self._win,
1237+
# width=-2,
1238+
# begin_y=3,
1239+
# begin_x=2,
1240+
# boxed=False,
1241+
# has_history=False,
1242+
# caption='',
1243+
# box_color=curses.color_pair(9),
1244+
# caption_color=curses.color_pair(4),
1245+
# edit_color=curses.color_pair(9),
1246+
# cursor_color=curses.color_pair(8),
1247+
# unfocused_color=curses.color_pair(5),
1248+
# string_changed_handler='')
1249+
#self._widgets[0].bracket = False
1250+
#self._line_editor = self._widgets[0]
12141251
elif i == 1:
12151252
''' search by '''
12161253
self._widgets[i] = SimpleCursesWidgetColumns(
@@ -1249,7 +1286,7 @@ def show(self):
12491286
elif i == 4:
12501287
'''' sort ascending / descending '''
12511288
self._widgets[4] = SimpleCursesCheckBox(
1252-
self._widgets[3].Y + self._widgets[3].height + 2, self._widgets[3].X - 2 + self._widgets[3].margin,
1289+
self._widgets[3].Y + self._widgets[3].height + 1, self._widgets[3].X - 2 + self._widgets[3].margin,
12531290
'Sort descending',
12541291
curses.color_pair(9), curses.color_pair(4), curses.color_pair(5))
12551292
elif i == 5:
@@ -1289,16 +1326,15 @@ def show(self):
12891326
self._h_buttons.calculate_buttons_position()
12901327
for n in range(1, len(self._widgets)):
12911328
if self._widgets[n]:
1292-
if i in (2, 4):
1293-
if i == 2:
1329+
if n in (2, 4):
1330+
if n == 2:
12941331
self._widgets[2].Y = self._widgets[1].Y + self._widgets[1].height + 2
12951332
else:
1296-
self._widgets[4].Y = self._widgets[3].Y + self._widgets[3].height + 2
1333+
self._widgets[4].Y = self._widgets[3].Y + self._widgets[3].height + 1
12971334
self._widgets[4].X = self._widgets[3].X - 2 + self._widgets[3].margin
1298-
self._widgets[i].window = self._win
1299-
self._widgets[i].resize()
1300-
else:
1301-
self._widgets[n].show()
1335+
self._widgets[n].move()
1336+
# self._widgets[n].resize()
1337+
self._widgets[n].show()
13021338
self._win.refresh()
13031339

13041340
# self._refresh()

pyradio/player.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,6 @@ class Player(object):
213213

214214
all_config_files = {}
215215

216-
''' the function to call when a station starts to play
217-
(playback detected) to add a click to its click counter
218-
'''
219-
click_station_function = None
220-
221216
def __init__(self,
222217
config,
223218
outputStream,
@@ -523,10 +518,6 @@ def _stop_delay_thread(self):
523518
pass
524519
self.delay_thread = None
525520

526-
''' click station (if applicable) '''
527-
if self.click_station_function and self.isPlaying():
528-
self.click_station_function()
529-
530521
def _is_in_playback_token(self, a_string):
531522
for a_token in self._playback_token_tuple:
532523
if a_token in a_string:

pyradio/radio.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,18 +1142,11 @@ def playSelection(self, restart=False):
11421142
self.playback_timeout = int(self._cnf.connection_timeout_int)
11431143
except ValueError:
11441144
self.playback_timeout = 10
1145-
if self._cnf.browsing_station_service and self.playback_timeout == 0:
1146-
''' if playback timeout check if disabled,
1147-
click the station immediately (even if the station
1148-
may turn out to be broken)
1149-
'''
1150-
self._click_station()
1145+
self._click_station()
11511146

11521147
def _click_station(self):
11531148
if self._cnf._online_browser:
11541149
self._cnf._online_browser.click(self.playing)
1155-
else:
1156-
self.player.click_station_function = None
11571150

11581151
def playbackTimeoutCounter(self, *args):
11591152
timeout = args[0]
@@ -3026,10 +3019,7 @@ def _open_playlist(self, a_url=None):
30263019
return
30273020

30283021
''' make sure we don't send a wrong click '''
3029-
self.player.click_station_function = None
3030-
logger.error('---=== Click function is None ===---')
30313022
self._cnf._online_browser.search()
3032-
self.player.click_station_function = self._click_station
30333023
else:
30343024
self._cnf.remove_from_playlist_history()
30353025
self._print_unknown_browser_service()
@@ -3044,7 +3034,6 @@ def _open_playlist(self, a_url=None):
30443034
self._cnf.browsing_station_service = False
30453035
elif self._cnf.register_to_open:
30463036
''' open a register '''
3047-
self.player.click_station_function = None
30483037
self._playlist_in_editor = self._cnf.register_to_open
30493038
if logger.isEnabledFor(logging.DEBUG):
30503039
logger.debug('opening register: ' + self._cnf.register_to_open)
@@ -3079,7 +3068,6 @@ def _open_playlist(self, a_url=None):
30793068
#else:
30803069
# txt = '''Reading playlists. Please wait...'''
30813070
#self._show_help(txt, self.ws.NORMAL_MODE, caption=' ', prompt=' ', is_message=True)
3082-
self.player.click_station_function = None
30833071
if self.ws.operation_mode != self.ws.PLAYLIST_MODE:
30843072
self.selections[self.ws.operation_mode] = [self.selection, self.startPos, self.playing, self._cnf.stations]
30853073
self.ws.window_mode = self.ws.PLAYLIST_MODE
@@ -3125,28 +3113,28 @@ def _return_from_online_browser_search(self, ret):
31253113
self._cnf.browsing_station_service = False
31263114
return
31273115

3116+
''' get stations with online field '''
31283117
tmp_stations = self._cnf._online_browser.stations(2)
31293118

31303119
''' set browser parent so that it resizes correctly '''
31313120
if self._cnf.browsing_station_service:
31323121
self._cnf._online_browser.parent = self.bodyWin
31333122

3134-
if tmp_stations:
3135-
self._cnf.stations = tmp_stations[:]
3136-
self.stations = self._cnf.stations
3137-
self._cnf._online_browser.vote_callback = self._print_vote_result
3138-
self._cnf.number_of_stations = len(self.stations)
3139-
self._cnf.dirty_playlist = False
3140-
#self._cnf.add_to_playlist_history(self._cnf.online_browser.BASE_URL, '', self._cnf.online_browser.TITLE, browsing_station_service=True)
3141-
self._cnf.station_path = self._cnf.online_browser.BASE_URL
3142-
self._cnf.station_title = self._cnf.online_browser.title
3143-
self.number_of_items = len(self.stations)
3144-
self.selection = 0
3145-
self.startPos = 0
3146-
self.setupAndDrawScreen()
3147-
self.detect_if_player_exited = False
3148-
self._align_stations_and_refresh(self.ws.operation_mode)
3149-
self._set_active_stations()
3123+
self._cnf.stations = tmp_stations[:]
3124+
self.stations = self._cnf.stations
3125+
self._cnf._online_browser.vote_callback = self._print_vote_result
3126+
self._cnf.number_of_stations = len(self.stations)
3127+
self._cnf.dirty_playlist = False
3128+
#self._cnf.add_to_playlist_history(self._cnf.online_browser.BASE_URL, '', self._cnf.online_browser.TITLE, browsing_station_service=True)
3129+
self._cnf.station_path = self._cnf.online_browser.BASE_URL
3130+
self._cnf.station_title = self._cnf.online_browser.title
3131+
self.number_of_items = len(self.stations)
3132+
self.selection = 0
3133+
self.startPos = 0
3134+
self.setupAndDrawScreen()
3135+
self.detect_if_player_exited = False
3136+
self._align_stations_and_refresh(self.ws.operation_mode)
3137+
self._set_active_stations()
31503138

31513139
def _open_playlist_from_history(self,
31523140
reset=False,
@@ -3291,7 +3279,6 @@ def _open_playlist_from_history(self,
32913279
if logger.isEnabledFor(logging.INFO):
32923280
logger.info('Closing online browser!')
32933281
self._cnf.online_browser = None
3294-
self.player.click_station_function = None
32953282
''' check if browsing_station_service has changed '''
32963283
if not self._cnf.browsing_station_service and \
32973284
removed_playlist_history_item[-1]:
@@ -5597,7 +5584,6 @@ def keypress(self, char):
55975584
self.bodyWin.nodelay(False)
55985585
if char == -1:
55995586
''' ESCAPE '''
5600-
self.player.click_station_function = None
56015587
self._update_status_bar_right(status_suffix='')
56025588
if self.ws.operation_mode == self.ws.PLAYLIST_MODE:
56035589
''' return to stations view '''
@@ -6474,6 +6460,12 @@ def _redisplay_stations_and_playlists(self):
64746460
self.__displayBodyLine(lineNum, pad, None)
64756461
lineNum += 1
64766462
break
6463+
else:
6464+
''' we have no stations to display '''
6465+
if self._cnf.browsing_station_service:
6466+
''' we have to display emplty lines '''
6467+
for n in range(0, self.bodyMaxY + 1):
6468+
self.__displayBodyLine(n, pad, None)
64776469

64786470
if self._cnf.browsing_station_service:
64796471
if self._cnf.internal_header_height > 0:

pyradio/simple_curses_widgets.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ def Y(self):
4646

4747
@Y.setter
4848
def Y(self, value):
49-
raise ValueError('parameter is read only')
49+
self._Y = value
5050

5151
@property
5252
def X(self):
5353
return self._X
5454

5555
@X.setter
5656
def X(self, value):
57-
raise ValueError('parameter is read only')
57+
self._X = value
5858

5959
@property
6060
def height(self):
@@ -829,6 +829,7 @@ def __init__(self,
829829
Inactive checkbox color (curses.color_pair)
830830
bracket_color
831831
The color of the brackets (curses.color_pair)
832+
Also the color to use when widget is disabled
832833
char
833834
The character to indicate a checked checkbox (string)
834835
checked
@@ -900,6 +901,14 @@ def resize(self):
900901
self._width = len(self._caption) + 4
901902
self._win = curses.newwin(1, self._width, self._Y, self._X)
902903

904+
def move(self, new_Y=None, new_X=None):
905+
if new_Y:
906+
self._Y = new_Y
907+
if new_X:
908+
self._X = new_X
909+
if self._win:
910+
self._win.mvwin(self._Y, self._X)
911+
903912
def show(self):
904913
'''Put the widget on the screen'''
905914
if self._win:

pyradio/themes.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
logger = logging.getLogger(__name__)
1414

15+
def hex_color_to_rgb(a_color_string):
16+
return tuple(int(a_color_string[i:i+2], 16) for i in (0, 2, 4))
17+
18+
def hex_color_to_curses_rgb(a_color_string):
19+
rgb = hex_color_to_rgb(a_color_string)
20+
return tuple(int(round(x * 1000.0 / 255.0)) for x in rgb)
21+
1522

1623
class PyRadioTheme(object):
1724
_colors = {}
@@ -387,15 +394,15 @@ def _get_max_color(self, a_theme_name, max_color):
387394
for a_check in checks:
388395
if a_theme_name.endswith(a_check):
389396
try:
390-
num_of_colors = int(a_check[1:])
397+
num_of_colors = int(a_check[1:]) - 1
391398
except:
392399
pass
393400
break
394401
checks = (8, 16, 256)
395402
if num_of_colors == 0 or num_of_colors not in checks:
396403
num_of_colors = max_color
397404
for check in checks:
398-
if num_of_colors <= check:
405+
if num_of_colors < check:
399406
return check
400407
return num_of_colors
401408

0 commit comments

Comments
 (0)