Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 266 lines (234 sloc) 12.1 KB
#! /usr/bin/env python
#
# scs - shoutcast search; search among station on shoutcast.com
# Use as application to e.g. start random stream on command line.
#
# Copyright (c) 2009-2010 by the Authors.
# Please report bugs or feature requests by e-mail. Also, I'd be happy
# to hear from you if you enjoy this software.
#
# 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 2 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 <http://www.gnu.org/licenses/>.
#
import re
import sys
import urllib.error
import random
import optparse
import shoutcast_search.shoutcast_search as scs
description = 'Search shoutcast.com for radio stations (not for commercial use, see http://forums.winamp.com/showthread.php?threadid=295638). Use criteria to search for station names, genres or current songs, and filters to specify details. Keywords are used to search freely among station names, genres and current songs. With no criteria or keywords, %prog returns the current Top500 list. Stations can be sorted in different ways using sorters. Example: \'%prog -n 10 -g Rock -p "Depeche Mode" -b "=128"\' shows the top ten Rock 128kbps stations currently playing Depeche Mode.'
def _station_text(station_info, format):
url = scs.url_by_id(station_info['id'])
replacements = {'%g': station_info['genre'],
'%p': station_info['ct'],
'%s': station_info['name'],
'%b': station_info['br'],
'%l': station_info['lc'],
'%t': station_info['mt'],
'%u': url,
'%%': '%',
'\\n': '\n',
'\\t': '\t'}
resstr = format
for key, value in replacements.items():
resstr = resstr.replace(key, str(value))
return resstr
def _fail_exit(code, msg):
sys.stderr.write("{0}: {1}\n".format(sys.argv[0], msg))
sys.exit(code)
def _expression_param(value):
if not value:
return lambda x:True
if not re.compile('^[=><]?\d+$').match(value):
o.error('invalid expression: {0}'.format(value))
if value[0] in ('><'):
return lambda x: eval('{0}{1}'.format(x, value))
else:
return lambda x: eval('{0}=={1}'.format(x, value.strip('=')))
def _int_param(value):
try:
return int(value)
except:
o.error('invalid integer: {0}'.format(value))
def _generate_list_sorters(pattern = 'l'):
'''
We want to manipulate the list by pruning and sorting. Pattern contains a string that defines how
The pattern is:
[^]([bnr]|l\d+):
^ set ascending order for the next sorter. Sort order is reset to descending for each new sorter.
b sorts by bitrate.
l sorts by number of listeners.
r randomizes list.
n truncates the list with the number of stations given, e.g. n10 for ten stations.
Examples
^b: sort by bitrate ascending
ln10r: sort by bitrate descending, truncate the list to ten stations, randomize order. This is appropriate if you want a random popular station, but have a hard time predicting the number of listeners.
Behaviour with command line parameters '-n 10' is 'ln10' and with '-r -n 1' it is 'rn1'.
Default behaviour is 'l'
'''
def _create_sorter(field, descending):
return lambda list: sorted(list, key=lambda a: int(a[field]), reverse = descending)
def _filter_description(fieldname, descending):
descending_text = 'desc'
if not descending:
descending_text = 'asc'
return '{0} {1}'.format(fieldname, descending_text)
def _random(list):
random.shuffle(list)
return list
sorters = []
sorters_description = []
sort_descending = True
# I'd rather enumerate or something, but 'n' needs some special attention
# Not very pretty, though
index = 0
while index < len(pattern):
char = pattern[index]
if char == '^':
sort_descending = False
elif char == 'b':
sorters.append(_create_sorter('br', sort_descending))
sorters_description.append(_filter_description('bitrate', sort_descending))
elif char == 'l':
sorters.append(_create_sorter('lc', sort_descending))
sorters_description.append(_filter_description('listeners', sort_descending))
elif char == 'r':
sorters.append(_random)
sorters_description.append('random order')
elif char == 'n':
number = ''
while True:
index += 1
if index >= len(pattern) or pattern[index] not in '0123456789':
break
number = number + pattern[index]
index -= 1
if not number:
o.error('missing number for sorter n in "{0}"'.format(pattern))
value = int(number)
sorters.append(lambda list: list[:value])
sorters_description.append('top {0}'.format(value))
else:
o.error('invalid sorter: {0}'.format(char))
if char != '^':
sort_descending = True # Reset sort order
index += 1
return (sorters, sorters_description)
if __name__ == '__main__':
o = optparse.OptionParser(usage = "%prog [options] [keywords] - shoutcast search",
version="%prog 0.4.1",
description = description)
o.add_option('', '--list-genres', dest='do_list_genres', action='store_true',
default=False, help='list available genres and exit')
o.add_option('-n', '--limit', dest='limit', action='store',
default=0, help='maximum number of stations.')
o.add_option('-r', '--random', dest='random', action='store_true',
default=False, help='sort stations randomly unless --sort is given.')
o.add_option('-v', '--verbose', dest='verbose', action='store_true',
default=False, help='verbose output, useful for getting search right.')
fmt = optparse.OptionGroup(o, 'Format', 'Specifies how the found stations should be printed. Codes: %u - url, %g - genre, %p - current song, %s - station name, %b - bitrate, %l - number of listeners, %t - MIME / codec, %% - %, \n - newline, \t - tab')
fmt.add_option('-f', '--format', dest='format', action='store',
default='', help='results formatting.')
o.add_option_group(fmt)
p = optparse.OptionGroup(o, 'Criteria')
p.add_option('-g', '--genre', dest='genre', action='append',
default=[], help='genre, e.g. \'-g Ambient\'.')
p.add_option('-p', '--playing', dest='song', action='append',
default=[], help='currently played song / artist, e.g. \'-p Shantel\'.')
p.add_option('-s', '--station', dest='station', action='append',
default=[], help='station name, e.g. \'-s "Groove Salad"\'.')
o.add_option_group(p)
f = optparse.OptionGroup(o, 'Filters', 'Filter the search results. These can NOT be used alone, e.g. to search for all stations with no listeners. If no criteria or keywords are given, the Top500 stations are used.')
f.add_option('-b', '--bitrate', dest='bitrate', action='store',
default='', help='bitrate (kbps), [=><]NNN, e.g. \'-b "=128"\' for 128kbps.')
f.add_option('-l', '--listeners', dest='listeners', action='store',
default='', help='number of listeners, [=><]NNN, e.g. \'-l ">500"\' for more than 500 listeners.')
f.add_option('-t', '--type', dest='codec', action='store',
default='', help='"mpeg" for MP3, "aacp" for aacPlus.')
o.add_option_group(f)
s = optparse.OptionGroup(o, 'Sorters', 'Manipulate the order of the returned list. The list can be sorted by number of listeners (l) and bitrate (b), it can be randomized (r) and it can be truncated (n), i.e. shortened to a specified amount of stations. Sorting is performed in written order, for example "ln20r" sorts the list by number of listeners, trunkates it to twenty stations and then randomizes it, giving the top twenty random stations matching the search. ^ is used to set sort order to ascending for l and b. The default sort order is reset to descending for each new sorter. Specifying sorters void the "-r" option.')
s.add_option('', '--sort', dest='sort_rules', action='store',
default='', help='rules for manipulating the order of the list. "l" for number of listeners, "b" for bitrate, "r" to randomize order, "n<integer>" to truncate list.')
o.add_option_group(s)
(options, args) = o.parse_args()
try:
if options.do_list_genres:
genres = scs.get_genres()
print('\n'.join(genres))
if genres:
sys.exit(0)
else:
sys.exit(4)
p_keywords = args
p_verbose = options.verbose
p_random = options.random
p_genre = options.genre
p_station = options.station
p_song = options.song
p_sort_rules = options.sort_rules
p_limit = _int_param(options.limit)
p_bitrate = _expression_param(options.bitrate)
p_listeners = _expression_param(options.listeners)
p_format = '%u'
if p_verbose:
p_format = '%s [%bkbps %t]\\n\\t%u\\n\\t%g, %l listeners\\n\\tNow playing: %p\\n'
if options.format:
p_format = options.format
p_mime_type = ''
if options.codec:
if options.codec.strip('"') not in ('mpeg', 'aacp'):
o.error('CODEC must be "mpeg", "aacp" or none')
p_mime_type = 'audio/' + options.codec.strip('"')
sorters, sorters_description = _generate_list_sorters(p_sort_rules)
if sorters:
p_random = False #Start with sorted list when using sorters
if p_verbose:
# Print information about query to help debug
print('Search summary')
print('-' * 30)
print(' Keywords: {0}'.format(', '.join(p_keywords)))
print(' Genres: {0}'.format(', '.join(p_genre)))
print(' Playing: {0}'.format(', '.join(p_song)))
print(' Stations: {0}'.format(', '.join(p_station)))
bitrate_str = ''
if options.bitrate:
bitrate_str = options.bitrate
print(' Bitrate: {0}'.format(bitrate_str))
listeners_str = ''
if options.listeners:
listeners_str = options.listeners
print('Listeners: {0}'.format(listeners_str))
print(' Type: {0}'.format(options.codec))
order_str = 'by no listeners'
if p_random:
order_str = 'random'
if p_sort_rules:
order_str = 'by sorters'
print(' Order: {0}'.format(order_str))
print(' Sorter: {0}'.format(' | '.join(sorters_description)))
limit_str = ''
if p_limit > 0:
limit_str = str(p_limit)
print(' Limit: {0}'.format(limit_str))
print(' Format: {0}'.format(p_format))
print('')
results = scs.search(p_keywords, p_station, p_genre, p_song, p_bitrate, p_listeners, p_mime_type, p_limit, p_random, sorters)
print('\n'.join(_station_text(el, p_format) for el in results))
if p_verbose:
print('\n{0:d} station(s) found.'.format(len(results)))
if not results:
_fail_exit(4, 'no station found\n')
except urllib.error.URLError as e:
_fail_exit(1, 'network error: {0}'.format(e))
except Exception as e:
_fail_exit(3, 'unknown error: {0}'.format(e))