From a2a94b957ad7179a237a041c709285f4d8573b4f Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 16 Sep 2013 13:51:20 +0200 Subject: [PATCH] TODO: --list argument --- radiorec.py | 21 +++++++++++++++------ settings.ini | 5 +++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/radiorec.py b/radiorec.py index e7a5b80..f09eae4 100755 --- a/radiorec.py +++ b/radiorec.py @@ -8,20 +8,22 @@ with "at" or "crontab". import argparse import configparser +import datetime import os import sys import threading import urllib.request 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 ' '(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): @@ -46,10 +48,14 @@ def read_settings(): config.read(settings_base_dir + 'settings.ini') return dict(config.items()) -def record(stoprec, streamurl, target_dir): - target = open(target_dir + '/test.mp3', "wb") +def record(stoprec, streamurl, target_dir, name=None): conn = urllib.request.urlopen(streamurl) - #print(conn.getheader('Content-Type')) + filename = target_dir + os.sep + datetime.datetime.now().isoformat() + if name: + filename += '_' + name + if(conn.getheader('Content-Type') == 'audio/mpeg'): + filename += '.mp3' + target = open(filename, "wb") while(not stoprec.is_set() and not conn.closed): target.write(conn.read(1024)) @@ -57,6 +63,9 @@ def main(): args = check_args() settings = read_settings() streamurl = '' + if(args.list): + for l in args.list: + print(l) try: streamurl = settings['STATIONS'][args.station] except KeyError: @@ -66,7 +75,7 @@ def main(): stoprec = threading.Event() recthread = threading.Thread(target = record, - args = (stoprec, streamurl, target_dir), daemon = True) + args = (stoprec, streamurl, target_dir, args.name), daemon = True) recthread.start() recthread.join(args.duration * 60) diff --git a/settings.ini b/settings.ini index 20ff6ec..aeb56c6 100644 --- a/settings.ini +++ b/settings.ini @@ -3,3 +3,8 @@ target_dir = $HOME/Arbeitsfläche [STATIONS] dlf = http://dradio_mp3_dlf_m.akacast.akamaistream.net/7/249/142684/v1/gnl.akacast.akamaistream.net/dradio_mp3_dlf_m +dkultur = http://dradio_mp3_dkultur_m.akacast.akamaistream.net/7/530/142684/v1/gnl.akacast.akamaistream.net/dradio_mp3_dkultur_m +dwissen = http://dradio_mp3_dwissen_m.akacast.akamaistream.net/7/728/142684/v1/gnl.akacast.akamaistream.net/dradio_mp3_dwissen_m +swr2 = http://mp3-live.swr.de/swr2_m.m3u +brklassik = http://streams.br-online.de/br-klassik_2.m3u +mdrklassik = http://avw.mdr.de/livestreams/mdr_klassik_live_128.m3u