Added list feature

This commit is contained in:
Martin Brodbeck 2013-09-16 15:43:29 +02:00
parent a2a94b957a
commit 6d6312f2d6

View file

@ -14,18 +14,6 @@ import sys
import threading import threading
import urllib.request import urllib.request
def check_args():
parser = argparse.ArgumentParser(description='This program records internet radio streams')
parser.add_argument('station', type=str, help='Name of the radio station '
'(see config file for a list)')
parser.add_argument('duration', type=check_duration,
help='Recording time in minutes')
parser.add_argument('name', nargs='?', type=str,
help='A name for the recording')
parser.add_argument('-l', '--list', action='store_true',
help='Get a list of all known radio stations')
return parser.parse_args()
def check_duration(value): def check_duration(value):
try: try:
value = int(value) value = int(value)
@ -48,7 +36,7 @@ def read_settings():
config.read(settings_base_dir + 'settings.ini') config.read(settings_base_dir + 'settings.ini')
return dict(config.items()) return dict(config.items())
def record(stoprec, streamurl, target_dir, name=None): def record_worker(stoprec, streamurl, target_dir, name=None):
conn = urllib.request.urlopen(streamurl) conn = urllib.request.urlopen(streamurl)
filename = target_dir + os.sep + datetime.datetime.now().isoformat() filename = target_dir + os.sep + datetime.datetime.now().isoformat()
if name: if name:
@ -59,13 +47,9 @@ def record(stoprec, streamurl, target_dir, name=None):
while(not stoprec.is_set() and not conn.closed): while(not stoprec.is_set() and not conn.closed):
target.write(conn.read(1024)) target.write(conn.read(1024))
def main(): def record(args):
args = check_args()
settings = read_settings() settings = read_settings()
streamurl = '' streamurl = ''
if(args.list):
for l in args.list:
print(l)
try: try:
streamurl = settings['STATIONS'][args.station] streamurl = settings['STATIONS'][args.station]
except KeyError: except KeyError:
@ -74,7 +58,8 @@ def main():
target_dir = os.path.expandvars(settings['GLOBAL']['target_dir']) target_dir = os.path.expandvars(settings['GLOBAL']['target_dir'])
stoprec = threading.Event() stoprec = threading.Event()
recthread = threading.Thread(target = record, print('Recording ' + args.station + '')
recthread = threading.Thread(target = record_worker,
args = (stoprec, streamurl, target_dir, args.name), daemon = True) args = (stoprec, streamurl, target_dir, args.name), daemon = True)
recthread.start() recthread.start()
recthread.join(args.duration * 60) recthread.join(args.duration * 60)
@ -82,5 +67,29 @@ def main():
if(recthread.is_alive): if(recthread.is_alive):
stoprec.set() stoprec.set()
def list(args):
settings = read_settings()
print('Known stations:')
for key in settings['STATIONS']:
print(key)
def main():
parser = argparse.ArgumentParser(prog='radiorec', description='This program records internet radio streams')
subparsers = parser.add_subparsers(help='sub-command help')
parser_record = subparsers.add_parser('record', help='Record a station')
parser_record.add_argument('station', type=str, help='Name of the radio station '
'(see config file for a list)')
parser_record.add_argument('duration', type=check_duration,
help='Recording time in minutes')
parser_record.add_argument('name', nargs='?', type=str,
help='A name for the recording')
parser_record.set_defaults(func=record)
parser_list = subparsers.add_parser('list', help='List all known stations')
parser_list.set_defaults(func=list)
#parser_list.add_argument('-l', '--list', action='store_true',
# help='Get a list of all known radio stations')
args = parser.parse_args()
args.func(args)
if __name__ == '__main__': if __name__ == '__main__':
main() main()