102 lines
3.5 KiB
Python
102 lines
3.5 KiB
Python
import argparse
|
|
import datetime
|
|
import json
|
|
import os
|
|
|
|
# globals
|
|
mediadir = "/var/lib/ffplayout/tv-media"
|
|
playlistdir = "/var/lib/ffplayout/playlists"
|
|
channel = "newellijaytelevision"
|
|
verbose = False
|
|
from_date = datetime.date.today()
|
|
to_date = datetime.date.today()
|
|
playlist_duration = 6 * 60 * 60 # six hours, in seconds
|
|
|
|
def main():
|
|
parse_config()
|
|
for single_date in daterange(from_date, to_date):
|
|
if verbose:
|
|
print("Processing date", single_date)
|
|
build_day(single_date)
|
|
|
|
def parse_config():
|
|
global mediadir, playlistdir, channel, verbose, from_date, to_date, playlist_duration
|
|
parser = argparse.ArgumentParser(
|
|
prog="mt_clockwheel",
|
|
description="a simple clockwheel playlist generator for ffplayout",
|
|
epilog="brought to you by your friends at Mountain Town Technology and Community Media Network"
|
|
)
|
|
|
|
parser.add_argument('-d', '--directory', dest='mediadir', help='root directory for media library')
|
|
parser.add_argument('-p', '--playlists', dest='playlistdir', help='root directory for playlists')
|
|
parser.add_argument('-c', '--channel', dest='channel', help='channel name to generate playlists for')
|
|
parser.add_argument('-v', '--verbose', action='store_true', help='provide verbose output logging')
|
|
parser.add_argument('-f', '--from', dest='from_date', help='first date to generate playlist for')
|
|
parser.add_argument('-t', '--to', dest='to_date', help='last date to generate playlist for')
|
|
parser.add_argument('-l', '--length', dest='playlist_duration', help='duration (in seconds) for each playlist - if a factor of 24 * 60 * 60 then playlist will be duplicated to fill 24 hours')
|
|
|
|
args = parser.parse_args()
|
|
if args.verbose:
|
|
verbose = True
|
|
if args.mediadir:
|
|
mediadir = args.mediadir
|
|
if args.playlistdir:
|
|
playlistdir = args.playlistdir
|
|
if args.channel:
|
|
channel = args.channel
|
|
if args.from_date:
|
|
from_date = datetime.datetime.strptime(args.from_date, "%Y-%m-%d").date()
|
|
if args.to_date:
|
|
to_date = datetime.datetime.strptime(args.to_date, "%Y-%m-%d").date()
|
|
if args.playlist_duration:
|
|
playlist_duration = args.playlist_duration
|
|
|
|
if verbose:
|
|
print("Arguments parsed, config:")
|
|
print("mediadir:", mediadir)
|
|
print("playlistdir:", playlistdir)
|
|
print("channel:", channel)
|
|
print("from_date:", from_date)
|
|
print("to_date:", to_date)
|
|
|
|
def build_day(this_date):
|
|
d = {} # empty dict that will become our JSON output
|
|
|
|
d["channel"] = channel
|
|
d["date"] = this_date.strftime("%Y-%m-%d")
|
|
d["program"] = [] # empty list to populate with programs
|
|
|
|
total_time = 0
|
|
|
|
while total_time < playlist_duration:
|
|
entry, length = get_playlist_entry()
|
|
d["program"].append(entry)
|
|
total_time += length
|
|
if verbose:
|
|
print(' added program:', json.dumps(entry), length)
|
|
|
|
# TODO: see if playlist_duration is a factor of 24 * 60 * 60 and, if so, duplicate it to fill 24 hours
|
|
|
|
if verbose:
|
|
print("playlist json:", json.dumps(d))
|
|
|
|
def get_playlist_entry():
|
|
entry = {
|
|
"in": 0,
|
|
"out": 300,
|
|
"duration": 300,
|
|
"source": "test.mp4"
|
|
}
|
|
|
|
length = entry["duration"]
|
|
|
|
return entry, length
|
|
|
|
# generator function to yield single dates from a date range
|
|
def daterange(start_date, end_date):
|
|
for n in range(int((end_date - start_date).days) + 1): # adding 1 to make the range inclusive
|
|
yield start_date + datetime.timedelta(n)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|