From 435ffb827ad6b1a540aebcbfa95b4054a34c274b Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 17 Sep 2013 10:09:38 +0200 Subject: [PATCH] Added support for M3U playlists\nAdded verbose output --- radiorec.py | 25 ++++++++++++++++++------- settings.ini | 1 + 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/radiorec.py b/radiorec.py index d9cb3e5..4aae276 100755 --- a/radiorec.py +++ b/radiorec.py @@ -47,28 +47,39 @@ def record_worker(stoprec, streamurl, target_dir, name=None): elif(content_type == 'application/ogg' or content_type == 'audio/ogg'): filename += '.ogg' elif(content_type == 'audio/x-mpegurl'): - #TODO Add support for M3U playlists print('Sorry, M3U playlists are currently not supported') sys.exit() else: print('Unknown content type "' + content_type + '". Assuming mp3.') filename += 'mp3' - target = open(filename, "wb") - while(not stoprec.is_set() and not conn.closed): - target.write(conn.read(1024)) + with open(filename, "wb") as target: + while(not stoprec.is_set() and not conn.closed): + target.write(conn.read(1024)) def record(args): settings = read_settings() streamurl = '' + global verboseprint + verboseprint = print if args.verbose else lambda *a, **k: None + try: streamurl = settings['STATIONS'][args.station] except KeyError: print('Unkown station name: ' + args.station) - return + sys.exit() + if streamurl.endswith('.m3u'): + verboseprint('Seems to be an M3U playlist. Trying to parse…') + with urllib.request.urlopen(streamurl) as remotefile: + for line in remotefile: + if not line.decode('utf-8').startswith('#'): + tmpstr = line.decode('utf-8') + break + streamurl = tmpstr + verboseprint('stream url: ' + streamurl) target_dir = os.path.expandvars(settings['GLOBAL']['target_dir']) stoprec = threading.Event() - print('Recording ' + args.station + '…') + verboseprint('Recording ' + args.station + '…') recthread = threading.Thread(target = record_worker, args = (stoprec, streamurl, target_dir, args.name), daemon = True) recthread.start() @@ -79,7 +90,6 @@ def record(args): def list(args): settings = read_settings() - print('Known stations:') for key in settings['STATIONS']: print(key) @@ -93,6 +103,7 @@ def main(): help='Recording time in minutes') parser_record.add_argument('name', nargs='?', type=str, help='A name for the recording') + parser_record.add_argument('-v', '--verbose', action='store_true', help="Verbose output") parser_record.set_defaults(func=record) parser_list = subparsers.add_parser('list', help='List all known stations') parser_list.set_defaults(func=list) diff --git a/settings.ini b/settings.ini index abb6969..92df468 100644 --- a/settings.ini +++ b/settings.ini @@ -6,5 +6,6 @@ dlf = http://dradio_mp3_dlf_m.akacast.akamaistream.net/7/249/142684/v1/gnl.akaca 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://swr-mp3-m-swr2.akacast.akamaistream.net/7/721/137135/v1/gnl.akacast.akamaistream.net/swr-mp3-m-swr2 +swr2_m3u = http://mp3-live.swr.de/swr2_m.m3u brklassik = http://gffstream.ic.llnwd.net/stream/gffstream_w13a mdrklassik = http://c22033-l.i.core.cdn.streamfarm.net/22008mdrklassik/live/3087mdrklassik/live_de_128.mp3[brodbemn@pcvs90