diff --git a/radiorec.py b/radiorec.py index 97af199..119a650 100755 --- a/radiorec.py +++ b/radiorec.py @@ -8,15 +8,45 @@ with "at" or "crontab". import argparse import configparser +import threading +import time import urllib.request -def main(): - parser = argparse.ArgumentParser(description='This program records internet radio streams.') +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=int, help='Recording time in minutes') - parser.add_argument('name', nargs='?', type=str, help='A name for the recording') - args = parser.parse_args() + '(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') + return parser.parse_args() + +def _check_duration(value): + try: + value = int(value) + except ValueError: + raise argparse.ArgumentTypeError('Duration must be a positive integer.') + + if value < 1: + raise argparse.ArgumentTypeError('Duration must be a positive integer.') + else: + return value + +def record(stoprec): + while(not stoprec.is_set()): + time.sleep(1) + +def main(): + args = _check_args() + + stoprec = threading.Event() + recthread = threading.Thread(target=record, args=(stoprec,)) + recthread.start() + recthread.join(args.duration * 60) + if(recthread.is_alive): + stoprec.set() if __name__ == '__main__': main()