|
|
|
import os
|
|
|
|
import random
|
|
|
|
import string
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from functools import lru_cache
|
|
|
|
from mutagen.mp3 import MP3
|
|
|
|
|
|
|
|
|
|
|
|
def random_choices(population, k):
|
|
|
|
"""Emulate random.choices for Python < 3.6"""
|
|
|
|
return [random.choice(population) for _ in range(k)]
|
|
|
|
|
|
|
|
|
|
|
|
def mp3_sources(data_dir, pattern):
|
|
|
|
def start_time_for_source_fn(fn):
|
|
|
|
return datetime.strptime(os.path.basename(fn), pattern)
|
|
|
|
|
|
|
|
@lru_cache(maxsize=None)
|
|
|
|
def length_for_source_fn(fn):
|
|
|
|
return timedelta(seconds=MP3(fn).info.length)
|
|
|
|
|
|
|
|
def get_sources():
|
|
|
|
# Get a sorted list of all source files with start time and length
|
|
|
|
sources = []
|
|
|
|
for fn in os.listdir(data_dir):
|
|
|
|
fn = os.path.join(data_dir, fn)
|
|
|
|
try:
|
|
|
|
start_time = start_time_for_source_fn(fn)
|
|
|
|
length = length_for_source_fn(fn)
|
|
|
|
sources.append({'fn': fn, 'start_time': start_time, 'length': length})
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return sources
|
|
|
|
|
|
|
|
return get_sources
|
|
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
SECRET_KEY = os.environ.get('SECRET_KEY') or \
|
|
|
|
''.join(random_choices(string.ascii_letters, k=20))
|
|
|
|
OUT_DIR = os.environ.get('OUT_DIR') or '/var/tmp/prolefeeder-test-out'
|
|
|
|
MAX_LENGTH = os.environ.get('MAX_LENGTH') or 180
|
|
|
|
|
|
|
|
DEBUG = False
|
|
|
|
|
|
|
|
if not os.path.exists(OUT_DIR):
|
|
|
|
raise RuntimeError("OUT_DIR does not exist")
|
|
|
|
|
|
|
|
# Here's the interesting part: configuring the source.
|
|
|
|
#
|
|
|
|
# We need to return a function that returns a list of dicts of source files with metadata, e.g.
|
|
|
|
# [ {"fn": "/path/to/foo-20:01.mp3", "start_time": datetime(...), "length": timedelta(hours=1)} ]
|
|
|
|
|
|
|
|
data_dir = os.environ.get('DATA_DIR') or '/var/tmp/prolefeeder-test-data'
|
|
|
|
pattern = 'qfhi-%Y%m%d-%H%M.mp3'
|
|
|
|
|
|
|
|
if not os.path.exists(data_dir):
|
|
|
|
raise RuntimeError("DATA_DIR does not exist")
|
|
|
|
|
|
|
|
SOURCES = mp3_sources(data_dir, pattern)
|