Using bash, awk, and jq to append data to Astro content collection

Using bash, awk, and jq to append data to Astro content collection

src/data/videos.json
[
{
"id": 1,
"videos": {
"<YOUTUBE_CHANNEL_NAME_1>": [
{
"id": "<YOUTUBE_VIDEO_ID>",
"title": "<YOUTUBE_VIDEO_TITLE>"
}
],
"<YOUTUBE_CHANNEL_NAME_2>": [
{
"id": "<YOUTUBE_VIDEO_ID>",
"title": "<YOUTUBE_VIDEO_TITLE>"
},
{
"id": "<YOUTUBE_VIDEO_ID>",
"title": "<YOUTUBE_VIDEO_TITLE>"
}
]
}
}
]
scripts/get_latest_videos.sh
#!/usr/bin/env bash
declare -A channel0=([channelName]='channel-1' [channelId]='UUbRP3c757lWg9M-U7TyEkXA')
declare -A channel1=([channelName]='channel-2' [channelId]='UUUyeluBRhGPCW4rPe_UvBZQ')
youtubeAPIKey=""
> latest_videos
declare -n channel
for channel in ${!channel@}; do
video=$(curl -s "https://www.googleapis.com/youtube/v3/playlistItems?key=${youtubeAPIKey}&maxResults=1&part=snippet&playlistId=${channel[channelId]}")
videoId=$(echo $video | jq ".items[].snippet.resourceId.videoId")
title=$(echo $video | jq ".items[].snippet.title")
echo "${channel[channelName]};${videoId};${title}" >> latest_videos
done
awk -F';' '{print $1 " " $2 " " $3}' latest_videos | xargs -l ./scripts/append_video.sh
latest_videos
<YOUTUBE_CHANNEL_NAME_1>;"<YOUTUBE_VIDEO_ID>";"<YOUTUBE_VIDEO_TITLE>"
<YOUTUBE_CHANNEL_NAME_2>;"<YOUTUBE_VIDEO_ID>";"<YOUTUBE_VIDEO_TITLE>"
scripts/append_video.sh
#!/usr/bin/env bash
channelName=$1
id=$2
title=$3
cat ./src/data/videos.json | echo $(jq ".[0].videos[\"${channelName}\"] += [{ "id": \"${id}\", "title": \"${title}\" }]") > ./src/data/videos.json