From ce9f83dc96932d79da0d1a5d55e96488d236449c Mon Sep 17 00:00:00 2001 From: georg Date: Fri, 6 Nov 2020 20:23:08 +0100 Subject: [PATCH 1/7] fixing m3u Problem --- radiorec.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/radiorec.py b/radiorec.py index 73543cf..bc1b819 100755 --- a/radiorec.py +++ b/radiorec.py @@ -123,11 +123,9 @@ def record(args): if streamurl.endswith('.m3u'): verboseprint('Seems to be an M3U playlist. Trying to parse...') pool = urllib3.PoolManager() - with pool.request('GET',streamurl) as remotefile: - for line in remotefile: - if not line.decode('utf-8').startswith('#') and len(line) > 1: - tmpstr = line.decode('utf-8') - break + remotefile = pool.request('GET', streamurl) + if not remotefile.data.decode('utf-8').startswith('#') and len(remotefile.data) > 1: + tmpstr = remotefile.data.decode('utf-8') streamurl = tmpstr verboseprint(print_time() + " ... Stream URL: " + streamurl) From a8ad2c03e795863c4ee906a48359672fab5def07 Mon Sep 17 00:00:00 2001 From: georg Date: Mon, 21 Dec 2020 18:33:49 +0100 Subject: [PATCH 2/7] NDR in settings --- settings.ini | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/settings.ini b/settings.ini index 953ad46..2365b35 100644 --- a/settings.ini +++ b/settings.ini @@ -1,5 +1,5 @@ [GLOBAL] -target_dir = $HOME/Arbeitsfläche +target_dir = $HOME/tmp [STATIONS] brklassik = http://streams.br-online.de/br-klassik_2.m3u @@ -11,3 +11,4 @@ mdrklassik = http://avw.mdr.de/livestreams/mdr_klassik_live_128.m3u radioeins = http://www.radioeins.de/live.m3u swr2 = http://mp3-live.swr.de/swr2_m.m3u wdr3 = http://www.wdr.de/wdrlive/media/mp3/wdr3_hq.m3u +ndrkultur = http://www.ndr.de/resources/metadaten/audio/m3u/ndrkultur.m3u From b4988e7299cda507a4421131059848418dd063eb Mon Sep 17 00:00:00 2001 From: georg Date: Mon, 21 Dec 2020 22:34:20 +0100 Subject: [PATCH 3/7] No mor open with, plain fileobject --- radiorec.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/radiorec.py b/radiorec.py index bc1b819..05b4dfd 100755 --- a/radiorec.py +++ b/radiorec.py @@ -124,8 +124,10 @@ def record(args): verboseprint('Seems to be an M3U playlist. Trying to parse...') pool = urllib3.PoolManager() remotefile = pool.request('GET', streamurl) - if not remotefile.data.decode('utf-8').startswith('#') and len(remotefile.data) > 1: - tmpstr = remotefile.data.decode('utf-8') + for line in remotefile.data.decode().split(): + if not line.startswith('#') and len(line) > 1: + tmpstr = line + break streamurl = tmpstr verboseprint(print_time() + " ... Stream URL: " + streamurl) From 3498a8d00bae18d00e84b5a06a02ec56cbc5a92d Mon Sep 17 00:00:00 2001 From: georg Date: Mon, 21 Dec 2020 23:05:50 +0100 Subject: [PATCH 4/7] =?UTF-8?q?Settings=20revert=20to=20Arbeitsfl=C3=A4che?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- settings.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.ini b/settings.ini index 2365b35..5026290 100644 --- a/settings.ini +++ b/settings.ini @@ -1,5 +1,5 @@ [GLOBAL] -target_dir = $HOME/tmp +target_dir = $HOME/Arbeitsfläche [STATIONS] brklassik = http://streams.br-online.de/br-klassik_2.m3u From 9793b69715eba5befccc48d17dce0ceeec1a8028 Mon Sep 17 00:00:00 2001 From: georg Date: Tue, 22 Dec 2020 16:28:18 +0100 Subject: [PATCH 5/7] Added some checks for missing Targetfolder too many http requests and wrong urls. Also changed the url of wdr3. --- radiorec.py | 29 +++++++++++++++++++++++------ settings.ini | 5 +++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/radiorec.py b/radiorec.py index 05b4dfd..4543b53 100755 --- a/radiorec.py +++ b/radiorec.py @@ -106,12 +106,14 @@ def record_worker(stoprec, streamurl, target_dir, args): while(not stoprec.is_set() and not conn.closed): target.write(conn.read(1024)) + verboseprint(print_time() + " ... Connection closed = " + str(conn.closed)) conn.release_conn() def record(args): settings = read_settings(args) streamurl = '' + tmpstr = '' global verboseprint verboseprint = print if args.verbose else lambda *a, **k: None @@ -123,15 +125,30 @@ def record(args): if streamurl.endswith('.m3u'): verboseprint('Seems to be an M3U playlist. Trying to parse...') pool = urllib3.PoolManager() - remotefile = pool.request('GET', streamurl) - for line in remotefile.data.decode().split(): - if not line.startswith('#') and len(line) > 1: - tmpstr = line - break - streamurl = tmpstr + try: + remotefile = pool.request('GET', streamurl) + except MaxRetryError: + print('The URL of the station is not found! Check' + args.station + ' in the Settings!') + sys.exit() + if remotefile.status != 200: + print('The URL of the station is somehow faulty! Check' + args.station + ' in the Settings!') + sys.exit() + else: + for line in remotefile.data.decode().split(): + if not line.startswith('#') and len(line) > 1 and line.endswith('mp3'): + tmpstr = line + break + if not len(tmpstr) > 1: + print('Could not find a mp3 stream') + sys.exit() + else: + streamurl = tmpstr verboseprint(print_time() + " ... Stream URL: " + streamurl) target_dir = os.path.expandvars(settings['GLOBAL']['target_dir']) + if not os.path.isdir(target_dir + os.sep): + print('Target directory not found! Check that ' + target_dir + ' is a valid folder!') + sys.exit() started_at = time.time() should_end_at = started_at + (args.duration * 60) remaining = (args.duration * 60) diff --git a/settings.ini b/settings.ini index 5026290..10d0bdd 100644 --- a/settings.ini +++ b/settings.ini @@ -1,5 +1,6 @@ [GLOBAL] -target_dir = $HOME/Arbeitsfläche +#target_dir = $HOME/Arbeitsfläche +target_dir = $HOME/tmp [STATIONS] brklassik = http://streams.br-online.de/br-klassik_2.m3u @@ -10,5 +11,5 @@ erfplus = http://c14000-l.i.core.cdn.streamfarm.net/14000cina/live/3212erf_96/li mdrklassik = http://avw.mdr.de/livestreams/mdr_klassik_live_128.m3u radioeins = http://www.radioeins.de/live.m3u swr2 = http://mp3-live.swr.de/swr2_m.m3u -wdr3 = http://www.wdr.de/wdrlive/media/mp3/wdr3_hq.m3u +wdr3 = http://wdr-wdr3-live.icecast.wdr.de/wdr/wdr3/live/mp3/256/stream.mp3 ndrkultur = http://www.ndr.de/resources/metadaten/audio/m3u/ndrkultur.m3u From 5ba2de2f4005fafb77ebc695e5aee873f53f59f2 Mon Sep 17 00:00:00 2001 From: georg Date: Tue, 22 Dec 2020 16:31:35 +0100 Subject: [PATCH 6/7] reverted temporary Settings... --- settings.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/settings.ini b/settings.ini index 10d0bdd..6868a52 100644 --- a/settings.ini +++ b/settings.ini @@ -1,6 +1,6 @@ [GLOBAL] -#target_dir = $HOME/Arbeitsfläche -target_dir = $HOME/tmp +target_dir = $HOME/Arbeitsfläche + [STATIONS] brklassik = http://streams.br-online.de/br-klassik_2.m3u From aba237db2ed91b1dc591827526e091cd43873ca6 Mon Sep 17 00:00:00 2001 From: georg Date: Fri, 25 Dec 2020 18:27:36 +0100 Subject: [PATCH 7/7] Changed the sys.exit, if not tmpstr, the errorhandling, the creation of the Target Directory, and some PEP8 Styling things too.. --- radiorec.py | 53 +++++++++++++++++++++++++++++++++------------------- settings.ini | 2 +- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/radiorec.py b/radiorec.py index 4543b53..ec48acf 100755 --- a/radiorec.py +++ b/radiorec.py @@ -29,11 +29,14 @@ import urllib3 import logging import time +from urllib3.exceptions import MaxRetryError logging.basicConfig(level=logging.DEBUG) + def print_time(): return time.strftime("%Y-%m-%d %H:%M:%S") + def check_duration(value): try: value = int(value) @@ -58,7 +61,8 @@ def read_settings(args): elif sys.platform == 'win32': settings_base_dir = os.getenv('LOCALAPPDATA') + os.sep + 'radiorec' elif sys.platform == 'darwin': - settings_base_dir = os.getenv('HOME') + os.sep + 'Library' + os.sep + 'Application Support' + os.sep + 'radiorec' + settings_base_dir = os.getenv('HOME') + os.sep + 'Library' + os.sep + \ + 'Application Support' + os.sep + 'radiorec' settings_base_dir += os.sep config = configparser.ConfigParser() try: @@ -72,7 +76,7 @@ def read_settings(args): def record_worker(stoprec, streamurl, target_dir, args): pool = urllib3.PoolManager() - conn = pool.request('GET',streamurl, preload_content=False) + conn = pool.request('GET', streamurl, preload_content=False) conn.auto_close = False if conn.status != 200: conn.release_conn() @@ -85,7 +89,7 @@ def record_worker(stoprec, streamurl, target_dir, args): if args.name: filename += '_' + args.name content_type = conn.getheader('Content-Type') - if(content_type == 'audio/mpeg'): + if (content_type == 'audio/mpeg'): filename += '.mp3' elif(content_type == 'application/aacp' or content_type == 'audio/aacp'): filename += '.aac' @@ -106,10 +110,10 @@ def record_worker(stoprec, streamurl, target_dir, args): while(not stoprec.is_set() and not conn.closed): target.write(conn.read(1024)) - verboseprint(print_time() + " ... Connection closed = " + str(conn.closed)) conn.release_conn() + def record(args): settings = read_settings(args) streamurl = '' @@ -128,27 +132,33 @@ def record(args): try: remotefile = pool.request('GET', streamurl) except MaxRetryError: - print('The URL of the station is not found! Check' + args.station + ' in the Settings!') + logging.getLogger(__name__).error('The URL of the station is somehow faulty! Check' + + args.station + ' in the Settings!') sys.exit() if remotefile.status != 200: - print('The URL of the station is somehow faulty! Check' + args.station + ' in the Settings!') - sys.exit() + logging.getLogger(__name__).error( + 'The URL of the station is somehow faulty! Check' + args.station + ' in the Settings!') + sys.exit(1) else: for line in remotefile.data.decode().split(): if not line.startswith('#') and len(line) > 1 and line.endswith('mp3'): tmpstr = line break - if not len(tmpstr) > 1: - print('Could not find a mp3 stream') - sys.exit() + if not tmpstr: + logging.getLogger(__name__).error('Could not find a mp3 stream') + sys.exit(1) else: streamurl = tmpstr verboseprint(print_time() + " ... Stream URL: " + streamurl) target_dir = os.path.expandvars(settings['GLOBAL']['target_dir']) - if not os.path.isdir(target_dir + os.sep): - print('Target directory not found! Check that ' + target_dir + ' is a valid folder!') - sys.exit() + if not os.path.isdir(target_dir): + try: + os.mkdir(target_dir) + except FileNotFoundError: + logging.getLogger(__name__).error('Target directory not found! Check that ' + + target_dir + ' is a valid folder!') + sys.exit(1) started_at = time.time() should_end_at = started_at + (args.duration * 60) remaining = (args.duration * 60) @@ -159,18 +169,20 @@ def record(args): recthread = threading.Thread(target=record_worker, args=(stoprec, streamurl, target_dir, args)) recthread.setDaemon(True) recthread.start() - verboseprint(print_time() + " ... Started thread " + str(recthread) + " timeout: " + str(remaining / 60) + " min") + verboseprint(print_time() + " ... Started thread " + str(recthread) + " timeout: " + + str(remaining / 60) + " min") recthread.join(remaining) - verboseprint(print_time() + " ... Came out of rec thread again") + verboseprint(print_time() + " ... Came out of rec thread again") - if(recthread.is_alive): + if recthread.is_alive: stoprec.set() verboseprint(print_time() + " ... Called stoprec.set()") else: verboseprint(print_time() + " ... recthread.is_alive = False") remaining = should_end_at - time.time() - verboseprint(print_time() + " ... Remaining: " + str(remaining / 60) + ", Threads: " + str(threading.activeCount())) + verboseprint(print_time() + " ... Remaining: " + str(remaining / 60) + + ", Threads: " + str(threading.activeCount())) def list(args): @@ -180,7 +192,8 @@ def list(args): def main(): - parser = argparse.ArgumentParser(description='This program records internet radio streams. It is free software and comes with ABSOLUTELY NO WARRANTY.') + parser = argparse.ArgumentParser(description='This program records internet radio streams. ' + 'It is free software and comes with ABSOLUTELY NO WARRANTY.') subparsers = parser.add_subparsers(help='sub-command help') parser_record = subparsers.add_parser('record', help='Record a station') parser_record.add_argument('station', type=str, @@ -201,7 +214,8 @@ def main(): parser_record.set_defaults(func=record) parser_list = subparsers.add_parser('list', help='List all known stations') parser_list.set_defaults(func=list) - parser_list.add_argument('-s', '--settings', nargs='?', type=str, help="specify alternative location for settings.ini") + parser_list.add_argument('-s', '--settings', nargs='?', type=str, + help="specify alternative location for settings.ini") if not len(sys.argv) > 1: print('Error: No argument specified.\n') @@ -210,5 +224,6 @@ def main(): args = parser.parse_args() args.func(args) + if __name__ == '__main__': main() diff --git a/settings.ini b/settings.ini index 6868a52..caed715 100644 --- a/settings.ini +++ b/settings.ini @@ -8,7 +8,7 @@ dkultur = http://www.deutschlandradio.de/streaming/dkultur.m3u dlf = http://www.deutschlandradio.de/streaming/dlf.m3u dwissen = http://dradio_mp3_dwissen_m.akacast.akamaistream.net/7/728/142684/v1/gnl.akacast.akamaistream.net/dradio_mp3_dwissen_m erfplus = http://c14000-l.i.core.cdn.streamfarm.net/14000cina/live/3212erf_96/live_de_96.mp3 -mdrklassik = http://avw.mdr.de/livestreams/mdr_klassik_live_128.m3u +mdrklassik = http://avw.mdr.de/streams/284350-0_mp3_high.m3u radioeins = http://www.radioeins.de/live.m3u swr2 = http://mp3-live.swr.de/swr2_m.m3u wdr3 = http://wdr-wdr3-live.icecast.wdr.de/wdr/wdr3/live/mp3/256/stream.mp3