Created
May 27, 2018 19:38
-
-
Save weldpua2008/4bee443b6bc9bf90ba64b32f3583e2b8 to your computer and use it in GitHub Desktop.
Query NASA image and video library for all assets related to
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| ################################################################################################# | |
| # Query the NASA image and video library for all assets related to user by default(Ilan Ramon). | |
| # From the results make a list of images with file size larger than by default (1000 kb). | |
| # Author: weldpua2008@gmail.com | |
| # Requires https://stedolan.github.io/jq/download/ | |
| ################################################################################################## | |
| user="Ilan Ramon" | |
| parallel_executions=4 | |
| limit_images=$(( 1024 * 1024 )) | |
| # | |
| if [[ "${1:-}" = "" ]] && [[ "${1:-}" = " " ]];then | |
| user="${1:-}" | |
| fi | |
| get_line(){ | |
| local nasa_id=${1:-} | |
| [[ "${nasa_id}" = "" ]] && return 1 | |
| [[ "${nasa_id}" = " " ]] && return 1 | |
| metadata_url=$(curl -s https://images-api.nasa.gov/asset//${nasa_id}| jq -r '.collection.items|.[]|select(.href| contains("metadata.json"))|.href') | |
| if [[ "${metadata_url}" = " " ]] || [[ "${metadata_url}" = "" ]] || [[ "${metadata_url}" = "null" ]];then return 1;fi | |
| file_size=$(curl -s -L ${metadata_url}|jq -r '."File:FileSize"'| awk 'BEGIN{IGNORECASE=1} {if ($2 ~ /kb/) print $1*1024; else if ($2 ~ /mb/) printf("%.0f",$1*1024*1024); else if ($2 ~ /gb/) printf("%.0f",$1*1024*1024*1024); else if ($2 ~ /tb/) printf("%.0f",$1*1024*1024*1024*1024); else print $1 }') | |
| if [[ "${file_size}" = " " ]] || [[ "${file_size}" = "" ]] || [[ "${file_size}" = "null" ]];then return 1;fi | |
| if [[ ${file_size} -gt $limit_images ]];then | |
| _href=$(curl -s https://images-api.nasa.gov/asset//${nasa_id}| jq -r '.collection.items|.[]|select(.href| contains("orig."))|.href') | |
| if [[ "${_href}" = " " ]] || [[ "${_href}" = "" ]] || [[ "${_href}" = "null" ]];then return 1;fi | |
| _size=$(echo ${file_size}|awk '{printf("%.0f",$1/1024)}') | |
| if [[ "${_size}" = " " ]] || [[ "${_size}" = "" ]] || [[ "${_size}" = "null" ]];then return 1;fi | |
| echo "${_href},${_size}" | |
| fi | |
| } | |
| check_jq(){ | |
| echo '[]'| jq . &> /dev/null | |
| if [ $? -ne 0 ];then | |
| echo "The script $0 Requires jq in your \$PATH ( https://stedolan.github.io/jq/download/)" | |
| echo "use :" | |
| echo " wget https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 -O /usr/local/bin/jq && chmod 755 /usr/local/bin/jq" | |
| exit 1 | |
| fi | |
| } | |
| main(){ | |
| check_jq | |
| export -f get_line | |
| echo 'Nasa_id,kb' | |
| curl -s -G https://images-api.nasa.gov/search --data-urlencode "q=${user}" --data-urlencode "media_type=image" | jq -r '."collection"."items"|.[]|."data"|.[]|."nasa_id"'|xargs -d '\n' -P ${parallel_executions:-10} -n 100 -I % bash -c 'get_line "%"' | |
| } | |
| ###### MAIN FUNC | |
| main |
Author
weldpua2008
commented
May 27, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment