TODO: --list argument
This commit is contained in:
parent
74f1f787f8
commit
a2a94b957a
2 changed files with 20 additions and 6 deletions
21
radiorec.py
21
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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue