More functions and basic threading.

This commit is contained in:
Martin Brodbeck 2013-09-13 12:54:59 +02:00
parent 41394fa027
commit 548145d530

View file

@ -8,15 +8,45 @@ with "at" or "crontab".
import argparse import argparse
import configparser import configparser
import threading
import time
import urllib.request import urllib.request
def main(): def _check_args():
parser = argparse.ArgumentParser(description='This program records internet radio streams.') parser = argparse.ArgumentParser(description='This program records '
'internet radio streams.')
parser.add_argument('station', type=str, help='Name of the radio station ' parser.add_argument('station', type=str, help='Name of the radio station '
'(see config file for a list)') '(see config file for a list)')
parser.add_argument('duration', type=int, help='Recording time in minutes') parser.add_argument('duration', type=_check_duration,
parser.add_argument('name', nargs='?', type=str, help='A name for the recording') help='Recording time in minutes')
args = parser.parse_args() 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__': if __name__ == '__main__':
main() main()