radiorec/radiorec.py

230 lines
8.6 KiB
Python
Raw Normal View History

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
"""
2013-09-18 15:19:04 +02:00
radiorec.py Recording internet radio streams
Copyright (C) 2013 Martin Brodbeck <martin@brodbeck-online.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-09-11 11:06:13 +02:00
"""
2013-09-11 10:46:47 +02:00
import argparse
import configparser
2013-09-16 13:51:20 +02:00
import datetime
2013-09-13 21:34:41 +02:00
import os
import stat
2013-09-13 21:34:41 +02:00
import sys
2013-09-13 12:54:59 +02:00
import threading
2020-08-24 20:06:05 +02:00
import urllib3
import logging
import time
from urllib3.exceptions import MaxRetryError
2020-08-24 20:06:05 +02:00
logging.basicConfig(level=logging.DEBUG)
2013-09-11 10:46:47 +02:00
2020-08-24 20:06:05 +02:00
def print_time():
return time.strftime("%Y-%m-%d %H:%M:%S")
2016-03-10 15:10:28 +01:00
2013-09-13 21:34:41 +02:00
def check_duration(value):
2013-09-13 12:54:59 +02:00
try:
value = int(value)
except ValueError:
2016-03-10 15:10:28 +01:00
raise argparse.ArgumentTypeError(
2020-08-24 20:06:05 +02:00
'Duration in minutes must be a positive integer.')
2013-09-13 12:54:59 +02:00
if value < 1:
2016-03-10 15:10:28 +01:00
raise argparse.ArgumentTypeError(
2020-08-24 20:06:05 +02:00
'Duration in minutes must be a positive integer.')
2013-09-13 12:54:59 +02:00
else:
return value
2016-03-10 15:10:28 +01:00
2019-03-18 21:34:29 +01:00
def read_settings(args):
2013-09-13 21:34:41 +02:00
settings_base_dir = ''
2019-03-18 21:34:29 +01:00
if args.settings:
settings_base_dir = args.settings
elif sys.platform.startswith('linux'):
2016-03-10 15:10:28 +01:00
settings_base_dir = os.getenv(
'HOME') + os.sep + '.config' + os.sep + 'radiorec'
2013-09-13 21:34:41 +02:00
elif sys.platform == 'win32':
settings_base_dir = os.getenv('LOCALAPPDATA') + os.sep + 'radiorec'
2016-10-05 12:21:16 +02:00
elif sys.platform == 'darwin':
settings_base_dir = os.getenv('HOME') + os.sep + 'Library' + os.sep + \
'Application Support' + os.sep + 'radiorec'
2013-09-13 21:34:41 +02:00
settings_base_dir += os.sep
config = configparser.ConfigParser()
2013-09-18 15:20:47 +02:00
try:
config.read_file(open(settings_base_dir + 'settings.ini'))
except FileNotFoundError as err:
print(str(err))
2020-08-24 20:06:05 +02:00
print('Please copy/create the settings file to/in the appropriate location.')
2013-09-18 15:20:47 +02:00
sys.exit()
2013-09-13 21:34:41 +02:00
return dict(config.items())
2016-03-10 15:10:28 +01:00
def record_worker(stoprec, streamurl, target_dir, args):
2020-08-24 20:06:05 +02:00
pool = urllib3.PoolManager()
conn = pool.request('GET', streamurl, preload_content=False)
2020-08-24 20:06:05 +02:00
conn.auto_close = False
if conn.status != 200:
conn.release_conn()
time.sleep(10)
verboseprint(print_time() + " ... Waited to return for retry bcof status " + str(conn.status))
return
cur_dt_string = datetime.datetime.now().strftime('%Y-%m-%dT%H_%M_%S')
filename = target_dir + os.sep + cur_dt_string + "_" + args.station
if args.name:
filename += '_' + args.name
2013-09-16 15:49:35 +02:00
content_type = conn.getheader('Content-Type')
if (content_type == 'audio/mpeg'):
2013-09-16 13:51:20 +02:00
filename += '.mp3'
elif(content_type == 'application/aacp' or content_type == 'audio/aacp'):
filename += '.aac'
2013-09-16 15:49:35 +02:00
elif(content_type == 'application/ogg' or content_type == 'audio/ogg'):
filename += '.ogg'
2013-09-16 16:28:47 +02:00
elif(content_type == 'audio/x-mpegurl'):
print('Sorry, M3U playlists are currently not supported')
sys.exit()
2013-09-16 15:49:35 +02:00
else:
2013-09-16 16:28:47 +02:00
print('Unknown content type "' + content_type + '". Assuming mp3.')
2015-01-22 09:07:55 +01:00
filename += '.mp3'
2020-08-24 20:06:05 +02:00
verboseprint(print_time() + " ... Writing to: " + filename + ", Content-Type: " + conn.getheader('Content-Type'))
with open(filename, 'wb') as target:
if args.public:
verboseprint('Apply public write permissions (Linux only)')
2020-08-24 20:06:05 +02:00
os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
while(not stoprec.is_set() and not conn.closed):
target.write(conn.read(1024))
2013-09-13 12:54:59 +02:00
2020-08-24 20:06:05 +02:00
verboseprint(print_time() + " ... Connection closed = " + str(conn.closed))
conn.release_conn()
2016-03-10 15:10:28 +01:00
2013-09-16 15:43:29 +02:00
def record(args):
2019-03-18 21:34:29 +01:00
settings = read_settings(args)
2013-09-13 21:34:41 +02:00
streamurl = ''
tmpstr = ''
global verboseprint
verboseprint = print if args.verbose else lambda *a, **k: None
2013-09-13 21:34:41 +02:00
try:
2013-09-13 21:55:38 +02:00
streamurl = settings['STATIONS'][args.station]
2013-09-13 21:34:41 +02:00
except KeyError:
print('Unkown station name: ' + args.station)
sys.exit()
if streamurl.endswith('.m3u'):
2013-09-17 13:44:57 +02:00
verboseprint('Seems to be an M3U playlist. Trying to parse...')
2020-08-24 20:06:05 +02:00
pool = urllib3.PoolManager()
try:
remotefile = pool.request('GET', streamurl)
except MaxRetryError:
logging.getLogger(__name__).error('The URL of the station is somehow faulty! Check' +
args.station + ' in the Settings!')
sys.exit()
if remotefile.status != 200:
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 tmpstr:
logging.getLogger(__name__).error('Could not find a mp3 stream')
sys.exit(1)
else:
streamurl = tmpstr
2013-09-13 21:34:41 +02:00
2020-08-24 20:06:05 +02:00
verboseprint(print_time() + " ... Stream URL: " + streamurl)
target_dir = os.path.expandvars(settings['GLOBAL']['target_dir'])
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)
2020-08-24 20:06:05 +02:00
started_at = time.time()
should_end_at = started_at + (args.duration * 60)
remaining = (args.duration * 60)
# as long as recording is supposed to run
while time.time() < should_end_at:
stoprec = threading.Event()
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")
2020-08-24 20:06:05 +02:00
recthread.join(remaining)
verboseprint(print_time() + " ... Came out of rec thread again")
2020-08-24 20:06:05 +02:00
if recthread.is_alive:
2020-08-24 20:06:05 +02:00
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()))
2013-09-11 16:10:16 +02:00
2016-03-10 15:10:28 +01:00
2013-09-16 15:43:29 +02:00
def list(args):
2019-03-18 21:34:29 +01:00
settings = read_settings(args)
2013-09-18 15:19:04 +02:00
for key in sorted(settings['STATIONS']):
2013-09-16 15:43:29 +02:00
print(key)
2016-03-10 15:10:28 +01:00
2013-09-16 15:43:29 +02:00
def main():
parser = argparse.ArgumentParser(description='This program records internet radio streams. '
'It is free software and comes with ABSOLUTELY NO WARRANTY.')
2013-09-16 15:43:29 +02:00
subparsers = parser.add_subparsers(help='sub-command help')
parser_record = subparsers.add_parser('record', help='Record a station')
2016-03-10 15:10:28 +01:00
parser_record.add_argument('station', type=str,
help='Name of the radio station '
'(see `radiorec.py list`)')
2015-02-18 14:20:25 +01:00
parser_record.add_argument('duration', type=check_duration,
2016-03-10 15:10:28 +01:00
help='Recording time in minutes')
2015-02-18 14:20:25 +01:00
parser_record.add_argument('name', nargs='?', type=str,
2016-03-10 15:10:28 +01:00
help='A name for the recording')
parser_record.add_argument(
'-p', '--public', action='store_true',
help="Public write permissions (Linux only)")
parser_record.add_argument(
'-v', '--verbose', action='store_true', help="Verbose output")
2019-03-18 21:34:29 +01:00
parser_record.add_argument(
'-s', '--settings', nargs='?', type=str,
help="specify alternative location for settings.ini")
2013-09-16 15:43:29 +02:00
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")
if not len(sys.argv) > 1:
print('Error: No argument specified.\n')
parser.print_help()
sys.exit(1)
2013-09-16 15:43:29 +02:00
args = parser.parse_args()
args.func(args)
2013-09-11 16:10:16 +02:00
if __name__ == '__main__':
main()