Skip to content

Commit 6c4ed19

Browse files
committed
- adding no search result notification for Radio Browser
- old result is not a class member any more
1 parent 80785ae commit 6c4ed19

File tree

3 files changed

+110
-57
lines changed

3 files changed

+110
-57
lines changed

pyradio/browser.py

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ def __init__(self,
240240
pyradio_info=None,
241241
search_return_function=None,
242242
message_function=None):
243+
'''
244+
When first_search is True, it means that we are opening
245+
the browser. If empty result is returned by the first
246+
browser search, we show an empty stations' list.
247+
if it is False and an empty result is returned by the first
248+
browser search, which means we are already in the browser's
249+
search screen, we just display the 'no result message'.
250+
All of this is done at radio.py
251+
'''
252+
self.first_search = True
243253
self._cnf = config
244254
if session:
245255
self._session = session
@@ -272,14 +282,13 @@ def initialize(self):
272282
'term': 'big band',
273283
'post_data': {'order': 'votes', 'reverse': 'true'},
274284
})
275-
self._search_history_index = 1
276285

277286
self._search_history.append({
278287
'type': 'search',
279288
'term': '',
280-
'post_data': {'name': 'jazsssss'},
289+
'post_data': {'name': 'jaz'},
281290
})
282-
self._search_history_index = 2
291+
self._search_history_index = 1
283292
return True
284293
return False
285294

@@ -465,8 +474,6 @@ def search(self, go_back_in_history=True):
465474
self._old_search_by = self.search_by
466475
self._sort = None
467476
url = self._format_url(self._search_history[self._search_history_index])
468-
if logger.isEnabledFor(logging.DEBUG):
469-
logger.debug('== search url = "{}"'.format(url))
470477
post_data = {}
471478
if self._search_history[self._search_history_index]['post_data']:
472479
post_data = deepcopy(self._search_history[self._search_history_index]['post_data'])
@@ -477,25 +484,31 @@ def search(self, go_back_in_history=True):
477484
post_data['limit'] = 100
478485
if not 'hidebroken' not in post_data.keys():
479486
post_data['hidebroken'] = True
480-
# url = 'https://' + self._server + '/json/stations/search'
487+
481488
if logger.isEnabledFor(logging.DEBUG):
482-
logger.debug('== headers = "{}"'.format(self._headers))
483-
logger.debug('== post_data = "{}"'.format(post_data))
489+
logger.debug(' == history = {}'.format(self._search_history[self._search_history_index]))
490+
logger.debug(' == url = "{}"'.format(url))
491+
logger.debug(' == headers = "{}"'.format(self._headers))
492+
logger.debug(' == post_data = "{}"'.format(post_data))
493+
494+
''' keep server results here '''
495+
new_raw_stations = []
496+
484497
try:
485-
# r = requests.get(url=url)
486498
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 ')
488499
r.raise_for_status()
489-
logger.error('== request raised ')
490-
self._raw_stations = self._extract_data(json.loads(r.text))
491-
logger.error('== got reply')
492-
logger.error('DE \n\n{}'.format(self._raw_stations))
493-
ret = True, go_back_in_history
500+
new_raw_stations = self._extract_data(json.loads(r.text))
501+
# logger.error('DE \n\n{}'.format(new_raw_stations))
502+
ret = True, len(new_raw_stations), go_back_in_history
494503
except requests.exceptions.RequestException as e:
495504
if logger.isEnabledFor(logging.ERROR):
496505
logger.error(e)
497506
self._raw_stations = []
498-
ret = False, go_back_in_history
507+
ret = False, 0, go_back_in_history
508+
509+
''' use server result '''
510+
if len(new_raw_stations) > 0:
511+
self._raw_stations = new_raw_stations[:]
499512

