summaryrefslogtreecommitdiff
path: root/fetch_program.sh
blob: 4a93b0a01f8ef116f5e0b786ba99a839f10e145a (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
#!/bin/sh
set -e

year="${1:-$(date +%Y)}"
echo "Fetching for year: $year"
current_time="$(date)"
dir="programs/$year"
mkdir -p "$dir"
file_path="$dir/bornhack_program_$year_${current_time}"
xml_path="$file_path.xml"
json_path="$file_path.json"
last_xml="$(ls -1 programs/$year/bornhack_program_*.xml 2>/dev/null | LC_ALL=C sort | tail -n 1)"
last_json="${last_xml%.*}.json"

curl "https://bornhack.dk/bornhack-$year/program/frab.xml" > "$xml_path"

symlink() {
  ln -sfr "$json_path" "programs/next.json"
  mv  --no-copy "programs/next.json" "programs/current.json"
  stat -c %N programs/current.json
}


if [ -f "$last_xml" ]; then
  if diff -q "$xml_path" "$last_xml" >/dev/null; then
    echo "No change in program"
    # echo "Newly fetched program: $xml_path is the same as last program: $last_program, cleaning up last_program"
    rm -f -- "$last_xml"
    mv "$last_json" "$json_path"
    symlink
    exit 0
  fi
fi

cat "$xml_path" | xq '.schedule.day | map(.room) | flatten | map(.event) | map(select(. != null)) | flatten | sort_by(.date)' > "$json_path"

if [ -f "$last_json" ]; then
  diff --color=always "$last_json" "$json_path"
  rm -f -- "$last_json"
fi

symlink

echo 'New program content fetched'