2013-09-11 10:46:47 +02:00
|
|
|
#!/usr/bin/env python3
|
2013-09-11 16:10:16 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
2013-09-11 10:46:47 +02:00
|
|
|
|
2013-09-11 11:06:13 +02:00
|
|
|
"""
|
|
|
|
This script records internet radio streams. It can be used in conjunction
|
|
|
|
with "at" or "crontab".
|
|
|
|
"""
|
|
|
|
|
2013-09-11 10:46:47 +02:00
|
|
|
import argparse
|
|
|
|
import configparser
|
2013-09-13 12:54:59 +02:00
|
|
|
import threading
|
2013-09-11 10:46:47 +02:00
|
|
|
import urllib.request
|
|
|
|
|
2013-09-13 12:54:59 +02:00
|
|
|
def _check_args():
|
|
|
|
parser = argparse.ArgumentParser(description='This program records '
|
|
|
|
'internet radio streams.')
|
2013-09-12 15:23:00 +02:00
|
|
|
parser.add_argument('station', type=str, help='Name of the radio station '
|
2013-09-13 12:54:59 +02:00
|
|
|
'(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')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
def _check_duration(value):
|
|
|
|
try:
|
|
|
|
value = int(value)
|
|
|
|
except ValueError:
|
|
|
|
raise argparse.ArgumentTypeError('Duration must be a positive integer.')
|
|
|
|
|
|
|
|
if value < 1:
|
|
|
|
raise argparse.ArgumentTypeError('Duration must be a positive integer.')
|
|
|
|
else:
|
|
|
|
return value
|
|
|
|
|
|
|
|
def record(stoprec):
|
2013-09-13 13:51:01 +02:00
|
|
|
target = open('./test.mp3', "wb")
|
|
|
|
conn = urllib.request.urlopen('http://dradio_mp3_dlf_m.akacast.akamaistream.net/7/249/142684/v1/gnl.akacast.akamaistream.net/dradio_mp3_dlf_m')
|
|
|
|
#print(conn.getheader('Content-Type'))
|
|
|
|
while(not stoprec.is_set() and not conn.closed):
|
|
|
|
target.write(conn.read(1024))
|
2013-09-13 12:54:59 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
args = _check_args()
|
|
|
|
|
|
|
|
stoprec = threading.Event()
|
2013-09-13 13:51:01 +02:00
|
|
|
recthread = threading.Thread(target = record, args = (stoprec,), daemon = True)
|
2013-09-13 12:54:59 +02:00
|
|
|
recthread.start()
|
|
|
|
recthread.join(args.duration * 60)
|
|
|
|
if(recthread.is_alive):
|
|
|
|
stoprec.set()
|
2013-09-11 16:10:16 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|