500513
if self._search_return_function:
501514
self._search_return_function(ret)
@@ -506,6 +519,7 @@ def _get_search_elements(self, a_search):
506519
values from a search dict.
507520
To be used with the sort function
508521
'''
522+
logger.error('DE search in function is "{}"'.format(a_search))
509523
a_term = a_search['term']
510524
p_data = a_search['post_data']
511525
self.search_by = None
@@ -516,12 +530,14 @@ def _get_search_elements(self, a_search):
516530
if 'reverse' in a_search['post_data']:
517531
self.reverse = True if a_search['post_data']['reverse'] == 'true' else False
518532

533+
logger.error('DE search by was "{}"'.format(self.search_by))
519534
if self.search_by is None:
520535
a_type = a_search['type']
521536
if a_type == 'byname':
522537
self.search_by = 'name'
523-
elif a_type == 'topvotes':
538+
elif a_type == 'topvote':
524539
self.search_by = 'votes'
540+
logger.error('DE search by is votes')
525541
elif a_type == 'clickcount':
526542
self.search_by = 'clickcount'
527543
elif a_type == 'bitrate':
@@ -541,9 +557,13 @@ def _get_search_elements(self, a_search):
541557
if p_data:
542558
if 'name' in p_data.keys():
543559
self.search_by = 'name'
560+
logger.error('DE search by is name (default)')
544561

545562
if self.search_by is None:
546563
self.search_by = 'name'
564+
logger.error('DE search by is name (default)')
565+
566+
logger.error('DE search by is "{}"'.format(self.search_by))
547567

548568
def get_next(self, search_term, start=0, stop=None):
549569
if search_term:
@@ -1227,27 +1247,27 @@ def show(self):
12271247
for i, n in enumerate(self._widgets):
12281248
if n is None:
12291249
if i == 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]
1250+
#self._widgets[2] = SimpleCursesCheckBox(
1251+
# 1, 2, 'Display by',
1252+
# curses.color_pair(9),
1253+
# curses.color_pair(4),
1254+
# curses.color_pair(5))
1255+
self._widgets[0] = SimpleCursesLineEdit(
1256+
parent=self._win,
1257+
width=-2,
1258+
begin_y=3,
1259+
begin_x=2,
1260+
boxed=False,
1261+
has_history=False,
1262+
caption='',
1263+
box_color=curses.color_pair(9),
1264+
caption_color=curses.color_pair(4),
1265+
edit_color=curses.color_pair(9),
1266+
cursor_color=curses.color_pair(8),
1267+
unfocused_color=curses.color_pair(5),
1268+
string_changed_handler='')
1269+
self._widgets[0].bracket = False
1270+
self._line_editor = self._widgets[0]
12511271
elif i == 1:
12521272
''' search by '''
12531273
self._widgets[i] = SimpleCursesWidgetColumns(

pyradio/radio.py

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,13 +1605,27 @@ def closeRecoveryNotification(self, *arg, **karg):
16051605
#arg[1].release()
16061606
self.refreshBody()
16071607

1608+
def _show_no_browser_results(self):
1609+
self._show_notification_with_delay(delay=1.5,
1610+
txt='''
1611+
_____Query fetched no results!!!______
1612+
1613+
__Please change your search criteria__
1614+
__________and try again...
1615+
''',
1616+
mode_to_set=self.ws.NO_BROWSER_SEARCH_RESULT_NOTIFICATION,
1617+
callback_function=self.closeTimedNotificationWindow)
1618+
16081619
def _show_no_more_playlist_history(self):
16091620
self._show_notification_with_delay(
16101621
txt='___Top of history reached!!!___',
16111622
mode_to_set=self.ws.HISTORY_EMPTY_NOTIFICATION,
1612-
callback_function=self.closeHistoryEmptyNotification)
1623+
callback_function=self.closeTimedNotificationWindow)
16131624

1614-
def closeHistoryEmptyNotification(self):
1625+
def closeTimedNotificationWindow(self):
1626+
''' closes a window opend by _show_notification_with_delay
1627+
(threaded delayed notification)
1628+
'''
16151629
self.ws.close_window()
16161630
self.refreshBody()
16171631

@@ -3104,7 +3118,7 @@ def _return_from_online_browser_search(self, ret):
31043118
'''
31053119

