From 76f38cb8ac5a603d8a35c7ec7bdd7a068a26b912 Mon Sep 17 00:00:00 2001 From: scsc <754396291@qq.com> Date: Tue, 23 Jan 2024 00:18:08 +0800 Subject: [PATCH] Implement parse_tags_js with jq --- common.sh | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/common.sh b/common.sh index a8ea7ba..a17858e 100755 --- a/common.sh +++ b/common.sh @@ -3,14 +3,14 @@ one_year_ago=$(date -d "1 year ago" +%s) parse_tags_js() { is_empty=true - local date_names=($(grep -oP '(?<="last_updated":").+?(?=")|(?<="name":").+?(?=")' <(echo "$tags_js"))) - for ((i=0; i<${#date_names[@]}/2; i++)); do - date=$(date -d "${date_names[2*i]}" +%s) - name=${date_names[2*i+1]} + next_page=$(jq -r '.next' <(echo "$tags_js")) + for result in $(jq -c '.results[]' <(echo "$tags_js")); do + date=$(date -d "$(jq -r '.last_updated' <(echo "$result"))" +%s) + name=$(jq -r '.name' <(echo "$result")) if (( date > one_year_ago )); then echo "$name" is_empty=false - fi + fi done } docker-tags(){ @@ -21,11 +21,8 @@ docker-tags(){ return 1 fi parse_tags_js - while next_page=$(grep -oP '(?<="next":").+?(?=")' <(echo "$tags_js") | sed 's/\\u0026/\&/' | xargs) + while [[ -n "$next_page" && "$next_page" != "null" && $is_empty == false ]] do - if [[ -z "$next_page" || "$next_page" == "null" || $is_empty == true ]]; then - break - fi tags_js=$(curl -sSL "$next_page") if [ $? -ne 0 ]; then echo "curl $next_page failed" >&2