This commit is contained in:
Martin Brodbeck 2016-03-10 15:10:28 +01:00
parent 3980c09bf9
commit 2622b083db

View file

@ -28,21 +28,26 @@ import sys
import threading import threading
import urllib.request import urllib.request
def check_duration(value): def check_duration(value):
try: try:
value = int(value) value = int(value)
except ValueError: except ValueError:
raise argparse.ArgumentTypeError('Duration must be a positive integer.') raise argparse.ArgumentTypeError(
'Duration must be a positive integer.')
if value < 1: if value < 1:
raise argparse.ArgumentTypeError('Duration must be a positive integer.') raise argparse.ArgumentTypeError(
'Duration must be a positive integer.')
else: else:
return value return value
def read_settings(): def read_settings():
settings_base_dir = '' settings_base_dir = ''
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
settings_base_dir = os.getenv('HOME') + os.sep + '.config' + os.sep + 'radiorec' settings_base_dir = os.getenv(
'HOME') + os.sep + '.config' + os.sep + 'radiorec'
elif sys.platform == 'win32': elif sys.platform == 'win32':
settings_base_dir = os.getenv('LOCALAPPDATA') + os.sep + 'radiorec' settings_base_dir = os.getenv('LOCALAPPDATA') + os.sep + 'radiorec'
settings_base_dir += os.sep settings_base_dir += os.sep
@ -51,10 +56,12 @@ def read_settings():
config.read_file(open(settings_base_dir + 'settings.ini')) config.read_file(open(settings_base_dir + 'settings.ini'))
except FileNotFoundError as err: except FileNotFoundError as err:
print(str(err)) print(str(err))
print('Please copy/create the settings file to/in the appropriate location.') print('Please copy/create the settings file to/in the appropriate '
'location.')
sys.exit() sys.exit()
return dict(config.items()) return dict(config.items())
def record_worker(stoprec, streamurl, target_dir, args): def record_worker(stoprec, streamurl, target_dir, args):
conn = urllib.request.urlopen(streamurl) conn = urllib.request.urlopen(streamurl)
cur_dt_string = datetime.datetime.now().strftime('%Y-%m-%dT%H_%M_%S') cur_dt_string = datetime.datetime.now().strftime('%Y-%m-%dT%H_%M_%S')
@ -76,12 +83,13 @@ def record_worker(stoprec, streamurl, target_dir, args):
with open(filename, "wb") as target: with open(filename, "wb") as target:
if args.public: if args.public:
verboseprint('Apply public write permissions (Linux only)') verboseprint('Apply public write permissions (Linux only)')
os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP |
stat.S_IROTH | stat.S_IWOTH) stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
verboseprint('Recording ' + args.station + '...') verboseprint('Recording ' + args.station + '...')
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 record(args): def record(args):
settings = read_settings() settings = read_settings()
streamurl = '' streamurl = ''
@ -114,24 +122,32 @@ def record(args):
if(recthread.is_alive): if(recthread.is_alive):
stoprec.set() stoprec.set()
def list(args): def list(args):
settings = read_settings() settings = read_settings()
for key in sorted(settings['STATIONS']): for key in sorted(settings['STATIONS']):
print(key) print(key)
def main(): def main():
parser = argparse.ArgumentParser(description='This program records internet radio streams. ' parser = argparse.ArgumentParser(description='This program records '
'It is free software and comes with ABSOLUTELY NO WARRANTY.') 'internet radio streams. It is free '
'software and comes with ABSOLUTELY NO '
'WARRANTY.')
subparsers = parser.add_subparsers(help='sub-command help') subparsers = parser.add_subparsers(help='sub-command help')
parser_record = subparsers.add_parser('record', help='Record a station') parser_record = subparsers.add_parser('record', help='Record a station')
parser_record.add_argument('station', type=str, help='Name of the radio station ' parser_record.add_argument('station', type=str,
help='Name of the radio station '
'(see `radiorec.py list`)') '(see `radiorec.py list`)')
parser_record.add_argument('duration', type=check_duration, parser_record.add_argument('duration', type=check_duration,
help='Recording time in minutes') help='Recording time in minutes')
parser_record.add_argument('name', nargs='?', type=str, parser_record.add_argument('name', nargs='?', type=str,
help='A name for the recording') help='A name for the recording')
parser_record.add_argument('-p', '--public', action='store_true', help="Public write permissions (Linux only)") parser_record.add_argument(
parser_record.add_argument('-v', '--verbose', action='store_true', help="Verbose output") '-p', '--public', action='store_true',
help="Public write permissions (Linux only)")
parser_record.add_argument(
'-v', '--verbose', action='store_true', help="Verbose output")
parser_record.set_defaults(func=record) parser_record.set_defaults(func=record)
parser_list = subparsers.add_parser('list', help='List all known stations') parser_list = subparsers.add_parser('list', help='List all known stations')
parser_list.set_defaults(func=list) parser_list.set_defaults(func=list)