31063120
if not ret[0]:
3107-
if ret[1]:
3121+
if ret[2]:
31083122
self._goto_history_back_handler()
31093123
self._print_service_connection_error()
31103124
else:
@@ -3120,21 +3134,34 @@ def _return_from_online_browser_search(self, ret):
31203134
if self._cnf.browsing_station_service:
31213135
self._cnf._online_browser.parent = self.bodyWin
31223136

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()
3137+
if ret[1] == 0 and not self._cnf._online_browser.first_search:
3138+
logger.error('DE --== no items found ==--\noperating mode = {}'.format(self.ws.operation_mode))
3139+
''' go back to search mode '''
3140+
self.ws.operation_mode = self.ws.BROWSER_SEARCH_MODE
3141+
''' display no results message '''
3142+
self._show_no_browser_results()
3143+
3144+
else:
3145+
self._cnf.stations = tmp_stations[:]
3146+
self.stations = self._cnf.stations
3147+
self._cnf._online_browser.vote_callback = self._print_vote_result
3148+
self._cnf.number_of_stations = len(self.stations)
3149+
self._cnf.dirty_playlist = False
3150+
#self._cnf.add_to_playlist_history(self._cnf.online_browser.BASE_URL, '', self._cnf.online_browser.TITLE, browsing_station_service=True)
3151+
self._cnf.station_path = self._cnf.online_browser.BASE_URL
3152+
self._cnf.station_title = self._cnf.online_browser.title
3153+
self.number_of_items = len(self.stations)
3154+
self.selection = 0
3155+
self.startPos = 0
3156+
self.setupAndDrawScreen()
3157+
self.detect_if_player_exited = False
3158+
self._align_stations_and_refresh(self.ws.operation_mode)
3159+
self._set_active_stations()
3160+
3161+
''' all consecutive searches will display the
3162+
the 'no result' message'
3163+
'''
3164+
self._cnf._online_browser.first_search = False
31383165

31393166
def _open_playlist_from_history(self,
31403167
reset=False,
@@ -4892,7 +4919,11 @@ def keypress(self, char):
48924919
char not in self._chars_to_bypass:
48934920

48944921
ret = self._cnf._online_browser.keypress(char)
4895-
if ret == -1:
4922+
if ret == 0:
4923+
self._cnf._online_browser._search_history_index = 2
4924+
self._cnf._online_browser.search()
4925+
4926+
elif ret == -1:
48964927
self.ws.close_window()
48974928
self.refreshBody()
48984929
return

pyradio/window_stack.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class Window_Stack_Constants(object):
104104
THEME_MODE = 400
105105
HISTORY_EMPTY_NOTIFICATION = 500
106106
STATIONS_INTEGRATED_MODE = 501
107+
NO_BROWSER_SEARCH_RESULT_NOTIFICATION = 502
107108
UPDATE_NOTIFICATION_MODE = 1000
108109
UPDATE_NOTIFICATION_OK_MODE = 1001
109110
UPDATE_NOTIFICATION_NOK_MODE = 1002
@@ -208,6 +209,7 @@ class Window_Stack_Constants(object):
208209
BROWSER_SORT_MODE: 'BROWSER_SORT_MODE',
209210
BROWSER_SERVER_SELECTION_MODE: 'BROWSER_SERVER_SELECTION_MODE',
210211
BROWSER_SEARCH_MODE: 'BROWSER_SEARCH_MODE',
212+
NO_BROWSER_SEARCH_RESULT_NOTIFICATION: 'NO_BROWSER_SEARCH_RESULT_NOTIFICATION',
211213
}
212214

213215
''' When PASSIVE_WINDOWS target is one of them,

0 commit comments

Comments
 (0)