aboutsummaryrefslogtreecommitdiff
path: root/create_list.sh
blob: bc101be4170ab78ebff9ce235a19deccd978db54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/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}