#!/bin/bash # Creates a list from application/json files # Kaotisk Hund 2024 # # Convention: # { # "started_on":..., # "duration":..., # "list":[ # { # "index":..., # "hash":..., # "duration":..., # "starts_on":... # }, # ... # ] # } # # Just for convenience, we will simply make up a list of files found in hashes # we will be searching for files smaller than 4096 bytes. An extra check with # `file` to find 'JSON text data'. if [ -f "hashes/list" ] then echo 'ERROR: This script creates the first list only' echo 'Use ./append_latest_list.sh instead' exit 1 fi json_file_list="$(mktemp)" find hashes -type f -size -4096 | sort | while read filepath do file ${filepath} | grep 'JSON text data' > /dev/null 2>&1 if [ $? -eq 0 ] then if [ "$(cat ${filepath} | jq -r '.type')" != "show" ] then echo "Not a show: ${filepath}" continue fi if [ "$(cat ${filepath} | jq -r '.duration')" == "null" ] then echo "No duration field: ${filepath}" exit 1 fi if [ ! $(cat ${filepath} | jq -r '.created_on') -gt 0 ] then echo "No created_on field: ${filepath}" exit 1 fi if [ ! $(cat ${filepath} | jq -r '.published_on') -gt 0 ] then echo "No published_on field: ${filepath}" exit 1 fi if [ ! -n "$(cat ${filepath} | jq -r '.hash')" ] then echo "No hash field: ${filepath}" exit 1 fi if [ ! -n "$(cat ${filepath} | jq -r '.filename')" ] then echo "No file_extension field: ${filepath}" exit 1 fi if [ ! -n "$(cat ${filepath} | jq -r '.file_extension')" ] then echo "No file_extension field: ${filepath}" exit 1 fi if [ ! -n "$(cat ${filepath} | jq -r '.mimetype')" ] then echo "No mimetype field: ${filepath}" exit 1 fi # Optional so we don't check for those # cat ${filepath} | jq -r '.artist' # cat ${filepath} | jq -r '.title' echo ${filepath} >> ${json_file_list} fi done cat ${json_file_list} new_list_file="$(mktemp)" index=0 starts_on=0 total_duration="$(mktemp)" echo -n 0 > ${total_duration} ( echo '{' echo '"type":"list",' echo '"started_on":"'$(( $(date -u +%s) - 1800 ))000'",' echo '"list":[' cat ${json_file_list} | while read cur_file do if [ -f "${cur_file}" ] && [ -n "${cur_file}" ] then echo '//'${cur_file}'//' >&2 cat ${cur_file} | jq >&2 if [ ${index} -gt 0 ] then echo ',' fi echo '{' echo '"index":"'${index}'",' hash_string="$(cat ${cur_file} | jq -r '.hash')" echo '"hash":"'$(basename ${cur_file})'",' duration="$(cat ${cur_file} | jq -r '.duration')" echo '"duration":"'${duration}'",' echo '"starts_on":"'${starts_on}'"}' starts_on=$(( ${duration} + ${starts_on} )) index=$(( ${index} + 1 )) echo -n $(( $(cat ${total_duration}) + ${duration} )) > ${total_duration} fi done echo '],' echo '"duration":"'$(cat ${total_duration})'"' echo '}' ) | jq -c -M > ${new_list_file} sha_live=$(sha512sum ${new_list_file}|cut -d ' ' -f 1) cat ${new_list_file} | jq echo '{"latest_list":"'${sha_live}'"}' > ./hashes/list mv ${new_list_file} ./hashes/${sha_live} #rm ${new_list_file} rm ${json_file_list}