TODO: --list argument

This commit is contained in:
Martin Brodbeck 2013-09-16 13:51:20 +02:00
parent 74f1f787f8
commit a2a94b957a
2 changed files with 20 additions and 6 deletions

View file

@ -8,20 +8,22 @@ with "at" or "crontab".
import argparse import argparse
import configparser import configparser
import datetime
import os import os
import sys import sys
import threading import threading
import urllib.request import urllib.request
def check_args(): def check_args():
parser = argparse.ArgumentParser(description='This program records ' parser = argparse.ArgumentParser(description='This program records internet radio streams')
'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=check_duration, parser.add_argument('duration', type=check_duration,
help='Recording time in minutes') help='Recording time in minutes')
parser.add_argument('name', nargs='?', type=str, parser.add_argument('name', nargs='?', type=str,
help='A name for the recording') 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() return parser.parse_args()
def check_duration(value): def check_duration(value):
@ -46,10 +48,14 @@ def read_settings():
config.read(settings_base_dir + 'settings.ini') config.read(settings_base_dir + 'settings.ini')
return dict(config.items()) return dict(config.items())
def record(stoprec, streamurl, target_dir): def record(stoprec, streamurl, target_dir, name=None):
target = open(target_dir + '/test.mp3', "wb")
conn = urllib.request.urlopen(streamurl) 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): while(not stoprec.is_set() and not conn.closed):
target.write(conn.read(1024)) target.write(conn.read(1024))
@ -57,6 +63,9 @@ def main():
args = check_args() args = check_args()
settings = read_settings() settings = read_settings()
streamurl = '' streamurl = ''
if(args.list):
for l in args.list:
print(l)
try: try:
streamurl = settings['STATIONS'][args.station] streamurl = settings['STATIONS'][args.station]
except KeyError: except KeyError:
@ -66,7 +75,7 @@ def main():
stoprec = threading.Event() stoprec = threading.Event()
recthread = threading.Thread(target = record, recthread = threading.Thread(target = record,
args = (stoprec, streamurl, target_dir), daemon = True) args = (stoprec, streamurl, target_dir, args.name), daemon = True)
recthread.start() recthread.start()
recthread.join(args.duration * 60) recthread.join(args.duration * 60)

View file

@ -3,3 +3,8 @@ target_dir = $HOME/Arbeitsfläche
[STATIONS] [STATIONS]
dlf = http://dradio_mp3_dlf_m.akacast.akamaistream.net/7/249/142684/v1/gnl.akacast.akamaistream.net/dradio_mp3_dlf_m 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